Skip to main content

rustc_middle/dep_graph/
mod.rs

1use std::panic;
2
3use tracing::instrument;
4
5pub use self::dep_node::{DepKind, DepKindVTable, DepNode, WorkProductId, dep_kind_from_label};
6pub use self::dep_node_key::DepNodeKey;
7pub use self::graph::{
8    DepGraph, DepGraphData, DepNodeIndex, QuerySideEffect, TaskDepsRef, WorkProduct,
9    WorkProductMap, hash_result,
10};
11use self::graph::{MarkFrame, print_markframe_trace};
12pub use self::retained::RetainedDepGraph;
13pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
14pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
15pub use crate::ty::print::with_reduced_queries;
16use crate::ty::{self, TyCtxt};
17
18mod debug;
19pub(crate) mod dep_node;
20mod dep_node_key;
21mod edges;
22mod graph;
23mod retained;
24mod serialized;
25
26/// Describes the contents of the fingerprint generated by a given query.
27///
28/// This is mainly for determining whether and how we can reconstruct a key
29/// from the fingerprint.
30#[derive(#[automatically_derived]
impl ::core::fmt::Debug for KeyFingerprintStyle {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                KeyFingerprintStyle::DefPathHash => "DefPathHash",
                KeyFingerprintStyle::HirId => "HirId",
                KeyFingerprintStyle::Unit => "Unit",
                KeyFingerprintStyle::Opaque => "Opaque",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for KeyFingerprintStyle {
    #[inline]
    fn eq(&self, other: &KeyFingerprintStyle) -> 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 KeyFingerprintStyle {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::marker::Copy for KeyFingerprintStyle { }Copy, #[automatically_derived]
impl ::core::clone::Clone for KeyFingerprintStyle {
    #[inline]
    fn clone(&self) -> KeyFingerprintStyle { *self }
}Clone)]
31pub enum KeyFingerprintStyle {
32    /// The fingerprint is actually a DefPathHash.
33    DefPathHash,
34    /// The fingerprint is actually a HirId.
35    HirId,
36    /// Query key was `()` or equivalent, so fingerprint is just zero.
37    Unit,
38    /// The fingerprint is an opaque hash, and a key cannot be reconstructed from it.
39    Opaque,
40}
41
42impl KeyFingerprintStyle {
43    /// True if a key can _potentially_ be recovered from a key fingerprint
44    /// with this style.
45    ///
46    /// For some key types, recovery is possible but not guaranteed.
47    #[inline]
48    pub const fn is_maybe_recoverable(self) -> bool {
49        match self {
50            KeyFingerprintStyle::DefPathHash
51            | KeyFingerprintStyle::Unit
52            | KeyFingerprintStyle::HirId => true,
53            KeyFingerprintStyle::Opaque => false,
54        }
55    }
56}
57
58/// Execute the operation with provided dependencies.
59fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
60where
61    OP: FnOnce() -> R,
62{
63    ty::tls::with_context(|icx| {
64        let icx = ty::tls::ImplicitCtxt { task_deps, ..*icx };
65        ty::tls::enter_context(&icx, op)
66    })
67}
68
69/// Access dependencies from current implicit context.
70fn read_deps<OP>(op: OP)
71where
72    OP: for<'a> FnOnce(TaskDepsRef<'a>),
73{
74    ty::tls::with_context_opt(|icx| {
75        let Some(icx) = icx else { return };
76        op(icx.task_deps)
77    })
78}
79
80impl<'tcx> TyCtxt<'tcx> {
81    #[inline]
82    pub fn dep_kind_vtable(self, dk: DepKind) -> &'tcx DepKindVTable<'tcx> {
83        &self.dep_kind_vtables[dk.as_usize()]
84    }
85
86    #[inline(always)]
87    pub fn key_fingerprint_style(self, kind: DepKind) -> KeyFingerprintStyle {
88        self.dep_kind_vtable(kind).key_fingerprint_style
89    }
90
91    /// Try to force a dep node to execute and see if it's green.
92    ///
93    /// Returns true if the query has actually been forced. It is valid that a query
94    /// fails to be forced, e.g. when the query key cannot be reconstructed from the
95    /// dep-node or when the query kind outright does not support it.
96    #[inline]
97    #[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(97u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_middle::dep_graph"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("dep_node")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("dep_node");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("prev_index")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("prev_index");
                                                        NAME.as_str()
                                                    }], ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&dep_node)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&prev_index)
                                                            as &dyn ::tracing::field::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_fn {
                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")]
98    fn try_force_from_dep_node(
99        self,
100        dep_node: DepNode,
101        prev_index: SerializedDepNodeIndex,
102        frame: &MarkFrame<'_>,
103    ) -> bool {
104        if let Some(force_fn) = self.dep_kind_vtable(dep_node.kind).force_from_dep_node_fn {
105            match panic::catch_unwind(panic::AssertUnwindSafe(|| {
106                force_fn(self, dep_node, prev_index)
107            })) {
108                Err(value) => {
109                    if !value.is::<rustc_errors::FatalErrorMarker>() {
110                        print_markframe_trace(&self.dep_graph, frame);
111                    }
112                    panic::resume_unwind(value)
113                }
114                Ok(query_has_been_forced) => query_has_been_forced,
115            }
116        } else {
117            false
118        }
119    }
120}