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.
1112use std::fmt;
1314use rustc_errors::{DiagInner, TRACK_DIAGNOSTIC};
15use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
16use rustc_middle::ty::tls;
17use rustc_query_impl::QueryCtxt;
18use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
19use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
2021fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
22 tls::with_context_opt(|icx| {
23if let Some(icx) = icx {
24// `track_span_parent` gets called a lot from HIR lowering code.
25 // Skip doing anything if we aren't tracking dependencies.
26let tracks_deps = match icx.task_deps {
27 TaskDepsRef::Allow(..) => true,
28 TaskDepsRef::EvalAlways | TaskDepsRef::Ignore | TaskDepsRef::Forbid => false,
29 };
30if tracks_deps {
31let _span = icx.tcx.source_span(def_id);
32// Sanity check: relative span's parent must be an absolute span.
33if 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);
34 }
35 }
36 })
37}
3839/// This is a callback from `rustc_errors` as it cannot access the implicit state
40/// in `rustc_middle` otherwise. It is used when diagnostic messages are
41/// emitted and stores them in the current query, if there is one.
42fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
43 tls::with_context_opt(|icx| {
44if let Some(icx) = icx {
45icx.tcx.dep_graph.record_diagnostic(QueryCtxt::new(icx.tcx), &diagnostic);
4647// Diagnostics are tracked, we can ignore the dependency.
48let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
49 tls::enter_context(&icx, move || (*f)(diagnostic))
50 } else {
51// In any other case, invoke diagnostics anyway.
52(*f)(diagnostic)
53 }
54 })
55}
5657/// This is a callback from `rustc_hir` as it cannot access the implicit state
58/// in `rustc_middle` otherwise.
59fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60f.write_fmt(format_args!("DefId({0}:{1}", def_id.krate, def_id.index.index()))write!(f, "DefId({}:{}", def_id.krate, def_id.index.index())?;
61 tls::with_opt(|opt_tcx| {
62if let Some(tcx) = opt_tcx {
63f.write_fmt(format_args!(" ~ {0}", tcx.def_path_debug_str(def_id)))write!(f, " ~ {}", tcx.def_path_debug_str(def_id))?;
64 }
65Ok(())
66 })?;
67f.write_fmt(format_args!(")"))write!(f, ")")68}
6970/// This is a callback from `rustc_query_system` as it cannot access the implicit state
71/// in `rustc_middle` otherwise.
72pub fn dep_kind_debug(kind: DepKind, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 tls::with_opt(|opt_tcx| {
74if let Some(tcx) = opt_tcx {
75f.write_fmt(format_args!("{0}", tcx.dep_kind_info(kind).name))write!(f, "{}", tcx.dep_kind_info(kind).name)76 } else {
77default_dep_kind_debug(kind, f)
78 }
79 })
80}
8182/// This is a callback from `rustc_query_system` as it cannot access the implicit state
83/// in `rustc_middle` otherwise.
84pub fn dep_node_debug(node: DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85f.write_fmt(format_args!("{0:?}(", node.kind))write!(f, "{:?}(", node.kind)?;
8687 tls::with_opt(|opt_tcx| {
88if let Some(tcx) = opt_tcx {
89if let Some(def_id) = node.extract_def_id(tcx) {
90f.write_fmt(format_args!("{0}", tcx.def_path_debug_str(def_id)))write!(f, "{}", tcx.def_path_debug_str(def_id))?;
91 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(node) {
92f.write_fmt(format_args!("{0}", s))write!(f, "{s}")?;
93 } else {
94f.write_fmt(format_args!("{0}", node.hash))write!(f, "{}", node.hash)?;
95 }
96 } else {
97f.write_fmt(format_args!("{0}", node.hash))write!(f, "{}", node.hash)?;
98 }
99Ok(())
100 })?;
101102f.write_fmt(format_args!(")"))write!(f, ")")103}
104105/// Sets up the callbacks in prior crates which we want to refer to the
106/// TyCtxt in.
107pub fn setup_callbacks() {
108 rustc_span::SPAN_TRACK.swap(&(track_span_parentas fn(_)));
109 rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debugas fn(_, &mut fmt::Formatter<'_>) -> _));
110 rustc_query_system::dep_graph::dep_node::DEP_KIND_DEBUG111 .swap(&(dep_kind_debugas fn(_, &mut fmt::Formatter<'_>) -> _));
112 rustc_query_system::dep_graph::dep_node::DEP_NODE_DEBUG113 .swap(&(dep_node_debugas fn(_, &mut fmt::Formatter<'_>) -> _));
114TRACK_DIAGNOSTIC.swap(&(track_diagnosticas _));
115}