Skip to content

Commit 4e63ab6

Browse files
committed
Improve with_source_map.
Rename `with_source_map` as `set_source_map`. Because `with` functions (e.g. `with_session_globals`, `scoped_tls::ScopedKey::with`) are for *getting* a value for the duration of a closure, and `set` functions (e.g. `set_session_globals_then` `scoped_tls::ScopedKey::with`) are for *setting* a value for the duration of a closure. Also fix up the comment, which is wrong: - The bit about `TyCtxt` is wrong. - `span_debug1` doesn't exist any more. - There's only one level of fallback, not two. (This is effectively a follow-up to the changes in #93936.) Also add a comment explaining that `SessionGlobals::source_map` should only be used when absolutely necessary.
1 parent f049d5d commit 4e63ab6

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

compiler/rustc_interface/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
292292
override_queries: config.override_queries,
293293
};
294294

295-
rustc_span::with_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
295+
rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
296296
let r = {
297297
let _sess_abort_error = OnDrop(|| {
298298
compiler.sess.finish_diagnostics(registry);

compiler/rustc_span/src/lib.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ pub struct SessionGlobals {
8787
symbol_interner: symbol::Interner,
8888
span_interner: Lock<span_encoding::SpanInterner>,
8989
hygiene_data: Lock<hygiene::HygieneData>,
90+
91+
/// A reference to the source map in the `Session`. It's an `Option`
92+
/// because it can't be initialized until `Session` is created, which
93+
/// happens after `SessionGlobals`. `set_source_map` does the
94+
/// initialization.
95+
///
96+
/// This field should only be used in places where the `Session` is truly
97+
/// not available, such as `<Span as Debug>::fmt`.
9098
source_map: Lock<Option<Lrc<SourceMap>>>,
9199
}
92100

@@ -1013,16 +1021,9 @@ impl<D: Decoder> Decodable<D> for Span {
10131021
}
10141022
}
10151023

1016-
/// Calls the provided closure, using the provided `SourceMap` to format
1017-
/// any spans that are debug-printed during the closure's execution.
1018-
///
1019-
/// Normally, the global `TyCtxt` is used to retrieve the `SourceMap`
1020-
/// (see `rustc_interface::callbacks::span_debug1`). However, some parts
1021-
/// of the compiler (e.g. `rustc_parse`) may debug-print `Span`s before
1022-
/// a `TyCtxt` is available. In this case, we fall back to
1023-
/// the `SourceMap` provided to this function. If that is not available,
1024-
/// we fall back to printing the raw `Span` field values.
1025-
pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) -> T {
1024+
/// Insert `source_map` into the session globals for the duration of the
1025+
/// closure's execution.
1026+
pub fn set_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) -> T {
10261027
with_session_globals(|session_globals| {
10271028
*session_globals.source_map.borrow_mut() = Some(source_map);
10281029
});
@@ -1041,6 +1042,8 @@ pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) ->
10411042

10421043
impl fmt::Debug for Span {
10431044
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1045+
// Use the global `SourceMap` to print the span. If that's not
1046+
// available, fall back to printing the raw values.
10441047
with_session_globals(|session_globals| {
10451048
if let Some(source_map) = &*session_globals.source_map.borrow() {
10461049
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())

0 commit comments

Comments
 (0)