Skip to content

Commit a6ff53f

Browse files
authored
Unrolled build for #142450
Rollup merge of #142450 - xizheyin:rustc-query-doc, r=SparrowLii Add documentation on top of `rustc_middle/src/query/mod.rs` The `rustc-dev-guide` gives a high-level intro, but many details—especially about how the code works and modifiers in `query xxx(){...}`—are only in code comments or the macro implementation. This doc makes it easier for contributors and code readers to understand the workflow and available modifiers without jumping between files and docs. This PR adds a comprehensive module-level doc comment to `rustc_middle::query::mod.rs` that: 1. Provides an overview of the query system and macro-based query definitions for reading code more easily 2. Centralizes documentation for all query modifiers (previously scattered or only in `rustc_macro` code), closely following the authoritative list in QueryModifiers.
2 parents d9ca9bd + ade2ad9 commit a6ff53f

File tree

1 file changed

+60
-4
lines changed
  • compiler/rustc_middle/src/query

1 file changed

+60
-4
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
1-
//! Defines the various compiler queries.
21
//!
3-
//! For more information on the query system, see
4-
//! ["Queries: demand-driven compilation"](https://rustc-dev-guide.rust-lang.org/query.html).
5-
//! This chapter includes instructions for adding new queries.
2+
//! # The rustc Query System: Query Definitions and Modifiers
3+
//!
4+
//! The core processes in rustc are shipped as queries. Each query is a demand-driven function from some key to a value.
5+
//! The execution result of the function is cached and directly read during the next request, thereby improving compilation efficiency.
6+
//! Some results are saved locally and directly read during the next compilation, which are core of incremental compilation.
7+
//!
8+
//! ## How to Read This Module
9+
//!
10+
//! Each `query` block in this file defines a single query, specifying its key and value types, along with various modifiers.
11+
//! These query definitions are processed by the [`rustc_macros`], which expands them into the necessary boilerplate code
12+
//! for the query system—including the [`Providers`] struct (a function table for all query implementations, where each field is
13+
//! a function pointer to the actual provider), caching, and dependency graph integration.
14+
//! **Note:** The `Providers` struct is not a Rust trait, but a struct generated by the `rustc_macros` to hold all provider functions.
15+
//! The `rustc_macros` also supports a set of **query modifiers** (see below) that control the behavior of each query.
16+
//!
17+
//! The actual provider functions are implemented in various modules and registered into the `Providers` struct
18+
//! during compiler initialization (see [`rustc_interface::passes::DEFAULT_QUERY_PROVIDERS`]).
19+
//!
20+
//! [`rustc_macros`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_macros/index.html
21+
//! [`rustc_interface::passes::DEFAULT_QUERY_PROVIDERS`]: ../../rustc_interface/passes/static.DEFAULT_QUERY_PROVIDERS.html
22+
//!
23+
//! ## Query Modifiers
24+
//!
25+
//! Query modifiers are special flags that alter the behavior of a query. They are parsed and processed by the `rustc_macros`
26+
//! The main modifiers are:
27+
//!
28+
//! - `desc { ... }`: Sets the human-readable description for diagnostics and profiling. Required for every query.
29+
//! - `arena_cache`: Use an arena for in-memory caching of the query result.
30+
//! - `cache_on_disk_if { ... }`: Cache the query result to disk if the provided block evaluates to true.
31+
//! - `fatal_cycle`: If a dependency cycle is detected, abort compilation with a fatal error.
32+
//! - `cycle_delay_bug`: If a dependency cycle is detected, emit a delayed bug instead of aborting immediately.
33+
//! - `cycle_stash`: If a dependency cycle is detected, stash the error for later handling.
34+
//! - `no_hash`: Do not hash the query result for incremental compilation; just mark as dirty if recomputed.
35+
//! - `anon`: Make the query anonymous in the dependency graph (no dep node is created).
36+
//! - `eval_always`: Always evaluate the query, ignoring its dependencies and cached results.
37+
//! - `depth_limit`: Impose a recursion depth limit on the query to prevent stack overflows.
38+
//! - `separate_provide_extern`: Use separate provider functions for local and external crates.
39+
//! - `feedable`: Allow the query result to be set from another query ("fed" externally).
40+
//! - `return_result_from_ensure_ok`: When called via `tcx.ensure_ok()`, return `Result<(), ErrorGuaranteed>` instead of `()`.
41+
//! If the query needs to be executed and returns an error, the error is returned to the caller.
42+
//! Only valid for queries returning `Result<_, ErrorGuaranteed>`.
43+
//!
44+
//! For the up-to-date list, see the `QueryModifiers` struct in
45+
//! [`rustc_macros/src/query.rs`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/query.rs)
46+
//! and for more details in incremental compilation, see the
47+
//! [Query modifiers in incremental compilation](https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation-in-detail.html#query-modifiers) section of the rustc-dev-guide.
48+
//!
49+
//! ## Query Expansion and Code Generation
50+
//!
51+
//! The [`rustc_macros::rustc_queries`] macro expands each query definition into:
52+
//! - A method on [`TyCtxt`] (and [`TyCtxtAt`]) for invoking the query.
53+
//! - Provider traits and structs for supplying the query's value.
54+
//! - Caching and dependency graph integration.
55+
//! - Support for incremental compilation, disk caching, and arena allocation as controlled by the modifiers.
56+
//!
57+
//! [`rustc_macros::rustc_queries`]: ../../rustc_macros/macro.rustc_queries.html
58+
//!
59+
//! The macro-based approach allows the query system to be highly flexible and maintainable, while minimizing boilerplate.
60+
//!
61+
//! For more details, see the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/query.html).
662
763
#![allow(unused_parens)]
864

0 commit comments

Comments
 (0)