]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/maps/job.rs
374406158c1d51a5695dfd0ae615e4dd686d1e8c
[rust.git] / src / librustc / ty / maps / job.rs
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.
4 //
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.
10
11 use rustc_data_structures::sync::{Lock, Lrc};
12 use syntax_pos::Span;
13 use ty::tls;
14 use ty::maps::Query;
15 use ty::maps::plumbing::CycleError;
16 use ty::context::TyCtxt;
17 use errors::Diagnostic;
18
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>>),
23
24     /// The query is complete and produced `T`
25     Complete(T),
26
27     /// The query panicked. Queries trying to wait on this will raise a fatal error / silently panic
28     Poisoned,
29 }
30
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
35     pub span: Span,
36     pub query: Query<'tcx>,
37 }
38
39 /// A object representing an active query job.
40 pub struct QueryJob<'tcx> {
41     pub info: QueryInfo<'tcx>,
42
43     /// The parent query job which created this job and is implicitly waiting on it.
44     pub parent: Option<Lrc<QueryJob<'tcx>>>,
45
46     /// Diagnostic messages which are emitted while the query executes
47     pub diagnostics: Lock<Vec<Diagnostic>>,
48 }
49
50 impl<'tcx> QueryJob<'tcx> {
51     /// Creates a new query job
52     pub fn new(info: QueryInfo<'tcx>, parent: Option<Lrc<QueryJob<'tcx>>>) -> Self {
53         QueryJob {
54             diagnostics: Lock::new(Vec::new()),
55             info,
56             parent,
57         }
58     }
59
60     /// Awaits for the query job to complete.
61     ///
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>(
65         &self,
66         tcx: TyCtxt<'_, 'tcx, 'lcx>,
67         span: Span,
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();
72
73         while let Some(job) = current_job {
74             cycle.insert(0, job.info.clone());
75
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
81                 cycle[0].span = span;
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())
85                 });
86                 return Err(CycleError { usage, cycle });
87             }
88
89             current_job = job.parent.clone();
90         }
91
92         panic!("did not find a cycle")
93     }
94
95     /// Signals to waiters that the query is complete.
96     ///
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) {}
100 }