rustc_interface/
callbacks.rs
1use std::fmt;
13
14use 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};
20
21fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
22 tls::with_context_opt(|icx| {
23 if let Some(icx) = icx {
24 let tracks_deps = match icx.task_deps {
27 TaskDepsRef::Allow(..) => true,
28 TaskDepsRef::EvalAlways | TaskDepsRef::Ignore | TaskDepsRef::Forbid => false,
29 };
30 if tracks_deps {
31 let _span = icx.tcx.source_span(def_id);
32 debug_assert_eq!(_span.data_untracked().parent, None);
34 }
35 }
36 })
37}
38
39fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
43 tls::with_context_opt(|icx| {
44 if let Some(icx) = icx {
45 icx.tcx.dep_graph.record_diagnostic(QueryCtxt::new(icx.tcx), &diagnostic);
46
47 let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
49 tls::enter_context(&icx, move || (*f)(diagnostic))
50 } else {
51 (*f)(diagnostic)
53 }
54 })
55}
56
57fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 write!(f, "DefId({}:{}", def_id.krate, def_id.index.index())?;
61 tls::with_opt(|opt_tcx| {
62 if let Some(tcx) = opt_tcx {
63 write!(f, " ~ {}", tcx.def_path_debug_str(def_id))?;
64 }
65 Ok(())
66 })?;
67 write!(f, ")")
68}
69
70pub fn dep_kind_debug(kind: DepKind, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 tls::with_opt(|opt_tcx| {
74 if let Some(tcx) = opt_tcx {
75 write!(f, "{}", tcx.dep_kind_info(kind).name)
76 } else {
77 default_dep_kind_debug(kind, f)
78 }
79 })
80}
81
82pub fn dep_node_debug(node: DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 write!(f, "{:?}(", node.kind)?;
86
87 tls::with_opt(|opt_tcx| {
88 if let Some(tcx) = opt_tcx {
89 if let Some(def_id) = node.extract_def_id(tcx) {
90 write!(f, "{}", tcx.def_path_debug_str(def_id))?;
91 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(node) {
92 write!(f, "{s}")?;
93 } else {
94 write!(f, "{}", node.hash)?;
95 }
96 } else {
97 write!(f, "{}", node.hash)?;
98 }
99 Ok(())
100 })?;
101
102 write!(f, ")")
103}
104
105pub fn setup_callbacks() {
108 rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
109 rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
110 rustc_query_system::dep_graph::dep_node::DEP_KIND_DEBUG
111 .swap(&(dep_kind_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
112 rustc_query_system::dep_graph::dep_node::DEP_NODE_DEBUG
113 .swap(&(dep_node_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
114 TRACK_DIAGNOSTIC.swap(&(track_diagnostic as _));
115}