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, QuerySideEffect, TaskDepsRef, WorkProduct,
11    WorkProductMap, hash_result,
12};
13use self::graph::{MarkFrame, print_markframe_trace};
14pub use self::query::DepGraphQuery;
15pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
16pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
17use crate::ty::print::with_reduced_queries;
18use crate::ty::{self, TyCtxt};
19
20mod debug;
21pub mod dep_node;
22mod dep_node_key;
23mod edges;
24mod graph;
25mod query;
26mod serialized;
27
28/// Describes the contents of the fingerprint generated by a given query.
29///
30/// This is mainly for determining whether and how we can reconstruct a key
31/// from the fingerprint.
32#[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)]
33pub enum FingerprintStyle {
34    /// The fingerprint is actually a DefPathHash.
35    DefPathHash,
36    /// The fingerprint is actually a HirId.
37    HirId,
38    /// Query key was `()` or equivalent, so fingerprint is just zero.
39    Unit,
40    /// The fingerprint is an opaque hash, and a key cannot be reconstructed from it.
41    Opaque,
42}
43
44impl FingerprintStyle {
45    #[inline]
46    pub const fn reconstructible(self) -> bool {
47        match self {
48            FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
49                true
50            }
51            FingerprintStyle::Opaque => false,
52        }
53    }
54}
55
56/// Execute the operation with provided dependencies.
57fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
58where
59    OP: FnOnce() -> R,
60{
61    ty::tls::with_context(|icx| {
62        let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
63        ty::tls::enter_context(&icx, op)
64    })
65}
66
67/// Access dependencies from current implicit context.
68fn read_deps<OP>(op: OP)
69where
70    OP: for<'a> FnOnce(TaskDepsRef<'a>),
71{
72    ty::tls::with_context_opt(|icx| {
73        let Some(icx) = icx else { return };
74        op(icx.task_deps)
75    })
76}
77
78impl<'tcx> TyCtxt<'tcx> {
79    #[inline]
80    pub fn dep_kind_vtable(self, dk: DepKind) -> &'tcx DepKindVTable<'tcx> {
81        &self.dep_kind_vtables[dk.as_usize()]
82    }
83
84    fn with_reduced_queries<T>(self, f: impl FnOnce() -> T) -> T {
85        { let _guard = ReducedQueriesGuard::new(); f() }with_reduced_queries!(f())
86    }
87
88    #[inline(always)]
89    pub fn fingerprint_style(self, kind: DepKind) -> FingerprintStyle {
90        self.dep_kind_vtable(kind).fingerprint_style
91    }
92
93    /// Try to force a dep node to execute and see if it's green.
94    ///
95    /// Returns true if the query has actually been forced. It is valid that a query
96    /// fails to be forced, e.g. when the query key cannot be reconstructed from the
97    /// dep-node or when the query kind outright does not support it.
98    #[inline]
99    #[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(99u32),
                                    ::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")]
100    fn try_force_from_dep_node(
101        self,
102        dep_node: DepNode,
103        prev_index: SerializedDepNodeIndex,
104        frame: &MarkFrame<'_>,
105    ) -> bool {
106        if let Some(force_fn) = self.dep_kind_vtable(dep_node.kind).force_from_dep_node {
107            match panic::catch_unwind(panic::AssertUnwindSafe(|| {
108                force_fn(self, dep_node, prev_index)
109            })) {
110                Err(value) => {
111                    if !value.is::<rustc_errors::FatalErrorMarker>() {
112                        print_markframe_trace(&self.dep_graph, frame);
113                    }
114                    panic::resume_unwind(value)
115                }
116                Ok(query_has_been_forced) => query_has_been_forced,
117            }
118        } else {
119            false
120        }
121    }
122
123    /// Load data from the on-disk cache.
124    fn try_load_from_on_disk_cache(self, dep_node: &DepNode) {
125        if let Some(try_load_fn) = self.dep_kind_vtable(dep_node.kind).try_load_from_on_disk_cache {
126            try_load_fn(self, *dep_node)
127        }
128    }
129}