]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_interface/src/callbacks.rs
Auto merge of #106235 - compiler-errors:rework-bounds-collection, r=davidtwco
[rust.git] / compiler / rustc_interface / src / 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
12 use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS};
13 use rustc_middle::dep_graph::TaskDepsRef;
14 use rustc_middle::ty::tls;
15 use std::fmt;
16
17 fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
18     tls::with_opt(|tcx| {
19         if let Some(tcx) = tcx {
20             let _span = tcx.source_span(def_id);
21             // Sanity check: relative span's parent must be an absolute span.
22             debug_assert_eq!(_span.data_untracked().parent, None);
23         }
24     })
25 }
26
27 /// This is a callback from `rustc_ast` as it cannot access the implicit state
28 /// in `rustc_middle` otherwise. It is used when diagnostic messages are
29 /// emitted and stores them in the current query, if there is one.
30 fn track_diagnostic(diagnostic: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnostic)) {
31     tls::with_context_opt(|icx| {
32         if let Some(icx) = icx {
33             if let Some(diagnostics) = icx.diagnostics {
34                 let mut diagnostics = diagnostics.lock();
35                 diagnostics.extend(Some(diagnostic.clone()));
36                 std::mem::drop(diagnostics);
37             }
38
39             // Diagnostics are tracked, we can ignore the dependency.
40             let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
41             return tls::enter_context(&icx, move |_| (*f)(diagnostic));
42         }
43
44         // In any other case, invoke diagnostics anyway.
45         (*f)(diagnostic);
46     })
47 }
48
49 /// This is a callback from `rustc_hir` as it cannot access the implicit state
50 /// in `rustc_middle` otherwise.
51 fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52     write!(f, "DefId({}:{}", def_id.krate, def_id.index.index())?;
53     tls::with_opt(|opt_tcx| {
54         if let Some(tcx) = opt_tcx {
55             write!(f, " ~ {}", tcx.def_path_debug_str(def_id))?;
56         }
57         Ok(())
58     })?;
59     write!(f, ")")
60 }
61
62 /// Sets up the callbacks in prior crates which we want to refer to the
63 /// TyCtxt in.
64 pub fn setup_callbacks() {
65     rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
66     rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
67     TRACK_DIAGNOSTICS.swap(&(track_diagnostic as _));
68 }