1 // Copyright 2017 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 use rustc_data_structures::sync::{Lock, Lrc};
15 use ty::maps::plumbing::CycleError;
16 use ty::context::TyCtxt;
17 use errors::Diagnostic;
19 /// Indicates the state of a query for a given key in a query map
20 pub(super) enum QueryResult<'tcx, T> {
21 /// An already executing query. The query job can be used to await for its completion
22 Started(Lrc<QueryJob<'tcx>>),
24 /// The query is complete and produced `T`
27 /// The query panicked. Queries trying to wait on this will raise a fatal error / silently panic
31 /// A span and a query key
32 #[derive(Clone, Debug)]
33 pub struct QueryInfo<'tcx> {
34 /// The span for a reason this query was required
36 pub query: Query<'tcx>,
39 /// A object representing an active query job.
40 pub struct QueryJob<'tcx> {
41 pub info: QueryInfo<'tcx>,
43 /// The parent query job which created this job and is implicitly waiting on it.
44 pub parent: Option<Lrc<QueryJob<'tcx>>>,
46 /// Diagnostic messages which are emitted while the query executes
47 pub diagnostics: Lock<Vec<Diagnostic>>,
50 impl<'tcx> QueryJob<'tcx> {
51 /// Creates a new query job
52 pub fn new(info: QueryInfo<'tcx>, parent: Option<Lrc<QueryJob<'tcx>>>) -> Self {
54 diagnostics: Lock::new(Vec::new()),
60 /// Awaits for the query job to complete.
62 /// For single threaded rustc there's no concurrent jobs running, so if we are waiting for any
63 /// query that means that there is a query cycle, thus this always running a cycle error.
64 pub(super) fn await<'lcx>(
66 tcx: TyCtxt<'_, 'tcx, 'lcx>,
68 ) -> Result<(), CycleError<'tcx>> {
69 // Get the current executing query (waiter) and find the waitee amongst its parents
70 let mut current_job = tls::with_related_context(tcx, |icx| icx.query.clone());
71 let mut cycle = Vec::new();
73 while let Some(job) = current_job {
74 cycle.insert(0, job.info.clone());
76 if &*job as *const _ == self as *const _ {
77 // This is the end of the cycle
78 // The span entry we included was for the usage
79 // of the cycle itself, and not part of the cycle
80 // Replace it with the span which caused the cycle to form
82 // Find out why the cycle itself was used
83 let usage = job.parent.as_ref().map(|parent| {
84 (job.info.span, parent.info.query.clone())
86 return Err(CycleError { usage, cycle });
89 current_job = job.parent.clone();
92 panic!("did not find a cycle")
95 /// Signals to waiters that the query is complete.
97 /// This does nothing for single threaded rustc,
98 /// as there are no concurrent jobs which could be waiting on us
99 pub fn signal_complete(&self) {}