1 //! The implementation of the query system itself. This defines the macros that
2 //! generate the actual methods on tcx which find and execute the provider,
3 //! manage the caches, and so forth.
5 use crate::{on_disk_cache, queries, Queries};
6 use rustc_middle::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
7 use rustc_middle::ty::tls::{self, ImplicitCtxt};
8 use rustc_middle::ty::{self, TyCtxt};
9 use rustc_query_system::dep_graph::HasDepContext;
10 use rustc_query_system::query::{
11 QueryContext, QueryDescription, QueryJobId, QueryMap, QuerySideEffects,
14 use rustc_data_structures::sync::Lock;
15 use rustc_data_structures::thin_vec::ThinVec;
16 use rustc_errors::{Diagnostic, Handler};
17 use rustc_serialize::opaque;
18 use rustc_span::def_id::LocalDefId;
22 #[derive(Copy, Clone)]
23 pub struct QueryCtxt<'tcx> {
24 pub tcx: TyCtxt<'tcx>,
25 pub queries: &'tcx Queries<'tcx>,
28 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
29 type Target = TyCtxt<'tcx>;
32 fn deref(&self) -> &Self::Target {
37 impl HasDepContext for QueryCtxt<'tcx> {
38 type DepKind = rustc_middle::dep_graph::DepKind;
39 type DepContext = TyCtxt<'tcx>;
42 fn dep_context(&self) -> &Self::DepContext {
47 impl QueryContext for QueryCtxt<'tcx> {
48 fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
49 tls::with_related_context(**self, |icx| icx.query)
52 fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
53 self.queries.try_collect_active_jobs(**self)
56 // Interactions with on_disk_cache
57 fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
61 .map(|c| c.load_side_effects(**self, prev_dep_node_index))
65 fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
66 if let Some(c) = self.queries.on_disk_cache.as_ref() {
67 c.store_side_effects(dep_node_index, side_effects)
71 fn store_side_effects_for_anon_node(
73 dep_node_index: DepNodeIndex,
74 side_effects: QuerySideEffects,
76 if let Some(c) = self.queries.on_disk_cache.as_ref() {
77 c.store_side_effects_for_anon_node(dep_node_index, side_effects)
81 /// Executes a job by changing the `ImplicitCtxt` to point to the
82 /// new query job while it executes. It returns the diagnostics
83 /// captured during execution and the actual result.
87 token: QueryJobId<Self::DepKind>,
88 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
89 compute: impl FnOnce() -> R,
91 // The `TyCtxt` stored in TLS has the same global interner lifetime
92 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
93 // when accessing the `ImplicitCtxt`.
94 tls::with_related_context(**self, move |current_icx| {
95 // Update the `ImplicitCtxt` to point to our new query job.
96 let new_icx = ImplicitCtxt {
100 layout_depth: current_icx.layout_depth,
101 task_deps: current_icx.task_deps,
104 // Use the `ImplicitCtxt` while we execute the query.
105 tls::enter_context(&new_icx, |_| {
106 rustc_data_structures::stack::ensure_sufficient_stack(compute)
112 impl<'tcx> QueryCtxt<'tcx> {
114 pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
115 let queries = tcx.queries.as_any();
116 let queries = unsafe {
117 let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
118 let queries = queries.downcast_ref().unwrap();
119 let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
122 QueryCtxt { tcx, queries }
125 crate fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
126 self.queries.on_disk_cache.as_ref()
129 #[cfg(parallel_compiler)]
130 pub unsafe fn deadlock(self, registry: &rustc_rayon_core::Registry) {
131 rustc_query_system::query::deadlock(self, registry)
134 pub(super) fn encode_query_results(
136 encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
137 query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
138 ) -> opaque::FileEncodeResult {
139 macro_rules! encode_queries {
140 ($($query:ident,)*) => {
142 on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
151 rustc_cached_queries!(encode_queries!);
156 pub fn try_print_query_stack(
158 query: Option<QueryJobId<DepKind>>,
160 num_frames: Option<usize>,
162 rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
166 macro_rules! handle_cycle_error {
167 ([][$tcx: expr, $error:expr]) => {{
169 Value::from_cycle_error($tcx)
171 ([(fatal_cycle) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
173 $tcx.sess.abort_if_errors();
176 ([(cycle_delay_bug) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
177 $error.delay_as_bug();
178 Value::from_cycle_error($tcx)
180 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
181 handle_cycle_error!([$($modifiers)*][$($args)*])
185 macro_rules! is_anon {
189 ([(anon) $($rest:tt)*]) => {{
192 ([$other:tt $($modifiers:tt)*]) => {
193 is_anon!([$($modifiers)*])
197 macro_rules! is_eval_always {
201 ([(eval_always) $($rest:tt)*]) => {{
204 ([$other:tt $($modifiers:tt)*]) => {
205 is_eval_always!([$($modifiers)*])
209 macro_rules! hash_result {
211 Some(dep_graph::hash_result)
213 ([(no_hash) $($rest:tt)*]) => {{
216 ([$other:tt $($modifiers:tt)*]) => {
217 hash_result!([$($modifiers)*])
221 macro_rules! define_queries {
224 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
226 define_queries_struct! {
228 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
234 // Create an eponymous constructor for each query.
235 $(#[allow(nonstandard_style)] $(#[$attr])*
236 pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
237 let kind = dep_graph::DepKind::$name;
238 let name = stringify!($name);
239 // Disable visible paths printing for performance reasons.
240 // Showing visible path instead of any path is not that important in production.
241 let description = ty::print::with_no_visible_paths(
242 || ty::print::with_forced_impl_filename_line(
243 // Force filename-line mode to avoid invoking `type_of` query.
244 || queries::$name::describe(tcx, key)
246 let description = if tcx.sess.verbose() {
247 format!("{} [{}]", description, name)
251 let span = if kind == dep_graph::DepKind::def_span {
252 // The `def_span` query is used to calculate `default_span`,
253 // so exit to avoid infinite recursion.
256 Some(key.default_span(*tcx))
258 let def_id = key.key_as_def_id();
259 let def_kind = def_id
260 .and_then(|def_id| def_id.as_local())
261 // Use `tcx.hir().opt_def_kind()` to reduce the chance of
262 // accidentally triggering an infinite query loop.
263 .and_then(|def_id| tcx.hir().opt_def_kind(def_id))
264 .map(|def_kind| $crate::util::def_kind_to_simple_def_kind(def_kind));
266 let mut hcx = tcx.create_stable_hashing_context();
267 let mut hasher = StableHasher::new();
268 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
269 key.hash_stable(&mut hcx, &mut hasher);
270 hasher.finish::<u64>()
273 QueryStackFrame::new(name, description, span, def_kind, hash)
277 #[allow(nonstandard_style)]
279 use std::marker::PhantomData;
281 $(pub struct $name<$tcx> {
282 data: PhantomData<&$tcx ()>
286 $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
287 type Key = query_keys::$name<$tcx>;
288 type Value = query_values::$name<$tcx>;
289 type Stored = query_stored::$name<$tcx>;
290 const NAME: &'static str = stringify!($name);
293 impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> {
294 const ANON: bool = is_anon!([$($modifiers)*]);
295 const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
296 const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
297 const HASH_RESULT: Option<fn(&mut StableHashingContext<'_>, &Self::Value) -> Fingerprint> = hash_result!([$($modifiers)*]);
299 type Cache = query_storage::$name<$tcx>;
302 fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key>
303 where QueryCtxt<$tcx>: 'a
309 fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
312 &tcx.query_caches.$name
316 fn compute_fn(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
317 fn(TyCtxt<'tcx>, Self::Key) -> Self::Value
319 if key.query_crate_is_local() {
320 tcx.queries.local_providers.$name
322 tcx.queries.extern_providers.$name
326 fn handle_cycle_error(
327 tcx: QueryCtxt<'tcx>,
328 mut error: DiagnosticBuilder<'_>,
330 handle_cycle_error!([$($modifiers)*][tcx, error])
334 #[allow(nonstandard_style)]
335 pub mod query_callbacks {
337 use rustc_middle::dep_graph::DepNode;
338 use rustc_middle::ty::query::query_keys;
339 use rustc_query_system::dep_graph::DepNodeParams;
340 use rustc_query_system::query::{force_query, QueryDescription};
341 use rustc_query_system::dep_graph::FingerprintStyle;
343 // We use this for most things when incr. comp. is turned off.
344 pub fn Null() -> DepKindStruct {
347 is_eval_always: false,
348 fingerprint_style: FingerprintStyle::Unit,
349 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
350 try_load_from_on_disk_cache: None,
354 pub fn TraitSelect() -> DepKindStruct {
357 is_eval_always: false,
358 fingerprint_style: FingerprintStyle::Unit,
359 force_from_dep_node: None,
360 try_load_from_on_disk_cache: None,
364 pub fn CompileCodegenUnit() -> DepKindStruct {
367 is_eval_always: false,
368 fingerprint_style: FingerprintStyle::Opaque,
369 force_from_dep_node: None,
370 try_load_from_on_disk_cache: None,
374 pub fn CompileMonoItem() -> DepKindStruct {
377 is_eval_always: false,
378 fingerprint_style: FingerprintStyle::Opaque,
379 force_from_dep_node: None,
380 try_load_from_on_disk_cache: None,
384 $(pub fn $name()-> DepKindStruct {
385 let is_anon = is_anon!([$($modifiers)*]);
386 let is_eval_always = is_eval_always!([$($modifiers)*]);
388 let fingerprint_style =
389 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
391 if is_anon || !fingerprint_style.reconstructible() {
392 return DepKindStruct {
396 force_from_dep_node: None,
397 try_load_from_on_disk_cache: None,
402 fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> Option<query_keys::$name<'tcx>> {
403 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, &dep_node)
406 fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool {
407 if let Some(key) = recover(tcx, dep_node) {
408 let tcx = QueryCtxt::from_tcx(tcx);
409 force_query::<queries::$name<'_>, _>(tcx, key, dep_node);
416 fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: DepNode) {
417 debug_assert!(tcx.dep_graph.is_green(&dep_node));
419 let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
420 let tcx = QueryCtxt::from_tcx(tcx);
421 if queries::$name::cache_on_disk(tcx, &key, None) {
422 let _ = tcx.$name(key);
430 force_from_dep_node: Some(force_from_dep_node),
431 try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
436 pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
437 arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
442 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
443 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
444 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
445 macro_rules! define_queries_struct {
447 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
448 pub struct Queries<$tcx> {
449 local_providers: Box<Providers>,
450 extern_providers: Box<Providers>,
452 pub on_disk_cache: Option<OnDiskCache<$tcx>>,
454 $($(#[$attr])* $name: QueryState<
455 crate::dep_graph::DepKind,
456 query_keys::$name<$tcx>,
460 impl<$tcx> Queries<$tcx> {
462 local_providers: Providers,
463 extern_providers: Providers,
464 on_disk_cache: Option<OnDiskCache<$tcx>>,
467 local_providers: Box::new(local_providers),
468 extern_providers: Box::new(extern_providers),
470 $($name: Default::default()),*
474 pub(crate) fn try_collect_active_jobs(
477 ) -> Option<QueryMap<crate::dep_graph::DepKind>> {
478 let tcx = QueryCtxt { tcx, queries: self };
479 let mut jobs = QueryMap::default();
482 self.$name.try_collect_active_jobs(
484 dep_graph::DepKind::$name,
494 impl QueryEngine<'tcx> for Queries<'tcx> {
495 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
496 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
500 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
501 let qcx = QueryCtxt { tcx, queries: self };
502 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
511 key: query_keys::$name<$tcx>,
514 ) -> Option<query_stored::$name<$tcx>> {
515 let qcx = QueryCtxt { tcx, queries: self };
516 get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
522 fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
523 if def_id.is_top_level_module() {
524 "top-level module".to_string()
526 format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
530 rustc_query_description! {}