Skip to content

Commit a7220f3

Browse files
committed
Add documentation on top of rustc_middle/src/query/mod.rs
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent ed44c0e commit a7220f3

File tree

1 file changed

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

1 file changed

+59
-4
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
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 macro system, 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 macro system to hold all provider functions.
15+
//! The macro system 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_interface::passes::DEFAULT_QUERY_PROVIDERS`]: ../../rustc_interface/passes/static.DEFAULT_QUERY_PROVIDERS.html
21+
//!
22+
//! ## Query Modifiers
23+
//!
24+
//! Query modifiers are special flags that alter the behavior of a query. They are parsed and processed by the macro system
25+
//! The main modifiers are:
26+
//!
27+
//! - `desc { ... }`: Sets the human-readable description for diagnostics and profiling. Required for every query.
28+
//! - `arena_cache`: Use an arena for in-memory caching of the query result.
29+
//! - `cache_on_disk_if { ... }`: Cache the query result to disk if the provided block evaluates to true.
30+
//! - `fatal_cycle`: If a dependency cycle is detected, abort compilation with a fatal error.
31+
//! - `cycle_delay_bug`: If a dependency cycle is detected, emit a delayed bug instead of aborting immediately.
32+
//! - `cycle_stash`: If a dependency cycle is detected, stash the error for later handling.
33+
//! - `no_hash`: Do not hash the query result for incremental compilation; just mark as dirty if recomputed.
34+
//! - `anon`: Make the query anonymous in the dependency graph (no dep node is created).
35+
//! - `eval_always`: Always evaluate the query, ignoring its dependencies and cached results.
36+
//! - `depth_limit`: Impose a recursion depth limit on the query to prevent stack overflows.
37+
//! - `separate_provide_extern`: Use separate provider functions for local and external crates.
38+
//! - `feedable`: Allow the query result to be set from another query ("fed" externally).
39+
//! - `return_result_from_ensure_ok`: When called via `tcx.ensure_ok()`, return `Result<(), ErrorGuaranteed>` instead of `()`.
40+
//! If the query needs to be executed and returns an error, the error is returned to the caller.
41+
//! Only valid for queries returning `Result<_, ErrorGuaranteed>`.
42+
//!
43+
//! For the up-to-date list, see the `QueryModifiers` struct in
44+
//! [`rustc_macros/src/query.rs`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/query.rs)
45+
//! and for more details in incremental compilation, see the
46+
//! [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.
47+
//!
48+
//! ## Query Expansion and Code Generation
49+
//!
50+
//! The [`rustc_macros::rustc_queries`] macro expands each query definition into:
51+
//! - A method on [`TyCtxt`] (and [`TyCtxtAt`]) for invoking the query.
52+
//! - Provider traits and structs for supplying the query's value.
53+
//! - Caching and dependency graph integration.
54+
//! - Support for incremental compilation, disk caching, and arena allocation as controlled by the modifiers.
55+
//!
56+
//! [`rustc_macros::rustc_queries`]: ../../rustc_macros/macro.rustc_queries.html
57+
//!
58+
//! The macro-based approach allows the query system to be highly flexible and maintainable, while minimizing boilerplate.
59+
//!
60+
//! For more details, see the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/query.html).
661
762
#![allow(unused_parens)]
863

0 commit comments

Comments
 (0)