2019-12-25 13:38:57 -05:00
|
|
|
//! Throughout the compiler tree, there are several places which want to have
|
|
|
|
//! access to state or queries while being inside crates that are dependencies
|
2021-04-07 11:45:27 -05:00
|
|
|
//! of `rustc_middle`. To facilitate this, we have the
|
2019-12-25 13:38:57 -05:00
|
|
|
//! `rustc_data_structures::AtomicRef` type, which allows us to setup a global
|
|
|
|
//! static which can then be set in this file at program startup.
|
|
|
|
//!
|
2022-01-14 19:34:01 +01:00
|
|
|
//! See `SPAN_TRACK` for an example of how to set things up.
|
2019-12-25 13:38:57 -05:00
|
|
|
//!
|
|
|
|
//! The functions in this file should fall back to the default set in their
|
|
|
|
//! origin crate when the `TyCtxt` is not present in TLS.
|
|
|
|
|
|
|
|
use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS};
|
2021-04-30 19:38:06 +02:00
|
|
|
use rustc_middle::dep_graph::TaskDepsRef;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::tls;
|
2019-12-25 13:38:57 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
2021-04-27 19:24:33 +02:00
|
|
|
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
|
|
|
|
tls::with_opt(|tcx| {
|
|
|
|
if let Some(tcx) = tcx {
|
2021-04-27 19:58:46 +02:00
|
|
|
let _span = tcx.source_span(def_id);
|
|
|
|
// Sanity check: relative span's parent must be an absolute span.
|
|
|
|
debug_assert_eq!(_span.data_untracked().parent, None);
|
2021-04-27 19:24:33 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:45:27 -05:00
|
|
|
/// This is a callback from `rustc_ast` as it cannot access the implicit state
|
2022-05-23 22:51:34 +02:00
|
|
|
/// in `rustc_middle` otherwise. It is used when diagnostic messages are
|
2019-12-25 13:38:57 -05:00
|
|
|
/// emitted and stores them in the current query, if there is one.
|
2021-04-30 19:38:06 +02:00
|
|
|
fn track_diagnostic(diagnostic: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnostic)) {
|
2019-12-25 13:38:57 -05:00
|
|
|
tls::with_context_opt(|icx| {
|
|
|
|
if let Some(icx) = icx {
|
2021-09-30 19:38:50 +02:00
|
|
|
if let Some(diagnostics) = icx.diagnostics {
|
2019-12-25 13:38:57 -05:00
|
|
|
let mut diagnostics = diagnostics.lock();
|
|
|
|
diagnostics.extend(Some(diagnostic.clone()));
|
2021-04-30 19:38:06 +02:00
|
|
|
std::mem::drop(diagnostics);
|
2019-12-25 13:38:57 -05:00
|
|
|
}
|
2021-04-30 19:38:06 +02:00
|
|
|
|
|
|
|
// Diagnostics are tracked, we can ignore the dependency.
|
|
|
|
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
|
2023-02-07 16:11:40 +11:00
|
|
|
return tls::enter_context(&icx, move || (*f)(diagnostic));
|
2019-12-25 13:38:57 -05:00
|
|
|
}
|
2021-04-30 19:38:06 +02:00
|
|
|
|
|
|
|
// In any other case, invoke diagnostics anyway.
|
|
|
|
(*f)(diagnostic);
|
2019-12-25 13:38:57 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-07 11:45:27 -05:00
|
|
|
/// This is a callback from `rustc_hir` as it cannot access the implicit state
|
|
|
|
/// in `rustc_middle` otherwise.
|
2019-12-25 03:51:27 +01:00
|
|
|
fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "DefId({}:{}", def_id.krate, def_id.index.index())?;
|
|
|
|
tls::with_opt(|opt_tcx| {
|
|
|
|
if let Some(tcx) = opt_tcx {
|
|
|
|
write!(f, " ~ {}", tcx.def_path_debug_str(def_id))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
|
|
|
|
2019-12-25 13:38:57 -05:00
|
|
|
/// Sets up the callbacks in prior crates which we want to refer to the
|
|
|
|
/// TyCtxt in.
|
|
|
|
pub fn setup_callbacks() {
|
2021-04-27 19:24:33 +02:00
|
|
|
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
|
2019-12-25 03:51:27 +01:00
|
|
|
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
|
2021-04-30 19:38:06 +02:00
|
|
|
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as _));
|
2019-12-25 13:38:57 -05:00
|
|
|
}
|