Skip to content

Commit 39dd586

Browse files
Add links to the rustc docs (#578)
1 parent f53a659 commit 39dd586

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/query.md

+31-19
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ dependencies of the local crate). Note that what determines the crate
7676
that a query is targeting is not the *kind* of query, but the *key*.
7777
For example, when you invoke `tcx.type_of(def_id)`, that could be a
7878
local query or an external query, depending on what crate the `def_id`
79-
is referring to (see the `self::keys::Key` trait for more information
80-
on how that works).
79+
is referring to (see the [`self::keys::Key`][Key] trait for more
80+
information on how that works).
8181

8282
Providers always have the same signature:
8383

@@ -96,8 +96,10 @@ They return the result of the query.
9696
#### How providers are setup
9797

9898
When the tcx is created, it is given the providers by its creator using
99-
the `Providers` struct. This struct is generated by the macros here, but it
100-
is basically a big list of function pointers:
99+
the [`Providers`][providers_struct] struct. This struct is generated by
100+
the macros here, but it is basically a big list of function pointers:
101+
102+
[providers_struct]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/query/struct.Providers.html
101103

102104
```rust,ignore
103105
struct Providers {
@@ -110,11 +112,13 @@ At present, we have one copy of the struct for local crates, and one
110112
for external crates, though the plan is that we may eventually have
111113
one per crate.
112114

113-
These `Provider` structs are ultimately created and populated by
115+
These `Providers` structs are ultimately created and populated by
114116
`librustc_driver`, but it does this by distributing the work
115117
throughout the other `rustc_*` crates. This is done by invoking
116-
various `provide` functions. These functions tend to look something
117-
like this:
118+
various [`provide`][provide_fn] functions. These functions tend to look
119+
something like this:
120+
121+
[provide_fn]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/hir/fn.provide.html
118122

119123
```rust,ignore
120124
pub fn provide(providers: &mut Providers) {
@@ -147,13 +151,16 @@ fn fubar<'tcx>(tcx: TyCtxt<'tcx>, key: DefId) -> Fubar<'tcx> { ... }
147151

148152
N.B. Most of the `rustc_*` crates only provide **local
149153
providers**. Almost all **extern providers** wind up going through the
150-
[`rustc_metadata` crate][rustc_metadata], which loads the information from the
151-
crate metadata. But in some cases there are crates that provide queries for
152-
*both* local and external crates, in which case they define both a
153-
`provide` and a `provide_extern` function that `rustc_driver` can
154-
invoke.
154+
[`rustc_metadata` crate][rustc_metadata], which loads the information
155+
from the crate metadata. But in some cases there are crates that
156+
provide queries for *both* local and external crates, in which case
157+
they define both a [`provide`][ext_provide] and a
158+
[`provide_extern`][ext_provide_extern] function that `rustc_driver`
159+
can invoke.
155160

156161
[rustc_metadata]: https://github.com/rust-lang/rust/tree/master/src/librustc_metadata
162+
[ext_provide]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_llvm/attributes/fn.provide.html
163+
[ext_provide_extern]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_llvm/attributes/fn.provide_extern.html
157164

158165
### Adding a new kind of query
159166

@@ -205,7 +212,7 @@ Let's go over them one by one:
205212
(`ty::queries::type_of`) that will be generated to represent
206213
this query.
207214
- **Query key type:** the type of the argument to this query.
208-
This type must implement the `ty::query::keys::Key` trait, which
215+
This type must implement the [`ty::query::keys::Key`][Key] trait, which
209216
defines (for example) how to map it to a crate, and so forth.
210217
- **Result type of query:** the type produced by this query. This type
211218
should (a) not use `RefCell` or other interior mutability and (b) be
@@ -218,6 +225,8 @@ Let's go over them one by one:
218225
- **Query modifiers:** various flags and options that customize how the
219226
query is processed.
220227

228+
[Key]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/query/keys/trait.Key.html
229+
221230
So, to add a query:
222231

223232
- Add an entry to `rustc_queries!` using the format above.
@@ -229,7 +238,7 @@ So, to add a query:
229238
For each kind, the `rustc_queries` macro will generate a "query struct"
230239
named after the query. This struct is a kind of a place-holder
231240
describing the query. Each such struct implements the
232-
`self::config::QueryConfig` trait, which has associated types for the
241+
[`self::config::QueryConfig`][QueryConfig] trait, which has associated types for the
233242
key/value of that particular query. Basically the code generated looks something
234243
like this:
235244

@@ -247,13 +256,16 @@ impl<'tcx> QueryConfig for type_of<'tcx> {
247256
```
248257

249258
There is an additional trait that you may wish to implement called
250-
`self::config::QueryDescription`. This trait is used during cycle
251-
errors to give a "human readable" name for the query, so that we can
252-
summarize what was happening when the cycle occurred. Implementing
253-
this trait is optional if the query key is `DefId`, but if you *don't*
254-
implement it, you get a pretty generic error ("processing `foo`...").
259+
[`self::config::QueryDescription`][QueryDescription]. This trait is
260+
used during cycle errors to give a "human readable" name for the query,
261+
so that we can summarize what was happening when the cycle occurred.
262+
Implementing this trait is optional if the query key is `DefId`, but
263+
if you *don't* implement it, you get a pretty generic error ("processing `foo`...").
255264
You can put new impls into the `config` module. They look something like this:
256265

266+
[QueryConfig]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/query/trait.QueryConfig.html
267+
[QueryDescription]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/query/config/trait.QueryDescription.html
268+
257269
```rust,ignore
258270
impl<'tcx> QueryDescription for queries::type_of<'tcx> {
259271
fn describe(tcx: TyCtxt, key: DefId) -> String {

0 commit comments

Comments
 (0)