Skip to main content

rustc_middle/dep_graph/
mod.rs

1use std::panic;
2
3use tracing::instrument;
4
5pub use self::dep_node::{
6    DepKind, DepKindVTable, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
7    label_strs,
8};
9pub use self::graph::{
10    DepGraph, DepGraphData, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result,
11};
12use self::graph::{MarkFrame, print_markframe_trace};
13pub use self::query::DepGraphQuery;
14pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
15pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
16use crate::ty::print::with_reduced_queries;
17use crate::ty::{self, TyCtxt};
18
19mod debug;
20pub mod dep_node;
21mod dep_node_key;
22mod edges;
23mod graph;
24mod query;
25mod serialized;
26
27pub trait HasDepContext<'tcx>: Copy {
28    fn dep_context(&self) -> TyCtxt<'tcx>;
29}
30
31impl<'tcx> HasDepContext<'tcx> for TyCtxt<'tcx> {
32    fn dep_context(&self) -> TyCtxt<'tcx> {
33        *self
34    }
35}
36
37impl<'tcx, T: HasDepContext<'tcx>, Q: Copy> HasDepContext<'tcx> for (T, Q) {
38    fn dep_context(&self) -> TyCtxt<'tcx> {
39        self.0.dep_context()
40    }
41}
42
43/// Describes the contents of the fingerprint generated by a given query.
44///
45/// This is mainly for determining whether and how we can reconstruct a key
46/// from the fingerprint.
47#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FingerprintStyle {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                FingerprintStyle::DefPathHash => "DefPathHash",
                FingerprintStyle::HirId => "HirId",
                FingerprintStyle::Unit => "Unit",
                FingerprintStyle::Opaque => "Opaque",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for FingerprintStyle {
    #[inline]
    fn eq(&self, other: &FingerprintStyle) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for FingerprintStyle {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::marker::Copy for FingerprintStyle { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FingerprintStyle {
    #[inline]
    fn clone(&self) -> FingerprintStyle { *self }
}Clone)]
48pub enum FingerprintStyle {
49    /// The fingerprint is actually a DefPathHash.
50    DefPathHash,
51    /// The fingerprint is actually a HirId.
52    HirId,
53    /// Query key was `()` or equivalent, so fingerprint is just zero.
54    Unit,
55    /// The fingerprint is an opaque hash, and a key cannot be reconstructed from it.
56    Opaque,
57}
58
59impl FingerprintStyle {
60    #[inline]
61    pub const fn reconstructible(self) -> bool {
62        match self {
63            FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
64                true
65            }
66            FingerprintStyle::Opaque => false,
67        }
68    }
69}
70
71/// Execute the operation with provided dependencies.
72fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
73where
74    OP: FnOnce() -> R,
75{
76    ty::tls::with_context(|icx| {
77        let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
78        ty::tls::enter_context(&icx, op)
79    })
80}
81
82/// Access dependencies from current implicit context.
83fn read_deps<OP>(op: OP)
84where
85    OP: for<'a> FnOnce(TaskDepsRef<'a>),
86{
87    ty::tls::with_context_opt(|icx| {
88        let Some(icx) = icx else { return };
89        op(icx.task_deps)
90    })
91}
92
93impl<'tcx> TyCtxt<'tcx> {
94    #[inline]
95    pub fn dep_kind_vtable(self, dk: DepKind) -> &'tcx DepKindVTable<'tcx> {
96        &self.dep_kind_vtables[dk.as_usize()]
97    }
98
99    fn with_reduced_queries<T>(self, f: impl FnOnce() -> T) -> T {
100        { let _guard = ReducedQueriesGuard::new(); f() }with_reduced_queries!(f())
101    }
102
103    #[inline(always)]
104    pub fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle {
105        self.dep_kind_vtable(kind).fingerprint_style
106    }
107
108    /// Try to force a dep node to execute and see if it's green.
109    ///
110    /// Returns true if the query has actually been forced. It is valid that a query
111    /// fails to be forced, e.g. when the query key cannot be reconstructed from the
112    /// dep-node or when the query kind outright does not support it.
113    #[inline]
114    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_force_from_dep_node",
                                    "rustc_middle::dep_graph", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/dep_graph/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(114u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_middle::dep_graph"),
                                    ::tracing_core::field::FieldSet::new(&["dep_node",
                                                    "prev_index"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&dep_node)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&prev_index)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: bool = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if let Some(force_fn) =
                    self.dep_kind_vtable(dep_node.kind).force_from_dep_node {
                match panic::catch_unwind(panic::AssertUnwindSafe(||
                                { force_fn(self, dep_node, prev_index) })) {
                    Err(value) => {
                        if !value.is::<rustc_errors::FatalErrorMarker>() {
                            print_markframe_trace(&self.dep_graph, frame);
                        }
                        panic::resume_unwind(value)
                    }
                    Ok(query_has_been_forced) => query_has_been_forced,
                }
            } else { false }
        }
    }
}#[instrument(skip(self, frame), level = "debug")]
115    fn try_force_from_dep_node(
116        self,
117        dep_node: DepNode,
118        prev_index: SerializedDepNodeIndex,
119        frame: &MarkFrame<'_>,
120    ) -> bool {
121        if let Some(force_fn) = self.dep_kind_vtable(dep_node.kind).force_from_dep_node {
122            match panic::catch_unwind(panic::AssertUnwindSafe(|| {
123                force_fn(self, dep_node, prev_index)
124            })) {
125                Err(value) => {
126                    if !value.is::<rustc_errors::FatalErrorMarker>() {
127                        print_markframe_trace(&self.dep_graph, frame);
128                    }
129                    panic::resume_unwind(value)
130                }
131                Ok(query_has_been_forced) => query_has_been_forced,
132            }
133        } else {
134            false
135        }
136    }
137
138    /// Load data from the on-disk cache.
139    fn try_load_from_on_disk_cache(self, dep_node: &DepNode) {
140        if let Some(try_load_fn) = self.dep_kind_vtable(dep_node.kind).try_load_from_on_disk_cache {
141            try_load_fn(self, *dep_node)
142        }
143    }
144}