|
| 1 | +use std::fmt::Debug; |
| 2 | +use std::marker::PhantomData; |
| 3 | +use std::mem::transmute; |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use rustc_data_structures::sync::{DynSend, DynSync}; |
| 7 | +use rustc_hashes::Hash64; |
| 8 | +use rustc_hir::def::DefKind; |
| 9 | +use rustc_span::Span; |
| 10 | +use rustc_span::def_id::DefId; |
| 11 | + |
| 12 | +use crate::dep_graph::DepKind; |
| 13 | + |
| 14 | +/// Description of a frame in the query stack. |
| 15 | +/// |
| 16 | +/// This is mostly used in case of cycles for error reporting. |
| 17 | +#[derive(Clone, Debug)] |
| 18 | +pub struct QueryStackFrame<I> { |
| 19 | + /// This field initially stores a `QueryStackDeferred` during collection, |
| 20 | + /// but can later be changed to `QueryStackFrameExtra` containing concrete information |
| 21 | + /// by calling `lift`. This is done so that collecting query does not need to invoke |
| 22 | + /// queries, instead `lift` will call queries in a more appropriate location. |
| 23 | + pub info: I, |
| 24 | + |
| 25 | + pub dep_kind: DepKind, |
| 26 | + /// This hash is used to deterministically pick |
| 27 | + /// a query to remove cycles in the parallel compiler. |
| 28 | + pub hash: Hash64, |
| 29 | + pub def_id: Option<DefId>, |
| 30 | + /// A def-id that is extracted from a `Ty` in a query key |
| 31 | + pub def_id_for_ty_in_cycle: Option<DefId>, |
| 32 | +} |
| 33 | + |
| 34 | +impl<'tcx> QueryStackFrame<QueryStackDeferred<'tcx>> { |
| 35 | + #[inline] |
| 36 | + pub fn new( |
| 37 | + info: QueryStackDeferred<'tcx>, |
| 38 | + dep_kind: DepKind, |
| 39 | + hash: Hash64, |
| 40 | + def_id: Option<DefId>, |
| 41 | + def_id_for_ty_in_cycle: Option<DefId>, |
| 42 | + ) -> Self { |
| 43 | + Self { info, def_id, dep_kind, hash, def_id_for_ty_in_cycle } |
| 44 | + } |
| 45 | + |
| 46 | + pub fn lift(&self) -> QueryStackFrame<QueryStackFrameExtra> { |
| 47 | + QueryStackFrame { |
| 48 | + info: self.info.extract(), |
| 49 | + dep_kind: self.dep_kind, |
| 50 | + hash: self.hash, |
| 51 | + def_id: self.def_id, |
| 52 | + def_id_for_ty_in_cycle: self.def_id_for_ty_in_cycle, |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Clone, Debug)] |
| 58 | +pub struct QueryStackFrameExtra { |
| 59 | + pub description: String, |
| 60 | + pub span: Option<Span>, |
| 61 | + pub def_kind: Option<DefKind>, |
| 62 | +} |
| 63 | + |
| 64 | +impl QueryStackFrameExtra { |
| 65 | + #[inline] |
| 66 | + pub fn new(description: String, span: Option<Span>, def_kind: Option<DefKind>) -> Self { |
| 67 | + Self { description, span, def_kind } |
| 68 | + } |
| 69 | + |
| 70 | + // FIXME(eddyb) Get more valid `Span`s on queries. |
| 71 | + #[inline] |
| 72 | + pub fn default_span(&self, span: Span) -> Span { |
| 73 | + if !span.is_dummy() { |
| 74 | + return span; |
| 75 | + } |
| 76 | + self.span.unwrap_or(span) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Track a 'side effect' for a particular query. |
| 81 | +/// This is used to hold a closure which can create `QueryStackFrameExtra`. |
| 82 | +#[derive(Clone)] |
| 83 | +pub struct QueryStackDeferred<'tcx> { |
| 84 | + _dummy: PhantomData<&'tcx ()>, |
| 85 | + |
| 86 | + // `extract` may contain references to 'tcx, but we can't tell drop checking that it won't |
| 87 | + // access it in the destructor. |
| 88 | + extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend>, |
| 89 | +} |
| 90 | + |
| 91 | +impl<'tcx> QueryStackDeferred<'tcx> { |
| 92 | + pub fn new<C: Copy + DynSync + DynSend + 'tcx>( |
| 93 | + context: C, |
| 94 | + extract: fn(C) -> QueryStackFrameExtra, |
| 95 | + ) -> Self { |
| 96 | + let extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx> = |
| 97 | + Arc::new(move || extract(context)); |
| 98 | + // SAFETY: The `extract` closure does not access 'tcx in its destructor as the only |
| 99 | + // captured variable is `context` which is Copy and cannot have a destructor. |
| 100 | + Self { _dummy: PhantomData, extract: unsafe { transmute(extract) } } |
| 101 | + } |
| 102 | + |
| 103 | + pub fn extract(&self) -> QueryStackFrameExtra { |
| 104 | + (self.extract)() |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<'tcx> Debug for QueryStackDeferred<'tcx> { |
| 109 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 110 | + f.write_str("QueryStackDeferred") |
| 111 | + } |
| 112 | +} |
0 commit comments