@@ -167,50 +167,45 @@ Well, defining a query takes place in two steps:
167
167
168
168
To specify the query name and arguments, you simply add an entry to
169
169
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:
171
171
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
173
173
174
174
``` 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
+ }
178
182
179
183
...
180
184
}
181
185
```
182
186
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:
184
190
185
191
``` 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
192
198
| name of query
193
- query flags
199
+ query keyword
194
200
```
195
201
196
202
Let's go over them one by one:
197
203
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.
201
205
- ** Name of query:** the name of the query method
202
206
(` tcx.type_of(..) ` ). Also used as the name of a struct
203
207
(` ty::queries::type_of ` ) that will be generated to represent
204
208
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.
214
209
- ** Query key type:** the type of the argument to this query.
215
210
This type must implement the ` ty::query::keys::Key ` trait, which
216
211
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:
222
217
which is used to cheaply modify MIR in place. See the definition
223
218
of ` Steal ` for more details. New uses of ` Steal ` should ** not** be
224
219
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.
227
222
228
223
So, to add a query:
229
224
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.
232
226
- Link the provider by modifying the appropriate ` provide ` method;
233
227
or add a new one if needed and ensure that ` rustc_driver ` is invoking it.
234
228
235
229
#### Query structs and descriptions
236
230
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"
238
232
named after the query. This struct is a kind of a place-holder
239
233
describing the query. Each such struct implements the
240
234
` self::config::QueryConfig ` trait, which has associated types for the
@@ -243,11 +237,14 @@ like this:
243
237
244
238
``` rust,ignore
245
239
// 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 ()> }
247
241
248
242
impl<'tcx> QueryConfig for type_of<'tcx> {
249
243
type Key = DefId;
250
244
type Value = Ty<'tcx>;
245
+
246
+ const NAME: QueryName = QueryName::type_of;
247
+ const CATEGORY: ProfileCategory = ProfileCategory::Other;
251
248
}
252
249
```
253
250
@@ -262,9 +259,24 @@ You can put new impls into the `config` module. They look something like this:
262
259
``` rust,ignore
263
260
impl<'tcx> QueryDescription for queries::type_of<'tcx> {
264
261
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
+ }
266
276
}
267
277
}
268
278
```
269
279
280
+ ` rustc_queries ` macro will generate an appropriate ` impl ` automatically.
281
+
270
282
[ query-model ] : queries/query-evaluation-model-in-detail.html
0 commit comments