8000 rustc-dev-guide subtree update by tshepang · Pull Request #142743 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

rustc-dev-guide subtree update #142743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jun 20, 2025
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9aa572d
use consistent title capitalization
tshepang May 22, 2025
a4c8ef9
Adjust some doc for Query System
xizheyin Jun 13, 2025
94d56d1
Merge pull request #2441 from rust-lang/tshepang-remove-title-case
tshepang Jun 14, 2025
9265493
Merge pull request #2465 from xizheyin/rustc-query
tshepang Jun 14, 2025
a70deb6
content has moved to another chapter
tshepang Jun 14, 2025
9926215
Merge pull request #2466 from rust-lang/tshepang-moved
tshepang Jun 14, 2025
0834e48
use sentence case
tshepang Jun 14, 2025
a3f261c
Merge pull request #2467 from rust-lang/tshepang-patch-1
tshepang Jun 14, 2025
820e88a
title case
tshepang Jun 14, 2025
21a1e51
do not inline links
tshepang Jun 14, 2025
d16e1a1
Merge pull request #2468 from rust-lang/query-cleaning
tshepang Jun 14, 2025
808d16c
Use stage 1 for building docs
Darksonn Jun 16, 2025
3edb332
Merge pull request #2471 from Darksonn/patch-1
Noratrieb Jun 16, 2025
1dbf86a
Remove hanging parenthesis from example signature.
cbloodsworth Jun 16, 2025
02334e1
Merge pull request #2472 from cbloodsworth/fix/tytable_parens
BoxyUwU Jun 16, 2025
8bc55da
Profiling with perf: specify the section of bootstrap settings.
lolbinarycat Jun 17, 2025
671817f
Merge pull request #2475 from lolbinarycat/patch-3
Noratrieb Jun 17, 2025
d96b2d4
Stub chapter and consolidate under `/hir/`
BoxyUwU Jun 6, 2025
42888bb
Write chapter on Unambig vs Ambig Types/Consts
BoxyUwU Jun 17, 2025
c1506c0
Add links
BoxyUwU Jun 17, 2025
39e9800
Reviews
BoxyUwU Jun 18, 2025
8000
6c7830e
Merge pull request #2474 from BoxyUwU/ambig_unambig_ty_consts
BoxyUwU Jun 18, 2025
4145596
fix markup
tshepang Jun 18, 2025
79652d9
Merge pull request #2476 from rust-lang/tshepang-patch-1
tshepang Jun 18, 2025
0a185e4
initial instructions for gpu offload
ZuseZ4 Jun 2, 2025
f25cfe8
Merge pull request #2447 from rust-lang/offload-docs
ZuseZ4 Jun 19, 2025
48b36ee
Preparing for merge from rustc
invalid-email-address Jun 19, 2025
d854d56
Merge from rustc
invalid-email-address Jun 19, 2025
6ad42bf
Merge pull request #2477 from rust-lang/rustc-pull
tshepang Jun 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adjust some doc for Query System
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
< 8000 /div>
  • Loading branch information
xizheyin committed Jun 13, 2025
commit a4c8ef9f341217b103dfd4d0d9d9a641cda6ef4c
85 changes: 46 additions & 39 deletions src/doc/rustc-dev-guide/src/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ are cheaply cloneable; insert an `Rc` if necessary).
### Providers

If, however, the query is *not* in the cache, then the compiler will
try to find a suitable **provider**. A provider is a function that has
been defined and linked into the compiler somewhere that contains the
code to compute the result of the query.
call the corresponding **provider** function. A provider is a function
implemented in a specific module and **manually registered** into the
[`Providers`][providers_struct] struct during compiler initialization.
The macro system generates the [`Providers`][providers_struct] struct,
which acts as a function table for all query implementations, where each
field is a function pointer to the actual provider.

**Note:** The `Providers` struct is generated by macros and acts as a function table for all query implementations.
It is **not** a Rust trait, but a plain struct with function pointer fields.

