Document query internals #104011
Labels
A-docs
Area: Documentation for any part of the project, including the compiler, standard library, and tools
A-query-system
Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html)
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
rustc_query_impl
andrustc_query_system
are complicated. We should have a document somewhere that explains how they work internally.We used to have a very short sketch of that in the dev-guide, but it was removed in rust-lang/rustc-dev-guide@552de58 because it wasn't clear that it was an implementation detail and not user-facing.
My short summary (I'll try and update this later):
rust/compiler/rustc_middle/src/query/mod.rs
Lines 1 to 5 in b0f3940
rustc_queries
macro.rustc_queries
is defined inrust/compiler/rustc_macros/src/query.rs
Line 293 in b0f3940
rustc_query_append
macro, as well as two modules:rust/compiler/rustc_macros/src/query.rs
Lines 357 to 374 in b0f3940
rustc_query_append
is called in various places around the compiler. It takes another macro as its input, then calls that macro with the token stream emitted byrustc_queries
. This emulates eager expansion.rustc_query_append
arerustc_middle::define_callbacks
rust/compiler/rustc_middle/src/ty/query.rs
Line 177 in d933092
rustc_query_impl::define_queries
rust/compiler/rustc_query_impl/src/plumbing.rs
Line 444 in 24ce4cf
There are 3 main crates involved in defining queries, other than rustc_macros.
rustc_query_system
comes earliest in the dependency graph. It defines various traits along with some helper functions. It is very fast to compile. We may want to rename this to rustc_query_traits at some point.rustc_middle
comes next and depends on query_system. It defines TyCtxt and various helper functions that are used elsewhere in the compiler; this is the first place we can invokerustc_queries
, since before this we don't have access to TyCtxt. This is slow to compile but not because of the query system, it's just big.rustc_query_impl
depends on rustc_middle. It implements the traits in rustc_query_system for the queries declared in rustc_middle. It takes an extremely long time to compile.Because query_impl takes so long to compile, we don't depend on it anywhere except for rustc_interface. In particular, we use function pointers and dynamic dispatch as necessary so that rustc_middle and later crates don't need to depend on query internals.
cc @cjgillot @Nilstrieb
The text was updated successfully, but these errors were encountered: