Skip to main content

rustc_middle/
verify_ich.rs

1use std::cell::Cell;
2
3use rustc_data_structures::fingerprint::Fingerprint;
4use tracing::instrument;
5
6use crate::dep_graph::{DepGraphData, SerializedDepNodeIndex};
7use crate::ich::StableHashingContext;
8use crate::ty::TyCtxt;
9
10#[inline]
11#[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("incremental_verify_ich",
                                    "rustc_middle::verify_ich", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/verify_ich.rs"),
                                    ::tracing_core::__macro_support::Option::Some(11u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_middle::verify_ich"),
                                    ::tracing_core::field::FieldSet::new(&["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(&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: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if !dep_graph_data.is_index_green(prev_index) {
                incremental_verify_ich_not_green(tcx, prev_index)
            }
            let new_hash =
                hash_result.map_or(Fingerprint::ZERO,
                    |f|
                        {
                            tcx.with_stable_hashing_context(|mut hcx|
                                    f(&mut hcx, result))
                        });
            let old_hash =
                dep_graph_data.prev_value_fingerprint_of(prev_index);
            if new_hash != old_hash {
                incremental_verify_ich_failed(tcx, prev_index,
                    &|| format_value(result));
            }
        }
    }
}#[instrument(skip(tcx, dep_graph_data, result, hash_result, format_value), level = "debug")]
12pub fn incremental_verify_ich<'tcx, V>(
13    tcx: TyCtxt<'tcx>,
14    dep_graph_data: &DepGraphData,
15    result: &V,
16    prev_index: SerializedDepNodeIndex,
17    hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
18    format_value: fn(&V) -> String,
19) {
20    if !dep_graph_data.is_index_green(prev_index) {
21        incremental_verify_ich_not_green(tcx, prev_index)
22    }
23
24    let new_hash = hash_result.map_or(Fingerprint::ZERO, |f| {
25        tcx.with_stable_hashing_context(|mut hcx| f(&mut hcx, result))
26    });
27
28    let old_hash = dep_graph_data.prev_value_fingerprint_of(prev_index);
29
30    if new_hash != old_hash {
31        incremental_verify_ich_failed(tcx, prev_index, &|| format_value(result));
32    }
33}
34
35#[cold]
36#[inline(never)]
37fn incremental_verify_ich_not_green<'tcx>(tcx: TyCtxt<'tcx>, prev_index: SerializedDepNodeIndex) {
38    {
    ::core::panicking::panic_fmt(format_args!("fingerprint for green query instance not loaded from cache: {0:?}",
            tcx.dep_graph.data().unwrap().prev_node_of(prev_index)));
}panic!(
39        "fingerprint for green query instance not loaded from cache: {:?}",
40        tcx.dep_graph.data().unwrap().prev_node_of(prev_index)
41    )
42}
43
44// Note that this is marked #[cold] and intentionally takes `dyn Debug` for `result`,
45// as we want to avoid generating a bunch of different implementations for LLVM to
46// chew on (and filling up the final binary, too).
47#[cold]
48#[inline(never)]
49fn incremental_verify_ich_failed<'tcx>(
50    tcx: TyCtxt<'tcx>,
51    prev_index: SerializedDepNodeIndex,
52    result: &dyn Fn() -> String,
53) {
54    // When we emit an error message and panic, we try to debug-print the `DepNode`
55    // and query result. Unfortunately, this can cause us to run additional queries,
56    // which may result in another fingerprint mismatch while we're in the middle
57    // of processing this one. To avoid a double-panic (which kills the process
58    // before we can print out the query static), we print out a terse
59    // but 'safe' message if we detect a reentrant call to this method.
60    const INSIDE_VERIFY_PANIC: ::std::thread::LocalKey<Cell<bool>> =
    {
        const __RUST_STD_INTERNAL_INIT: Cell<bool> = { Cell::new(false) };
        unsafe {
            ::std::thread::LocalKey::new(const {
                        if ::std::mem::needs_drop::<Cell<bool>>() {
                            |_|
                                {
                                    #[thread_local]
                                    static __RUST_STD_INTERNAL_VAL:
                                        ::std::thread::local_impl::EagerStorage<Cell<bool>> =
                                        ::std::thread::local_impl::EagerStorage::new(__RUST_STD_INTERNAL_INIT);
                                    __RUST_STD_INTERNAL_VAL.get()
                                }
                        } else {
                            |_|
                                {
                                    #[thread_local]
                                    static __RUST_STD_INTERNAL_VAL: Cell<bool> =
                                        __RUST_STD_INTERNAL_INIT;
                                    &__RUST_STD_INTERNAL_VAL
                                }
                        }
                    })
        }
    };thread_local! {
61        static INSIDE_VERIFY_PANIC: Cell<bool> = const { Cell::new(false) };
62    };
63
64    let old_in_panic = INSIDE_VERIFY_PANIC.replace(true);
65
66    if old_in_panic {
67        tcx.dcx().emit_err(crate::error::Reentrant);
68    } else {
69        let run_cmd = if let Some(crate_name) = &tcx.sess.opts.crate_name {
70            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`cargo clean -p {0}` or `cargo clean`",
                crate_name))
    })format!("`cargo clean -p {crate_name}` or `cargo clean`")
71        } else {
72            "`cargo clean`".to_string()
73        };
74
75        let dep_node = tcx.dep_graph.data().unwrap().prev_node_of(prev_index);
76        tcx.dcx().emit_err(crate::error::IncrementCompilation {
77            run_cmd,
78            dep_node: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", dep_node))
    })format!("{dep_node:?}"),
79        });
80        {
    ::core::panicking::panic_fmt(format_args!("Found unstable fingerprints for {1:?}: {0}",
            result(), dep_node));
};panic!("Found unstable fingerprints for {dep_node:?}: {}", result());
81    }
82
83    INSIDE_VERIFY_PANIC.set(old_in_panic);
84}