**Providers are defined per-crate.** The compiler maintains,
internally, a table of providers for every crate, at least
Expand Down Expand Up @@ -97,7 +103,18 @@ fn provider<'tcx>(
Providers take two arguments: the `tcx` and the query key.
They return the result of the query.

### How providers are setup
N.B. Most of the `rustc_*` crates only provide **local
providers**. Almost all **extern providers** wind up going through the
[`rustc_metadata` crate][rustc_metadata], which loads the information
from the crate metadata. But in some cases there are crates that
provide queries for *both* local and external crates, in which case
they define both a `provide` and a `provide_extern` function, through
[`wasm_import_module_map`][wasm_import_module_map], that `rustc_driver` can invoke.

[rustc_metadata]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/index.html
[wasm_import_module_map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/back/symbol_export/fn.wasm_import_module_map.html

### How providers are set up

When the tcx is created, it is given the providers by its creator using
the [`Providers`][providers_struct] struct. This struct is generated by
Expand All @@ -108,61 +125,51 @@ the macros here, but it is basically a big list of function pointers:
```rust,ignore
struct Providers {
type_of: for<'tcx> fn(TyCtxt<'tcx>, DefId) -> Ty<'tcx>,
...
// ... one field for each query
}
```

At present, we have one copy of the struct for local crates, and one
for external crates, though the plan is that we may eventually have
one per crate.
#### How are providers registered?

The `Providers` struct is filled in during compiler initialization, mainly by the `rustc_driver` crate.
But the actual provider functions are implemented in various `rustc_*` crates (like `rustc_middle`, `rustc_hir_analysis`, etc).

These `Providers` structs are ultimately created and populated by
`rustc_driver`, but it does this by distributing the work
throughout the other `rustc_*` crates. This is done by invoking
various [`provide`][provide_fn] functions. These functions tend to look
something like this:
To register providers, each crate exposes a [`provide`][provide_fn] function that looks like this:

[provide_fn]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/fn.provide.html

```rust,ignore
pub fn provide(providers: &mut Providers) {
*providers = Providers {
type_of,
// ... add more providers here
..*providers
};
}
```

That is, they take an `&mut Providers` and mutate it in place. Usually
we use the formulation above just because it looks nice, but you could
as well do `providers.type_of = type_of`, which would be equivalent.
(Here, `type_of` would be a top-level function, defined as we saw
before.) So, if we want to add a provider for some other query,
let's call it `fubar`, into the crate above, we might modify the `provide()`
function like so:
- This function takes a mutable reference to the `Providers` struct and sets the fields to point to the correct provider functions.
- You can also assign fields individually, e.g. `providers.type_of = type_of;`.

```rust,ignore
pub fn provide(providers: &mut Providers) {
*providers = Providers {
type_of,
fubar,
..*providers
};
}
#### Adding a new provider

fn fubar<'tcx>(tcx: TyCtxt<'tcx>, key: DefId) -> Fubar<'tcx> { ... }
```
Suppose you want to add a new query called `fubar`. You would:

N.B. Most of the `rustc_*` crates only provide **local
providers**. Almost all **extern providers** wind up going through the
[`rustc_metadata` crate][rustc_metadata], which loads the information
from the crate metadata. But in some cases there are crates that
provide queries for *both* local and external crates, in which case
they define both a `provide` and a `provide_extern` function, through
[`wasm_import_module_map`][wasm_import_module_map], that `rustc_driver` can invoke.
1. Implement the provider function:
```rust,ignore
fn fubar<'tcx>(tcx: TyCtxt<'tcx>, key: DefId) -> Fubar<'tcx> { ... }
```
2. Register it in the `provide` function:
```rust,ignore
pub fn provide(providers: &mut Providers) {
*providers = Providers {
fubar,
..*providers
};
}
```

[rustc_metadata]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/index.html
[wasm_import_module_map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/back/symbol_export/fn.wasm_import_module_map.html
---

## Adding a new query

Expand Down
0