You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that this function accepts `query::Providers` not `util::Providers`. It is exceedingly rare to need a `provide` function that doesn't just accept `query::Providers`. If more than the `queries` field of `util::Providers` is being updated then `util::Providers` can be accepted instead:
154
+
Note that this function accepts `query::Providers` not `util::Providers`.
155
+
It is exceedingly rare to need a `provide` function that doesn't just accept `query::Providers`.
156
+
If more than the `queries` field of `util::Providers` is being updated then `util::Providers` can be accepted instead:
@@ -173,7 +175,8 @@ Note that `util::Providers` implements `DerefMut` to `query::Providers` so calle
173
175
174
176
#### Adding a new provider
175
177
176
-
Suppose you want to add a new query called `fubar`. This section focuses on wiring up the providers; for how to declare the query itself in the big `rustc_queries!` macro, see [Adding a new query](#adding-a-new-query) below.
178
+
Suppose you want to add a new query called `fubar`.
179
+
This section focuses on wiring up the providers; for how to declare the query itself in the big `rustc_queries!` macro, see [Adding a new query](#adding-a-new-query) below.
177
180
178
181
In practice you usually:
179
182
@@ -184,7 +187,8 @@ In practice you usually:
184
187
// existing assignments
185
188
}
186
189
```
187
-
If it exists, you will extend it to set the field for your new query. If the crate does not yet have a `provide` function, add one and make sure it is included in `DEFAULT_QUERY_PROVIDERS` in the `rustc_interface` crate so that it actually gets called during initialization (see the discussion above).
190
+
If it exists, you will extend it to set the field for your new query.
191
+
If the crate does not yet have a `provide` function, add one and make sure it is included in `DEFAULT_QUERY_PROVIDERS` in the `rustc_interface` crate so that it actually gets called during initialization (see the discussion above).
This determines whether to use the local provider from [`Providers`][providers_struct] or the external provider from [`ExternProviders`][extern_providers_struct].
211
215
212
216
2. For external crates, the query system will look for a provider in the [`ExternProviders`][extern_providers_struct] struct.
213
-
The `rustc_metadata` crate registers these external providers through the `provide_extern` function in `rustc_metadata/src/rmeta/decoder/cstore_impl.rs`. Just like:
217
+
The `rustc_metadata` crate registers these external providers through the `provide_extern` function in `rustc_metadata/src/rmeta/decoder/cstore_impl.rs`.
3. The metadata is stored in a binary format in `.rmeta` files that contains pre-computed information about the external crate, such as types, function signatures, trait implementations, and other information needed by the compiler. When an external query is made, the `rustc_metadata` crate:
230
+
3. The metadata is stored in a binary format in `.rmeta` files that contains pre-computed information about the external crate, such as types, function signatures, trait implementations, and other information needed by the compiler.
231
+
When an external query is made, the `rustc_metadata` crate:
226
232
- Loads the `.rmeta` file for the external crate
227
233
- Decodes the metadata using the `Decodable` trait
228
234
- Returns the decoded information to the query system
@@ -234,10 +240,11 @@ Here is a simplified example, when you call `tcx.type_of(def_id)` for a type def
234
240
3. The provider will load and decode the type information from the external crate's metadata
235
241
4. Return the decoded type to the caller
236
242
237
-
This is why most `rustc_*` crates only need to provide local providers - the external providers are handled by the metadata system.
243
+
This is why most `rustc_*` crates only need to provide local providers - the external providers are handled by the metadata system.
238
244
The only exception is when a crate needs to provide special handling for external queries, in which case it would implement both local and external providers.
239
245
240
-
When we define a new query that should work across crates, it does not automatically become cross-crate just because it is listed in `rustc_queries!`. You will typically need to:
246
+
When we define a new query that should work across crates, it does not automatically become cross-crate just because it is listed in `rustc_queries!`.
247
+
You will typically need to:
241
248
242
249
- Add the query to `rustc_queries!` with appropriate modifiers (for example whether it is cached on disk).
243
250
- Implement a local provider in the owning crate and register it via that crate's `provide` function.
@@ -296,17 +303,16 @@ query keyword
296
303
Let's go over these elements one by one:
297
304
298
305
-**Query keyword:** indicates a start of a query definition.
299
-
-**Name of query:** the name of the query method
300
-
(`tcx.type_of(..)`). Also used as the name of a struct
301
-
(`ty::queries::type_of`) that will be generated to represent
306
+
-**Name of query:** the name of the query method (`tcx.type_of(..)`).
307
+
Also used as the name of a struct (`ty::queries::type_of`) that will be generated to represent
302
308
this query.
303
309
-**Query key type:** the type of the argument to this query.
304
310
This type must implement the [`ty::query::keys::Key`][Key] trait, which
305
311
defines (for example) how to map it to a crate, and so forth.
306
-
-**Result type of query:** the type produced by this query. This type
307
-
should (a) not use `RefCell` or other interior mutability and (b) be
308
-
cheaply cloneable. Interning or using `Rc` or `Arc` is recommended for
309
-
non-trivial data types.[^steal]
312
+
-**Result type of query:** the type produced by this query.
313
+
This type should (a) not use `RefCell` or other interior mutability and (b) be
314
+
cheaply cloneable.
315
+
Interning or using `Rc` or `Arc` is recommended for non-trivial data types.[^steal]
310
316
-**Query modifiers:** various flags and options that customize how the
311
317
query is processed (mostly with respect to [incremental compilation][incrcomp]).
312
318
@@ -320,9 +326,9 @@ So, to add a query:
320
326
or add a new one if needed and ensure that `rustc_driver` is invoking it.
321
327
322
328
[^steal]: The one exception to those rules is the `ty::steal::Steal` type,
323
-
which is used to cheaply modify MIR in place. See the definition
324
-
of `Steal` for more details. New uses of `Steal` should **not** be
325
-
added without alerting `@rust-lang/compiler`.
329
+
which is used to cheaply modify MIR in place.
330
+
See the definition of `Steal` for more details.
331
+
New uses of `Steal` should **not** be added without alerting `@rust-lang/compiler`.
0 commit comments