Skip to main content

rustc_interface/
callbacks.rs

1//! Throughout the compiler tree, there are several places which want to have
2//! access to state or queries while being inside crates that are dependencies
3//! of `rustc_middle`. To facilitate this, we have the
4//! `rustc_data_structures::AtomicRef` type, which allows us to setup a global
5//! static which can then be set in this file at program startup.
6//!
7//! See `SPAN_TRACK` for an example of how to set things up.
8//!
9//! The functions in this file should fall back to the default set in their
10//! origin crate when the `TyCtxt` is not present in TLS.
11
12use std::fmt;
13
14use rustc_errors::DiagInner;
15use rustc_middle::dep_graph::TaskDepsRef;
16use rustc_middle::ty::tls;
17
18fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
19    tls::with_context_opt(|icx| {
20        if let Some(icx) = icx {
21            // `track_span_parent` gets called a lot from HIR lowering code.
22            // Skip doing anything if we aren't tracking dependencies.
23            let tracks_deps = match icx.task_deps {
24                TaskDepsRef::Allow(..) => true,
25                TaskDepsRef::EvalAlways | TaskDepsRef::Ignore | TaskDepsRef::Forbid => false,
26            };
27            if tracks_deps {
28                let _span = icx.tcx.source_span(def_id);
29                // Sanity check: relative span's parent must be an absolute span.
30                if true {
    match (&_span.data_untracked().parent, &None) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(_span.data_untracked().parent, None);
31            }
32        }
33    })
34}
35
36/// This is a callback from `rustc_errors` as it cannot access the implicit state
37/// in `rustc_middle` otherwise. It is used when diagnostic messages are
38/// emitted and stores them in the current query, if there is one.
39fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
40    tls::with_context_opt(|icx| {
41        if let Some(icx) = icx {
42            icx.tcx.dep_graph.record_diagnostic(icx.tcx, &diagnostic);
43
44            // Diagnostics are tracked, we can ignore the dependency.
45            let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
46            tls::enter_context(&icx, move || (*f)(diagnostic))
47        } else {
48            // In any other case, invoke diagnostics anyway.
49            (*f)(diagnostic)
50        }
51    })
52}
53
54/// This is a callback from `rustc_hir` as it cannot access the implicit state
55/// in `rustc_middle` otherwise.
56fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57    f.write_fmt(format_args!("DefId({0}:{1}", def_id.krate, def_id.index.index()))write!(f, "DefId({}:{}", def_id.krate, def_id.index.index())?;
58    tls::with_opt(|opt_tcx| {
59        if let Some(tcx) = opt_tcx {
60            f.write_fmt(format_args!(" ~ {0}", tcx.def_path_debug_str(def_id)))write!(f, " ~ {}", tcx.def_path_debug_str(def_id))?;
61        }
62        Ok(())
63    })?;
64    f.write_fmt(format_args!(")"))write!(f, ")")
65}
66
67/// Sets up the callbacks in prior crates which we want to refer to the
68/// TyCtxt in.
69pub fn setup_callbacks() {
70    rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
71    rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
72    rustc_errors::TRACK_DIAGNOSTIC.swap(&(track_diagnostic as _));
73}