Skip to content

Commit 7503057

Browse files
rasendubimark-i-m
authored andcommitted
Update query chapter for the query macro rewrite
There was a big macro rewrite in these pull requests: rust-lang/rust#56462 rust-lang/rust#59517 Update the query chapter to describe the new macro usage.
1 parent 07d1d3c commit 7503057

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

src/query.md

+44-32
Original file line numberDiff line numberDiff line change
@@ -167,50 +167,45 @@ Well, defining a query takes place in two steps:
167167

168168
To specify the query name and arguments, you simply add an entry to
169169
the big macro invocation in
170-
[`src/librustc/ty/query/mod.rs`][query-mod], which looks something like:
170+
[`src/librustc/query/mod.rs`][query-mod], which looks something like:
171171

172-
[query-mod]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/query/index.html
172+
[query-mod]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/query/index.html
173173

174174
```rust,ignore
175-
define_queries! { <'tcx>
176-
/// Records the type of every item.
177-
[] fn type_of: TypeOfItem(DefId) -> Ty<'tcx>,
175+
rustc_queries! {
176+
Other {
177+
/// Records the type of every item.
178+
query type_of(key: DefId) -> Ty<'tcx> {
179+
cache { key.is_local() }
180+
}
181+
}
178182
179183
...
180184
}
181185
```
182186

183-
Each line of the macro defines one query. The name is broken up like this:
187+
Queries are grouped into categories (`Other`, `Codegen`, `TypeChecking`, etc.).
188+
Each group contains one or more queries. Each query definition is broken up like
189+
this:
184190

185191
```rust,ignore
186-
[] fn type_of: TypeOfItem(DefId) -> Ty<'tcx>,
187-
^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^ ^^^^^^^^
188-
| | | | |
189-
| | | | result type of query
190-
| | | query key type
191-
| | dep-node constructor
192+
query type_of(key: DefId) -> Ty<'tcx> { ... }
193+
^^ ^^^^^^^ ^^^^^ ^^^^^^^^ ^^^
194+
| | | | |
195+
| | | | query modifiers
196+
| | | result type of query
197+
| | query key type
192198
| name of query
193-
query flags
199+
query keyword
194200
```
195201

196202
Let's go over them one by one:
197203

198-
- **Query flags:** these are largely unused right now, but the intention
199-
is that we'll be able to customize various aspects of how the query is
200-
processed.
204+
- **Query keyword:** indicates a start of a query definition.
201205
- **Name of query:** the name of the query method
202206
(`tcx.type_of(..)`). Also used as the name of a struct
203207
(`ty::queries::type_of`) that will be generated to represent
204208
this query.
205-
- **Dep-node constructor:** indicates the constructor function that
206-
connects this query to incremental compilation. Typically, this is a
207-
`DepNode` variant, which can be added by modifying the
208-
`define_dep_nodes!` macro invocation in
209-
[`librustc/dep_graph/dep_node.rs`][dep-node].
210-
- However, sometimes we use a custom function, in which case the
211-
name will be in snake case and the function will be defined at the
212-
bottom of the file. This is typically used when the query key is
213-
not a def-id, or just not the type that the dep-node expects.
214209
- **Query key type:** the type of the argument to this query.
215210
This type must implement the `ty::query::keys::Key` trait, which
216211
defines (for example) how to map it to a crate, and so forth.
@@ -222,19 +217,18 @@ Let's go over them one by one:
222217
which is used to cheaply modify MIR in place. See the definition
223218
of `Steal` for more details. New uses of `Steal` should **not** be
224219
added without alerting `@rust-lang/compiler`.
225-
226-
[dep-node]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/dep_graph/struct.DepNode.html
220+
- **Query modifiers:** various flags and options that customize how the
221+
query is processed.
227222

228223
So, to add a query:
229224

230-
- Add an entry to `define_queries!` using the format above.
231-
- Possibly add a corresponding entry to the dep-node macro.
225+
- Add an entry to `rustc_queries!` using the format above.
232226
- Link the provider by modifying the appropriate `provide` method;
233227
or add a new one if needed and ensure that `rustc_driver` is invoking it.
234228

235229
#### Query structs and descriptions
236230

237-
For each kind, the `define_queries` macro will generate a "query struct"
231+
For each kind, the `rustc_queries` macro will generate a "query struct"
238232
named after the query. This struct is a kind of a place-holder
239233
describing the query. Each such struct implements the
240234
`self::config::QueryConfig` trait, which has associated types for the
@@ -243,11 +237,14 @@ like this:
243237

244238
```rust,ignore
245239
// Dummy struct representing a particular kind of query:
246-
pub struct type_of<'tcx> { phantom: PhantomData<&'tcx ()> }
240+
pub struct type_of<'tcx> { data: PhantomData<&'tcx ()> }
247241
248242
impl<'tcx> QueryConfig for type_of<'tcx> {
249243
type Key = DefId;
250244
type Value = Ty<'tcx>;
245+
246+
const NAME: QueryName = QueryName::type_of;
247+
const CATEGORY: ProfileCategory = ProfileCategory::Other;
251248
}
252249
```
253250

@@ -262,9 +259,24 @@ You can put new impls into the `config` module. They look something like this:
262259
```rust,ignore
263260
impl<'tcx> QueryDescription for queries::type_of<'tcx> {
264261
fn describe(tcx: TyCtxt, key: DefId) -> String {
265-
format!("computing the type of `{}`", tcx.item_path_str(key))
262+
format!("computing the type of `{}`", tcx.def_path_str(key))
263+
}
264+
}
265+
```
266+
267+
Another option is to add `desc` modifier:
268+
269+
```rust,ignore
270+
rustc_queries! {
271+
Other {
272+
/// Records the type of every item.
273+
query type_of(key: DefId) -> Ty<'tcx> {
274+
desc { |tcx| "computing the type of `{}`", tcx.def_path_str(key) }
275+
}
266276
}
267277
}
268278
```
269279

280+
`rustc_queries` macro will generate an appropriate `impl` automatically.
281+
270282
[query-model]: queries/query-evaluation-model-in-detail.html

0 commit comments

Comments
 (0)