|
1 |
| -//! Defines the various compiler queries. |
2 | 1 | //!
|
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 | +//! This module defines all the queries used by the Rust compiler's incremental, demand-driven query system. |
| 5 | +//! Queries are the core abstraction for most computations in the compiler: each query is a pure function |
| 6 | +//! from some key to a value, and the query system handles caching, dependency tracking, and incremental recompilation. |
| 7 | +//! |
| 8 | +//! ## How to Read This File |
| 9 | +//! |
| 10 | +//! Each `query` block defines a single query, including its key and value types, and various modifiers. |
| 11 | +//! The queries are declared using the [`rustc_macros::rustc_queries`] macro, which expands to the necessary boilerplate code |
| 12 | +//! for the query system, including provider traits, caching, and dependency graph integration. |
| 13 | +//! The macro system also supports a set of **query modifiers** (see below) that control the behavior of each query. |
| 14 | +//! |
| 15 | +//! ## Query Modifiers |
| 16 | +//! |
| 17 | +//! Query modifiers are special flags that alter the behavior of a query. They are parsed and processed by the macro system |
| 18 | +//! The main modifiers are: |
| 19 | +//! |
| 20 | +//! - `desc { ... }`: Sets the human-readable description for diagnostics and profiling. Required for every query. |
| 21 | +//! - `arena_cache`: Use an arena for in-memory caching of the query result. |
| 22 | +//! - `cache_on_disk_if { ... }`: Cache the query result to disk if the provided block evaluates to true. |
| 23 | +//! - `fatal_cycle`: If a dependency cycle is detected, abort compilation with a fatal error. |
| 24 | +//! - `cycle_delay_bug`: If a dependency cycle is detected, emit a delayed bug instead of aborting immediately. |
| 25 | +//! - `cycle_stash`: If a dependency cycle is detected, stash the error for later handling. |
| 26 | +//! - `no_hash`: Do not hash the query result for incremental compilation; just mark as dirty if recomputed. |
| 27 | +//! - `anon`: Make the query anonymous in the dependency graph (no dep node is created). |
| 28 | +//! - `eval_always`: Always evaluate the query, ignoring its dependencies and cached results. |
| 29 | +//! - `depth_limit`: Impose a recursion depth limit on the query to prevent stack overflows. |
| 30 | +//! - `separate_provide_extern`: Use separate provider functions for local and external crates. |
| 31 | +//! - `feedable`: Allow the query result to be set from another query ("fed" externally). |
| 32 | +//! - `return_result_from_ensure_ok`: When called via `tcx.ensure_ok()`, return `Result<(), ErrorGuaranteed>` instead of `()`. |
| 33 | +//! If the query needs to be executed and returns an error, the error is returned to the caller. |
| 34 | +//! Only valid for queries returning `Result<_, ErrorGuaranteed>`. |
| 35 | +//! |
| 36 | +//! For the up-to-date list, see the `QueryModifiers` struct in |
| 37 | +//! [`rustc_macros/src/query.rs`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/query.rs) |
| 38 | +//! and for more details in incremental compilation, see the |
| 39 | +//! [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. |
| 40 | +//! |
| 41 | +//! ## Query Expansion and Code Generation |
| 42 | +//! |
| 43 | +//! The [`rustc_macros::rustc_queries`] macro expands each query definition into: |
| 44 | +//! - A method on [`TyCtxt`] (and [`TyCtxtAt`]) for invoking the query. |
| 45 | +//! - Provider traits and structs for supplying the query's value. |
| 46 | +//! - Caching and dependency graph integration. |
| 47 | +//! - Support for incremental compilation, disk caching, and arena allocation as controlled by the modifiers. |
| 48 | +//! |
| 49 | +//! The macro-based approach allows the query system to be highly flexible and maintainable, while minimizing boilerplate. |
| 50 | +//! |
| 51 | +//! For more details, see the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/query.html). |
6 | 52 |
|
7 | 53 | #![allow(unused_parens)]
|
8 | 54 |
|
|
0 commit comments