1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The implementation of the query system itself. Defines the macros
12 //! that generate the actual methods on tcx which find and execute the
13 //! provider, manage the caches, and so forth.
15 use dep_graph::{DepNodeIndex, DepNode, DepKind, DepNodeColor};
16 use errors::DiagnosticBuilder;
18 use errors::Diagnostic;
19 use errors::FatalError;
23 use ty::query::config::{QueryConfig, QueryDescription};
24 use ty::query::job::{QueryJob, QueryResult, QueryInfo};
27 use util::common::{profq_msg, ProfileQueriesMsg, QueryMsg};
29 use rustc_data_structures::fx::{FxHashMap};
30 use rustc_data_structures::sync::{Lrc, Lock};
33 use std::collections::hash_map::Entry;
35 use syntax::codemap::DUMMY_SP;
37 pub struct QueryCache<'tcx, D: QueryConfig<'tcx> + ?Sized> {
38 pub(super) results: FxHashMap<D::Key, QueryValue<D::Value>>,
39 pub(super) active: FxHashMap<D::Key, QueryResult<'tcx>>,
42 pub(super) struct QueryValue<T> {
44 pub(super) index: DepNodeIndex,
47 impl<T> QueryValue<T> {
48 pub(super) fn new(value: T,
49 dep_node_index: DepNodeIndex)
53 index: dep_node_index,
58 impl<'tcx, M: QueryConfig<'tcx>> QueryCache<'tcx, M> {
59 pub(super) fn new() -> QueryCache<'tcx, M> {
67 // If enabled, send a message to the profile-queries thread
68 macro_rules! profq_msg {
69 ($tcx:expr, $msg:expr) => {
70 if cfg!(debug_assertions) {
71 if $tcx.sess.profile_queries() {
72 profq_msg($tcx.sess, $msg)
78 // If enabled, format a key using its debug string, which can be
79 // expensive to compute (in terms of time).
80 macro_rules! profq_query_msg {
81 ($query:expr, $tcx:expr, $key:expr) => {{
82 let msg = if cfg!(debug_assertions) {
83 if $tcx.sess.profile_queries_and_keys() {
84 Some(format!("{:?}", $key))
94 /// A type representing the responsibility to execute the job in the `job` field.
95 /// This will poison the relevant query if dropped.
96 pub(super) struct JobOwner<'a, 'tcx: 'a, Q: QueryDescription<'tcx> + 'a> {
97 cache: &'a Lock<QueryCache<'tcx, Q>>,
99 job: Lrc<QueryJob<'tcx>>,
102 impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
103 /// Either gets a JobOwner corresponding the the query, allowing us to
104 /// start executing the query, or it returns with the result of the query.
105 /// If the query is executing elsewhere, this will wait for it.
106 /// If the query panicked, this will silently panic.
108 /// This function is inlined because that results in a noticeable speedup
109 /// for some compile-time benchmarks.
111 pub(super) fn try_get(
112 tcx: TyCtxt<'a, 'tcx, '_>,
115 ) -> TryGetJob<'a, 'tcx, Q> {
116 let cache = Q::query_cache(tcx);
118 let mut lock = cache.borrow_mut();
119 if let Some(value) = lock.results.get(key) {
120 profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
121 let result = Ok((value.value.clone(), value.index));
122 return TryGetJob::JobCompleted(result);
124 let job = match lock.active.entry((*key).clone()) {
125 Entry::Occupied(entry) => {
127 QueryResult::Started(ref job) => job.clone(),
128 QueryResult::Poisoned => FatalError.raise(),
131 Entry::Vacant(entry) => {
132 // No job entry for this query. Return a new one to be started later
133 return tls::with_related_context(tcx, |icx| {
134 let info = QueryInfo {
136 query: Q::query(key.clone()),
138 let job = Lrc::new(QueryJob::new(info, icx.query.clone()));
139 let owner = JobOwner {
144 entry.insert(QueryResult::Started(job));
145 TryGetJob::NotYetStarted(owner)
151 if let Err(cycle) = job.await(tcx, span) {
152 return TryGetJob::JobCompleted(Err(cycle));
157 /// Completes the query by updating the query cache with the `result`,
158 /// signals the waiter and forgets the JobOwner, so it won't poison the query
159 pub(super) fn complete(self, result: &Q::Value, dep_node_index: DepNodeIndex) {
160 // We can move out of `self` here because we `mem::forget` it below
161 let key = unsafe { ptr::read(&self.key) };
162 let job = unsafe { ptr::read(&self.job) };
163 let cache = self.cache;
165 // Forget ourself so our destructor won't poison the query
168 let value = QueryValue::new(result.clone(), dep_node_index);
170 let mut lock = cache.borrow_mut();
171 lock.active.remove(&key);
172 lock.results.insert(key, value);
175 job.signal_complete();
178 /// Executes a job by changing the ImplicitCtxt to point to the
179 /// new query job while it executes. It returns the diagnostics
180 /// captured during execution and the actual result.
181 pub(super) fn start<'lcx, F, R>(
183 tcx: TyCtxt<'_, 'tcx, 'lcx>,
185 -> (R, Vec<Diagnostic>)
187 F: for<'b> FnOnce(TyCtxt<'b, 'tcx, 'lcx>) -> R
189 // The TyCtxt stored in TLS has the same global interner lifetime
190 // as `tcx`, so we use `with_related_context` to relate the 'gcx lifetimes
191 // when accessing the ImplicitCtxt
192 let r = tls::with_related_context(tcx, move |current_icx| {
193 // Update the ImplicitCtxt to point to our new query job
194 let new_icx = tls::ImplicitCtxt {
196 query: Some(self.job.clone()),
197 layout_depth: current_icx.layout_depth,
198 task: current_icx.task,
201 // Use the ImplicitCtxt while we execute the query
202 tls::enter_context(&new_icx, |_| {
207 // Extract the diagnostic from the job
208 let diagnostics = mem::replace(&mut *self.job.diagnostics.lock(), Vec::new());
214 impl<'a, 'tcx, Q: QueryDescription<'tcx>> Drop for JobOwner<'a, 'tcx, Q> {
216 // Poison the query so jobs waiting on it panic
217 self.cache.borrow_mut().active.insert(self.key.clone(), QueryResult::Poisoned);
218 // Also signal the completion of the job, so waiters
219 // will continue execution
220 self.job.signal_complete();
225 pub struct CycleError<'tcx> {
226 /// The query and related span which uses the cycle
227 pub(super) usage: Option<(Span, Query<'tcx>)>,
228 pub(super) cycle: Vec<QueryInfo<'tcx>>,
231 /// The result of `try_get_lock`
232 pub(super) enum TryGetJob<'a, 'tcx: 'a, D: QueryDescription<'tcx> + 'a> {
233 /// The query is not yet started. Contains a guard to the cache eventually used to start it.
234 NotYetStarted(JobOwner<'a, 'tcx, D>),
236 /// The query was already completed.
237 /// Returns the result of the query and its dep node index
238 /// if it succeeded or a cycle error if it failed
239 JobCompleted(Result<(D::Value, DepNodeIndex), CycleError<'tcx>>),
242 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
243 pub(super) fn report_cycle(self, CycleError { usage, cycle: stack }: CycleError<'gcx>)
244 -> DiagnosticBuilder<'a>
246 assert!(!stack.is_empty());
248 let fix_span = |span: Span, query: &Query<'gcx>| {
249 self.sess.codemap().def_span(query.default_span(self, span))
252 // Disable naming impls with types in this path, since that
253 // sometimes cycles itself, leading to extra cycle errors.
254 // (And cycle errors around impls tend to occur during the
255 // collect/coherence phases anyhow.)
256 item_path::with_forced_impl_filename_line(|| {
257 let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
258 let mut err = struct_span_err!(self.sess,
261 "cycle detected when {}",
262 stack[0].query.describe(self));
264 for i in 1..stack.len() {
265 let query = &stack[i].query;
266 let span = fix_span(stack[(i + 1) % stack.len()].span, query);
267 err.span_note(span, &format!("...which requires {}...", query.describe(self)));
270 err.note(&format!("...which again requires {}, completing the cycle",
271 stack[0].query.describe(self)));
273 if let Some((span, query)) = usage {
274 err.span_note(fix_span(span, &query),
275 &format!("cycle used when {}", query.describe(self)));
282 pub fn try_print_query_stack() {
283 eprintln!("query stack during panic:");
285 tls::with_context_opt(|icx| {
286 if let Some(icx) = icx {
287 let mut current_query = icx.query.clone();
290 while let Some(query) = current_query {
291 let mut db = DiagnosticBuilder::new(icx.tcx.sess.diagnostic(),
293 &format!("#{} [{}] {}",
295 query.info.query.name(),
296 query.info.query.describe(icx.tcx)));
297 db.set_span(icx.tcx.sess.codemap().def_span(query.info.span));
298 icx.tcx.sess.diagnostic().force_print_db(db);
300 current_query = query.parent.clone();
306 eprintln!("end of query stack");
309 /// Try to read a node index for the node dep_node.
310 /// A node will have an index, when it's already been marked green, or when we can mark it
311 /// green. This function will mark the current task as a reader of the specified node, when
312 /// the a node index can be found for that node.
313 pub(super) fn try_mark_green_and_read(self, dep_node: &DepNode) -> Option<DepNodeIndex> {
314 match self.dep_graph.node_color(dep_node) {
315 Some(DepNodeColor::Green(dep_node_index)) => {
316 self.dep_graph.read_index(dep_node_index);
319 Some(DepNodeColor::Red) => {
323 // try_mark_green (called below) will panic when full incremental
324 // compilation is disabled. If that's the case, we can't try to mark nodes
325 // as green anyway, so we can safely return None here.
326 if !self.dep_graph.is_fully_enabled() {
329 match self.dep_graph.try_mark_green(self.global_tcx(), &dep_node) {
330 Some(dep_node_index) => {
331 debug_assert!(self.dep_graph.is_green(&dep_node));
332 self.dep_graph.read_index(dep_node_index);
343 fn try_get_with<Q: QueryDescription<'gcx>>(
347 -> Result<Q::Value, CycleError<'gcx>>
349 debug!("ty::queries::{}::try_get_with(key={:?}, span={:?})",
355 ProfileQueriesMsg::QueryBegin(
357 profq_query_msg!(Q::NAME, self, key),
361 let job = match JobOwner::try_get(self, span, &key) {
362 TryGetJob::NotYetStarted(job) => job,
363 TryGetJob::JobCompleted(result) => {
364 return result.map(|(v, index)| {
365 self.dep_graph.read_index(index);
371 // Fast path for when incr. comp. is off. `to_dep_node` is
372 // expensive for some DepKinds.
373 if !self.dep_graph.is_fully_enabled() {
374 let null_dep_node = DepNode::new_no_params(::dep_graph::DepKind::Null);
375 return self.force_query_with_job::<Q>(key, job, null_dep_node).map(|(v, _)| v);
378 let dep_node = Q::to_dep_node(self, &key);
380 if dep_node.kind.is_anon() {
381 profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
383 let res = job.start(self, |tcx| {
384 tcx.dep_graph.with_anon_task(dep_node.kind, || {
385 Q::compute(tcx.global_tcx(), key)
389 profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
390 let ((result, dep_node_index), diagnostics) = res;
392 self.dep_graph.read_index(dep_node_index);
394 self.queries.on_disk_cache
395 .store_diagnostics_for_anon_node(dep_node_index, diagnostics);
397 job.complete(&result, dep_node_index);
402 if !dep_node.kind.is_input() {
403 if let Some(dep_node_index) = self.try_mark_green_and_read(&dep_node) {
404 profq_msg!(self, ProfileQueriesMsg::CacheHit);
405 return self.load_from_disk_and_cache_in_memory::<Q>(key,
412 match self.force_query_with_job::<Q>(key, job, dep_node) {
413 Ok((result, dep_node_index)) => {
414 self.dep_graph.read_index(dep_node_index);
421 fn load_from_disk_and_cache_in_memory<Q: QueryDescription<'gcx>>(
424 job: JobOwner<'a, 'gcx, Q>,
425 dep_node_index: DepNodeIndex,
427 ) -> Result<Q::Value, CycleError<'gcx>>
429 // Note this function can be called concurrently from the same query
430 // We must ensure that this is handled correctly
432 debug_assert!(self.dep_graph.is_green(dep_node));
434 // First we try to load the result from the on-disk cache
435 let result = if Q::cache_on_disk(key.clone()) &&
436 self.sess.opts.debugging_opts.incremental_queries {
437 let prev_dep_node_index =
438 self.dep_graph.prev_dep_node_index_of(dep_node);
439 let result = Q::try_load_from_disk(self.global_tcx(),
440 prev_dep_node_index);
442 // We always expect to find a cached result for things that
443 // can be forced from DepNode.
444 debug_assert!(!dep_node.kind.can_reconstruct_query_key() ||
446 "Missing on-disk cache entry for {:?}",
450 // Some things are never cached on disk.
454 let result = if let Some(result) = result {
457 // We could not load a result from the on-disk cache, so
460 // The diagnostics for this query have already been
461 // promoted to the current session during
462 // try_mark_green(), so we can ignore them here.
463 let (result, _) = job.start(self, |tcx| {
464 // The dep-graph for this computation is already in
466 tcx.dep_graph.with_ignore(|| {
473 // If -Zincremental-verify-ich is specified, re-hash results from
474 // the cache and make sure that they have the expected fingerprint.
475 if self.sess.opts.debugging_opts.incremental_verify_ich {
476 use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
477 use ich::Fingerprint;
479 assert!(Some(self.dep_graph.fingerprint_of(dep_node_index)) ==
480 self.dep_graph.prev_fingerprint_of(dep_node),
481 "Fingerprint for green query instance not loaded \
482 from cache: {:?}", dep_node);
484 debug!("BEGIN verify_ich({:?})", dep_node);
485 let mut hcx = self.create_stable_hashing_context();
486 let mut hasher = StableHasher::new();
488 result.hash_stable(&mut hcx, &mut hasher);
490 let new_hash: Fingerprint = hasher.finish();
491 debug!("END verify_ich({:?})", dep_node);
493 let old_hash = self.dep_graph.fingerprint_of(dep_node_index);
495 assert!(new_hash == old_hash, "Found unstable fingerprints \
496 for {:?}", dep_node);
499 if self.sess.opts.debugging_opts.query_dep_graph {
500 self.dep_graph.mark_loaded_from_cache(dep_node_index, true);
503 job.complete(&result, dep_node_index);
508 fn force_query_with_job<Q: QueryDescription<'gcx>>(
511 job: JobOwner<'_, 'gcx, Q>,
513 -> Result<(Q::Value, DepNodeIndex), CycleError<'gcx>> {
514 // If the following assertion triggers, it can have two reasons:
515 // 1. Something is wrong with DepNode creation, either here or
516 // in DepGraph::try_mark_green()
517 // 2. Two distinct query keys get mapped to the same DepNode
518 // (see for example #48923)
519 assert!(!self.dep_graph.dep_node_exists(&dep_node),
520 "Forcing query with already existing DepNode.\n\
525 profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
526 let res = job.start(self, |tcx| {
527 if dep_node.kind.is_eval_always() {
528 tcx.dep_graph.with_eval_always_task(dep_node,
533 tcx.dep_graph.with_task(dep_node,
539 profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
541 let ((result, dep_node_index), diagnostics) = res;
543 if self.sess.opts.debugging_opts.query_dep_graph {
544 self.dep_graph.mark_loaded_from_cache(dep_node_index, false);
547 if dep_node.kind != ::dep_graph::DepKind::Null {
548 self.queries.on_disk_cache
549 .store_diagnostics(dep_node_index, diagnostics);
552 job.complete(&result, dep_node_index);
554 Ok((result, dep_node_index))
557 /// Ensure that either this query has all green inputs or been executed.
558 /// Executing query::ensure(D) is considered a read of the dep-node D.
560 /// This function is particularly useful when executing passes for their
561 /// side-effects -- e.g., in order to report errors for erroneous programs.
563 /// Note: The optimization is only available during incr. comp.
564 pub(super) fn ensure_query<Q: QueryDescription<'gcx>>(self, key: Q::Key) -> () {
565 let dep_node = Q::to_dep_node(self, &key);
567 // Ensuring an "input" or anonymous query makes no sense
568 assert!(!dep_node.kind.is_anon());
569 assert!(!dep_node.kind.is_input());
570 if self.try_mark_green_and_read(&dep_node).is_none() {
571 // A None return from `try_mark_green_and_read` means that this is either
572 // a new dep node or that the dep node has already been marked red.
573 // Either way, we can't call `dep_graph.read()` as we don't have the
574 // DepNodeIndex. We must invoke the query itself. The performance cost
575 // this introduces should be negligible as we'll immediately hit the
576 // in-memory cache, or another query down the line will.
577 let _ = self.get_query::<Q>(DUMMY_SP, key);
582 fn force_query<Q: QueryDescription<'gcx>>(
587 ) -> Result<(Q::Value, DepNodeIndex), CycleError<'gcx>> {
588 // We may be concurrently trying both execute and force a query
589 // Ensure that only one of them runs the query
590 let job = match JobOwner::try_get(self, span, &key) {
591 TryGetJob::NotYetStarted(job) => job,
592 TryGetJob::JobCompleted(result) => return result,
594 self.force_query_with_job::<Q>(key, job, dep_node)
597 pub(super) fn try_get_query<Q: QueryDescription<'gcx>>(
601 ) -> Result<Q::Value, DiagnosticBuilder<'a>> {
602 match self.try_get_with::<Q>(span, key) {
604 Err(e) => Err(self.report_cycle(e)),
608 pub(super) fn get_query<Q: QueryDescription<'gcx>>(
613 self.try_get_query::<Q>(span, key).unwrap_or_else(|mut e| {
615 Q::handle_cycle_error(self)
620 macro_rules! handle_cycle_error {
621 ([][$this: expr]) => {{
622 Value::from_cycle_error($this.global_tcx())
624 ([fatal_cycle$(, $modifiers:ident)*][$this:expr]) => {{
625 $this.sess.abort_if_errors();
628 ([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
629 handle_cycle_error!([$($modifiers),*][$($args)*])
633 macro_rules! define_queries {
634 (<$tcx:tt> $($category:tt {
635 $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*
637 define_queries_inner! { <$tcx>
638 $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($K) -> $V,)*)*
643 macro_rules! define_queries_inner {
645 $($(#[$attr:meta])* category<$category:tt>
646 [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
649 #[cfg(parallel_queries)]
650 use ty::query::job::QueryResult;
651 use rustc_data_structures::sync::Lock;
653 rustc_data_structures::stable_hasher::HashStable,
654 rustc_data_structures::stable_hasher::StableHasherResult,
655 rustc_data_structures::stable_hasher::StableHasher,
656 ich::StableHashingContext
659 define_queries_struct! {
661 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
664 impl<$tcx> Queries<$tcx> {
666 providers: IndexVec<CrateNum, Providers<$tcx>>,
667 on_disk_cache: OnDiskCache<'tcx>,
672 $($name: Lock::new(QueryCache::new())),*
676 #[cfg(parallel_queries)]
677 pub fn collect_active_jobs(&self) -> Vec<Lrc<QueryJob<$tcx>>> {
678 let mut jobs = Vec::new();
680 // We use try_lock here since we are only called from the
681 // deadlock handler, and this shouldn't be locked
682 $(for v in self.$name.try_lock().unwrap().active.values() {
684 QueryResult::Started(ref job) => jobs.push(job.clone()),
694 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
695 pub enum Query<$tcx> {
696 $($(#[$attr])* $name($K)),*
699 impl<$tcx> Query<$tcx> {
700 pub fn name(&self) -> &'static str {
702 $(Query::$name(_) => stringify!($name),)*
706 pub fn describe(&self, tcx: TyCtxt) -> String {
707 let (r, name) = match *self {
708 $(Query::$name(key) => {
709 (queries::$name::describe(tcx, key), stringify!($name))
712 if tcx.sess.verbose() {
713 format!("{} [{}]", r, name)
719 // FIXME(eddyb) Get more valid Span's on queries.
720 pub fn default_span(&self, tcx: TyCtxt<'_, $tcx, '_>, span: Span) -> Span {
721 if !span.is_dummy() {
724 // The def_span query is used to calculate default_span,
725 // so exit to avoid infinite recursion
727 Query::def_span(..) => return span,
731 $(Query::$name(key) => key.default_span(tcx),)*
736 impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
737 fn hash_stable<W: StableHasherResult>(&self,
738 hcx: &mut StableHashingContext<'a>,
739 hasher: &mut StableHasher<W>) {
740 mem::discriminant(self).hash_stable(hcx, hasher);
742 $(Query::$name(key) => key.hash_stable(hcx, hasher),)*
748 use std::marker::PhantomData;
750 $(#[allow(bad_style)]
751 pub struct $name<$tcx> {
752 data: PhantomData<&$tcx ()>
756 // This module and the functions in it exist only to provide a
757 // predictable symbol name prefix for query providers. This is helpful
758 // for analyzing queries in profilers.
759 pub(super) mod __query_compute {
761 pub fn $name<F: FnOnce() -> R, R>(f: F) -> R {
766 $(impl<$tcx> QueryConfig<$tcx> for queries::$name<$tcx> {
770 const NAME: &'static str = stringify!($name);
773 impl<$tcx> QueryAccessors<$tcx> for queries::$name<$tcx> {
774 fn query(key: Self::Key) -> Query<'tcx> {
778 fn query_cache<'a>(tcx: TyCtxt<'a, $tcx, '_>) -> &'a Lock<QueryCache<$tcx, Self>> {
783 fn to_dep_node(tcx: TyCtxt<'_, $tcx, '_>, key: &Self::Key) -> DepNode {
784 use dep_graph::DepConstructor::*;
786 DepNode::new(tcx, $node(*key))
790 fn compute(tcx: TyCtxt<'_, 'tcx, '_>, key: Self::Key) -> Self::Value {
791 __query_compute::$name(move || {
792 let provider = tcx.queries.providers[key.query_crate()].$name;
793 provider(tcx.global_tcx(), key)
797 fn handle_cycle_error(tcx: TyCtxt<'_, 'tcx, '_>) -> Self::Value {
798 handle_cycle_error!([$($modifiers)*][tcx])
802 impl<'a, $tcx, 'lcx> queries::$name<$tcx> {
803 /// Ensure that either this query has all green inputs or been executed.
804 /// Executing query::ensure(D) is considered a read of the dep-node D.
806 /// This function is particularly useful when executing passes for their
807 /// side-effects -- e.g., in order to report errors for erroneous programs.
809 /// Note: The optimization is only available during incr. comp.
810 pub fn ensure(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> () {
811 tcx.ensure_query::<queries::$name>(key);
815 #[derive(Copy, Clone)]
816 pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
817 pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
821 impl<'a, 'gcx, 'tcx> Deref for TyCtxtAt<'a, 'gcx, 'tcx> {
822 type Target = TyCtxt<'a, 'gcx, 'tcx>;
823 fn deref(&self) -> &Self::Target {
828 impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
829 /// Return a transparent wrapper for `TyCtxt` which uses
830 /// `span` as the location of queries performed through it.
831 pub fn at(self, span: Span) -> TyCtxtAt<'a, $tcx, 'lcx> {
839 pub fn $name(self, key: $K) -> $V {
840 self.at(DUMMY_SP).$name(key)
844 impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
846 pub fn $name(self, key: $K) -> $V {
847 self.tcx.get_query::<queries::$name>(self.span, key)
851 define_provider_struct! {
853 input: ($(([$($modifiers)*] [$name] [$K] [$V]))*)
856 impl<$tcx> Copy for Providers<$tcx> {}
857 impl<$tcx> Clone for Providers<$tcx> {
858 fn clone(&self) -> Self { *self }
863 macro_rules! define_queries_struct {
865 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
866 pub(crate) struct Queries<$tcx> {
867 /// This provides access to the incr. comp. on-disk cache for query results.
868 /// Do not access this directly. It is only meant to be used by
869 /// `DepGraph::try_mark_green()` and the query infrastructure.
870 pub(crate) on_disk_cache: OnDiskCache<'tcx>,
872 providers: IndexVec<CrateNum, Providers<$tcx>>,
874 $($(#[$attr])* $name: Lock<QueryCache<$tcx, queries::$name<$tcx>>>,)*
879 macro_rules! define_provider_struct {
881 input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
882 pub struct Providers<$tcx> {
883 $(pub $name: for<'a> fn(TyCtxt<'a, $tcx, $tcx>, $K) -> $R,)*
886 impl<$tcx> Default for Providers<$tcx> {
887 fn default() -> Self {
888 $(fn $name<'a, $tcx>(_: TyCtxt<'a, $tcx, $tcx>, key: $K) -> $R {
889 bug!("tcx.{}({:?}) unsupported by its crate",
890 stringify!($name), key);
892 Providers { $($name),* }
899 /// The red/green evaluation system will try to mark a specific DepNode in the
900 /// dependency graph as green by recursively trying to mark the dependencies of
901 /// that DepNode as green. While doing so, it will sometimes encounter a DepNode
902 /// where we don't know if it is red or green and we therefore actually have
903 /// to recompute its value in order to find out. Since the only piece of
904 /// information that we have at that point is the DepNode we are trying to
905 /// re-evaluate, we need some way to re-run a query from just that. This is what
906 /// `force_from_dep_node()` implements.
908 /// In the general case, a DepNode consists of a DepKind and an opaque
909 /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
910 /// is usually constructed by computing a stable hash of the query-key that the
911 /// DepNode corresponds to. Consequently, it is not in general possible to go
912 /// back from hash to query-key (since hash functions are not reversible). For
913 /// this reason `force_from_dep_node()` is expected to fail from time to time
914 /// because we just cannot find out, from the DepNode alone, what the
915 /// corresponding query-key is and therefore cannot re-run the query.
917 /// The system deals with this case letting `try_mark_green` fail which forces
918 /// the root query to be re-evaluated.
920 /// Now, if force_from_dep_node() would always fail, it would be pretty useless.
921 /// Fortunately, we can use some contextual information that will allow us to
922 /// reconstruct query-keys for certain kinds of DepNodes. In particular, we
923 /// enforce by construction that the GUID/fingerprint of certain DepNodes is a
924 /// valid DefPathHash. Since we also always build a huge table that maps every
925 /// DefPathHash in the current codebase to the corresponding DefId, we have
926 /// everything we need to re-run the query.
928 /// Take the `mir_validated` query as an example. Like many other queries, it
929 /// just has a single parameter: the DefId of the item it will compute the
930 /// validated MIR for. Now, when we call `force_from_dep_node()` on a dep-node
931 /// with kind `MirValidated`, we know that the GUID/fingerprint of the dep-node
932 /// is actually a DefPathHash, and can therefore just look up the corresponding
933 /// DefId in `tcx.def_path_hash_to_def_id`.
935 /// When you implement a new query, it will likely have a corresponding new
936 /// DepKind, and you'll have to support it here in `force_from_dep_node()`. As
937 /// a rule of thumb, if your query takes a DefId or DefIndex as sole parameter,
938 /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
939 /// add it to the "We don't have enough information to reconstruct..." group in
941 pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
944 use hir::def_id::LOCAL_CRATE;
946 // We must avoid ever having to call force_from_dep_node() for a
947 // DepNode::CodegenUnit:
948 // Since we cannot reconstruct the query key of a DepNode::CodegenUnit, we
949 // would always end up having to evaluate the first caller of the
950 // `codegen_unit` query that *is* reconstructible. This might very well be
951 // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
952 // to re-trigger calling the `codegen_unit` query with the right key. At
953 // that point we would already have re-done all the work we are trying to
954 // avoid doing in the first place.
955 // The solution is simple: Just explicitly call the `codegen_unit` query for
956 // each CGU, right after partitioning. This way `try_mark_green` will always
957 // hit the cache instead of having to go through `force_from_dep_node`.
958 // This assertion makes sure, we actually keep applying the solution above.
959 debug_assert!(dep_node.kind != DepKind::CodegenUnit,
960 "calling force_from_dep_node() on DepKind::CodegenUnit");
962 if !dep_node.kind.can_reconstruct_query_key() {
966 macro_rules! def_id {
968 if let Some(def_id) = dep_node.extract_def_id(tcx) {
971 // return from the whole function
978 () => { (def_id!()).krate }
982 ($query:ident, $key:expr) => {
984 use $crate::util::common::{ProfileQueriesMsg, profq_msg};
987 ProfileQueriesMsg::QueryBegin(
989 profq_query_msg!(::ty::query::queries::$query::NAME, tcx, $key),
993 match tcx.force_query::<::ty::query::queries::$query>($key, DUMMY_SP, *dep_node) {
996 tcx.report_cycle(e).emit();
1003 // FIXME(#45015): We should try move this boilerplate code into a macro
1005 match dep_node.kind {
1006 // These are inputs that are expected to be pre-allocated and that
1007 // should therefore always be red or green already
1008 DepKind::AllLocalTraitImpls |
1010 DepKind::CrateMetadata |
1014 // This are anonymous nodes
1015 DepKind::TraitSelect |
1017 // We don't have enough information to reconstruct the query key of
1022 DepKind::NeedsDrop |
1024 DepKind::ConstEval |
1025 DepKind::InstanceSymbolName |
1027 DepKind::BorrowCheckKrate |
1028 DepKind::Specializes |
1029 DepKind::ImplementationsOfTrait |
1030 DepKind::TypeParamPredicates |
1031 DepKind::CodegenUnit |
1032 DepKind::CompileCodegenUnit |
1033 DepKind::FulfillObligation |
1034 DepKind::VtableMethods |
1035 DepKind::EraseRegionsTy |
1036 DepKind::ConstValueToAllocation |
1037 DepKind::NormalizeProjectionTy |
1038 DepKind::NormalizeTyAfterErasingRegions |
1039 DepKind::ImpliedOutlivesBounds |
1040 DepKind::DropckOutlives |
1041 DepKind::EvaluateObligation |
1043 DepKind::TypeOpSubtype |
1044 DepKind::TypeOpProvePredicate |
1045 DepKind::TypeOpNormalizeTy |
1046 DepKind::TypeOpNormalizePredicate |
1047 DepKind::TypeOpNormalizePolyFnSig |
1048 DepKind::TypeOpNormalizeFnSig |
1049 DepKind::SubstituteNormalizeAndTestPredicates |
1050 DepKind::InstanceDefSizeEstimate |
1051 DepKind::ProgramClausesForEnv |
1053 // This one should never occur in this context
1055 bug!("force_from_dep_node() - Encountered {:?}", dep_node)
1058 // These are not queries
1059 DepKind::CoherenceCheckTrait |
1060 DepKind::ItemVarianceConstraints => {
1064 DepKind::RegionScopeTree => { force!(region_scope_tree, def_id!()); }
1066 DepKind::Coherence => { force!(crate_inherent_impls, LOCAL_CRATE); }
1067 DepKind::CoherenceInherentImplOverlapCheck => {
1068 force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
1070 DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
1071 DepKind::MirBuilt => { force!(mir_built, def_id!()); }
1072 DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
1073 DepKind::MirConst => { force!(mir_const, def_id!()); }
1074 DepKind::MirValidated => { force!(mir_validated, def_id!()); }
1075 DepKind::MirOptimized => { force!(optimized_mir, def_id!()); }
1077 DepKind::BorrowCheck => { force!(borrowck, def_id!()); }
1078 DepKind::MirBorrowCheck => { force!(mir_borrowck, def_id!()); }
1079 DepKind::UnsafetyCheckResult => { force!(unsafety_check_result, def_id!()); }
1080 DepKind::UnsafeDeriveOnReprPacked => { force!(unsafe_derive_on_repr_packed, def_id!()); }
1081 DepKind::Reachability => { force!(reachable_set, LOCAL_CRATE); }
1082 DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
1083 DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
1084 DepKind::AssociatedItems => { force!(associated_item, def_id!()); }
1085 DepKind::TypeOfItem => { force!(type_of, def_id!()); }
1086 DepKind::GenericsOfItem => { force!(generics_of, def_id!()); }
1087 DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); }
1088 DepKind::PredicatesDefinedOnItem => { force!(predicates_defined_on, def_id!()); }
1089 DepKind::ExplicitPredicatesOfItem => { force!(explicit_predicates_of, def_id!()); }
1090 DepKind::InferredOutlivesOf => { force!(inferred_outlives_of, def_id!()); }
1091 DepKind::InferredOutlivesCrate => { force!(inferred_outlives_crate, LOCAL_CRATE); }
1092 DepKind::SuperPredicatesOfItem => { force!(super_predicates_of, def_id!()); }
1093 DepKind::TraitDefOfItem => { force!(trait_def, def_id!()); }
1094 DepKind::AdtDefOfItem => { force!(adt_def, def_id!()); }
1095 DepKind::ImplTraitRef => { force!(impl_trait_ref, def_id!()); }
1096 DepKind::ImplPolarity => { force!(impl_polarity, def_id!()); }
1097 DepKind::FnSignature => { force!(fn_sig, def_id!()); }
1098 DepKind::CoerceUnsizedInfo => { force!(coerce_unsized_info, def_id!()); }
1099 DepKind::ItemVariances => { force!(variances_of, def_id!()); }
1100 DepKind::IsConstFn => { force!(is_const_fn, def_id!()); }
1101 DepKind::IsForeignItem => { force!(is_foreign_item, def_id!()); }
1102 DepKind::SizedConstraint => { force!(adt_sized_constraint, def_id!()); }
1103 DepKind::DtorckConstraint => { force!(adt_dtorck_constraint, def_id!()); }
1104 DepKind::AdtDestructor => { force!(adt_destructor, def_id!()); }
1105 DepKind::AssociatedItemDefIds => { force!(associated_item_def_ids, def_id!()); }
1106 DepKind::InherentImpls => { force!(inherent_impls, def_id!()); }
1107 DepKind::TypeckBodiesKrate => { force!(typeck_item_bodies, LOCAL_CRATE); }
1108 DepKind::TypeckTables => { force!(typeck_tables_of, def_id!()); }
1109 DepKind::UsedTraitImports => { force!(used_trait_imports, def_id!()); }
1110 DepKind::HasTypeckTables => { force!(has_typeck_tables, def_id!()); }
1111 DepKind::SymbolName => { force!(def_symbol_name, def_id!()); }
1112 DepKind::SpecializationGraph => { force!(specialization_graph_of, def_id!()); }
1113 DepKind::ObjectSafety => { force!(is_object_safe, def_id!()); }
1114 DepKind::TraitImpls => { force!(trait_impls_of, def_id!()); }
1115 DepKind::CheckMatch => { force!(check_match, def_id!()); }
1117 DepKind::ParamEnv => { force!(param_env, def_id!()); }
1118 DepKind::DescribeDef => { force!(describe_def, def_id!()); }
1119 DepKind::DefSpan => { force!(def_span, def_id!()); }
1120 DepKind::LookupStability => { force!(lookup_stability, def_id!()); }
1121 DepKind::LookupDeprecationEntry => {
1122 force!(lookup_deprecation_entry, def_id!());
1124 DepKind::ConstIsRvaluePromotableToStatic => {
1125 force!(const_is_rvalue_promotable_to_static, def_id!());
1127 DepKind::RvaluePromotableMap => { force!(rvalue_promotable_map, def_id!()); }
1128 DepKind::ImplParent => { force!(impl_parent, def_id!()); }
1129 DepKind::TraitOfItem => { force!(trait_of_item, def_id!()); }
1130 DepKind::IsReachableNonGeneric => { force!(is_reachable_non_generic, def_id!()); }
1131 DepKind::IsUnreachableLocalDefinition => {
1132 force!(is_unreachable_local_definition, def_id!());
1134 DepKind::IsMirAvailable => { force!(is_mir_available, def_id!()); }
1135 DepKind::ItemAttrs => { force!(item_attrs, def_id!()); }
1136 DepKind::CodegenFnAttrs => { force!(codegen_fn_attrs, def_id!()); }
1137 DepKind::FnArgNames => { force!(fn_arg_names, def_id!()); }
1138 DepKind::RenderedConst => { force!(rendered_const, def_id!()); }
1139 DepKind::DylibDepFormats => { force!(dylib_dependency_formats, krate!()); }
1140 DepKind::IsPanicRuntime => { force!(is_panic_runtime, krate!()); }
1141 DepKind::IsCompilerBuiltins => { force!(is_compiler_builtins, krate!()); }
1142 DepKind::HasGlobalAllocator => { force!(has_global_allocator, krate!()); }
1143 DepKind::ExternCrate => { force!(extern_crate, def_id!()); }
1144 DepKind::LintLevels => { force!(lint_levels, LOCAL_CRATE); }
1145 DepKind::InScopeTraits => { force!(in_scope_traits_map, def_id!().index); }
1146 DepKind::ModuleExports => { force!(module_exports, def_id!()); }
1147 DepKind::IsSanitizerRuntime => { force!(is_sanitizer_runtime, krate!()); }
1148 DepKind::IsProfilerRuntime => { force!(is_profiler_runtime, krate!()); }
1149 DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
1150 DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
1151 DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); }
1152 DepKind::CheckItemWellFormed => { force!(check_item_well_formed, def_id!()); }
1153 DepKind::CheckTraitItemWellFormed => { force!(check_trait_item_well_formed, def_id!()); }
1154 DepKind::CheckImplItemWellFormed => { force!(check_impl_item_well_formed, def_id!()); }
1155 DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
1156 DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
1157 DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
1158 DepKind::DeriveRegistrarFn => { force!(derive_registrar_fn, krate!()); }
1159 DepKind::CrateDisambiguator => { force!(crate_disambiguator, krate!()); }
1160 DepKind::CrateHash => { force!(crate_hash, krate!()); }
1161 DepKind::OriginalCrateName => { force!(original_crate_name, krate!()); }
1162 DepKind::ExtraFileName => { force!(extra_filename, krate!()); }
1164 DepKind::AllTraitImplementations => {
1165 force!(all_trait_implementations, krate!());
1168 DepKind::DllimportForeignItems => {
1169 force!(dllimport_foreign_items, krate!());
1171 DepKind::IsDllimportForeignItem => {
1172 force!(is_dllimport_foreign_item, def_id!());
1174 DepKind::IsStaticallyIncludedForeignItem => {
1175 force!(is_statically_included_foreign_item, def_id!());
1177 DepKind::NativeLibraryKind => { force!(native_library_kind, def_id!()); }
1178 DepKind::LinkArgs => { force!(link_args, LOCAL_CRATE); }
1180 DepKind::ResolveLifetimes => { force!(resolve_lifetimes, krate!()); }
1181 DepKind::NamedRegion => { force!(named_region_map, def_id!().index); }
1182 DepKind::IsLateBound => { force!(is_late_bound_map, def_id!().index); }
1183 DepKind::ObjectLifetimeDefaults => {
1184 force!(object_lifetime_defaults_map, def_id!().index);
1187 DepKind::Visibility => { force!(visibility, def_id!()); }
1188 DepKind::DepKind => { force!(dep_kind, krate!()); }
1189 DepKind::CrateName => { force!(crate_name, krate!()); }
1190 DepKind::ItemChildren => { force!(item_children, def_id!()); }
1191 DepKind::ExternModStmtCnum => { force!(extern_mod_stmt_cnum, def_id!()); }
1192 DepKind::GetLangItems => { force!(get_lang_items, LOCAL_CRATE); }
1193 DepKind::DefinedLangItems => { force!(defined_lang_items, krate!()); }
1194 DepKind::MissingLangItems => { force!(missing_lang_items, krate!()); }
1195 DepKind::VisibleParentMap => { force!(visible_parent_map, LOCAL_CRATE); }
1196 DepKind::MissingExternCrateItem => {
1197 force!(missing_extern_crate_item, krate!());
1199 DepKind::UsedCrateSource => { force!(used_crate_source, krate!()); }
1200 DepKind::PostorderCnums => { force!(postorder_cnums, LOCAL_CRATE); }
1202 DepKind::Freevars => { force!(freevars, def_id!()); }
1203 DepKind::MaybeUnusedTraitImport => {
1204 force!(maybe_unused_trait_import, def_id!());
1206 DepKind::MaybeUnusedExternCrates => { force!(maybe_unused_extern_crates, LOCAL_CRATE); }
1207 DepKind::StabilityIndex => { force!(stability_index, LOCAL_CRATE); }
1208 DepKind::AllTraits => { force!(all_traits, LOCAL_CRATE); }
1209 DepKind::AllCrateNums => { force!(all_crate_nums, LOCAL_CRATE); }
1210 DepKind::ExportedSymbols => { force!(exported_symbols, krate!()); }
1211 DepKind::CollectAndPartitionMonoItems => {
1212 force!(collect_and_partition_mono_items, LOCAL_CRATE);
1214 DepKind::IsCodegenedItem => { force!(is_codegened_item, def_id!()); }
1215 DepKind::OutputFilenames => { force!(output_filenames, LOCAL_CRATE); }
1217 DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }
1219 DepKind::Features => { force!(features_query, LOCAL_CRATE); }
1221 DepKind::ProgramClausesFor => { force!(program_clauses_for, def_id!()); }
1222 DepKind::WasmImportModuleMap => { force!(wasm_import_module_map, krate!()); }
1223 DepKind::ForeignModules => { force!(foreign_modules, krate!()); }
1225 DepKind::UpstreamMonomorphizations => {
1226 force!(upstream_monomorphizations, krate!());
1228 DepKind::UpstreamMonomorphizationsFor => {
1229 force!(upstream_monomorphizations_for, def_id!());
1237 // FIXME(#45015): Another piece of boilerplate code that could be generated in
1238 // a combined define_dep_nodes!()/define_queries!() macro.
1239 macro_rules! impl_load_from_cache {
1240 ($($dep_kind:ident => $query_name:ident,)*) => {
1242 // Check whether the query invocation corresponding to the given
1243 // DepNode is eligible for on-disk-caching.
1244 pub fn cache_on_disk(&self, tcx: TyCtxt) -> bool {
1245 use ty::query::queries;
1246 use ty::query::QueryDescription;
1249 $(DepKind::$dep_kind => {
1250 let def_id = self.extract_def_id(tcx).unwrap();
1251 queries::$query_name::cache_on_disk(def_id)
1257 // This is method will execute the query corresponding to the given
1258 // DepNode. It is only expected to work for DepNodes where the
1259 // above `cache_on_disk` methods returns true.
1260 // Also, as a sanity check, it expects that the corresponding query
1261 // invocation has been marked as green already.
1262 pub fn load_from_on_disk_cache(&self, tcx: TyCtxt) {
1264 $(DepKind::$dep_kind => {
1265 debug_assert!(tcx.dep_graph
1267 .map(|c| c.is_green())
1270 let def_id = self.extract_def_id(tcx).unwrap();
1271 let _ = tcx.$query_name(def_id);
1282 impl_load_from_cache!(
1283 TypeckTables => typeck_tables_of,
1284 MirOptimized => optimized_mir,
1285 UnsafetyCheckResult => unsafety_check_result,
1286 BorrowCheck => borrowck,
1287 MirBorrowCheck => mir_borrowck,
1288 MirConstQualif => mir_const_qualif,
1289 SymbolName => def_symbol_name,
1290 ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
1291 CheckMatch => check_match,
1292 TypeOfItem => type_of,
1293 GenericsOfItem => generics_of,
1294 PredicatesOfItem => predicates_of,
1295 UsedTraitImports => used_trait_imports,
1296 CodegenFnAttrs => codegen_fn_attrs,
1297 SpecializationGraph => specialization_graph_of,