Skip to content

Commit d732934

Browse files
Rollup merge of #109805 - nnethercote:source_map-cleanups, r=bjorn3
Source map cleanups r? `@bjorn3`
2 parents bd4e3f3 + 4e63ab6 commit d732934

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

compiler/rustc_expand/src/proc_macro.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl base::BangProcMacro for BangProcMacro {
5454
) -> Result<TokenStream, ErrorGuaranteed> {
5555
let _timer =
5656
ecx.sess.prof.generic_activity_with_arg_recorder("expand_proc_macro", |recorder| {
57-
recorder.record_arg_with_span(ecx.expansion_descr(), span);
57+
recorder.record_arg_with_span(ecx.sess.source_map(), ecx.expansion_descr(), span);
5858
});
5959

6060
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
@@ -85,7 +85,7 @@ impl base::AttrProcMacro for AttrProcMacro {
8585
) -> Result<TokenStream, ErrorGuaranteed> {
8686
let _timer =
8787
ecx.sess.prof.generic_activity_with_arg_recorder("expand_proc_macro", |recorder| {
88-
recorder.record_arg_with_span(ecx.expansion_descr(), span);
88+
recorder.record_arg_with_span(ecx.sess.source_map(), ecx.expansion_descr(), span);
8989
});
9090

9191
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
@@ -134,7 +134,11 @@ impl MultiItemModifier for DeriveProcMacro {
134134
let stream = {
135135
let _timer =
136136
ecx.sess.prof.generic_activity_with_arg_recorder("expand_proc_macro", |recorder| {
137-
recorder.record_arg_with_span(ecx.expansion_descr(), span);
137+
recorder.record_arg_with_span(
138+
ecx.sess.source_map(),
139+
ecx.expansion_descr(),
140+
span,
141+
);
138142
});
139143
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
140144
let strategy = exec_strategy(ecx);

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())

compiler/rustc_span/src/profiling.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::source_map::SourceMap;
2+
13
use std::borrow::Borrow;
24

35
use rustc_data_structures::profiling::EventArgRecorder;
@@ -11,25 +13,17 @@ pub trait SpannedEventArgRecorder {
1113
///
1214
/// Note: when self-profiling with costly event arguments, at least one argument
1315
/// needs to be recorded. A panic will be triggered if that doesn't happen.
14-
fn record_arg_with_span<A>(&mut self, event_arg: A, span: crate::Span)
16+
fn record_arg_with_span<A>(&mut self, source_map: &SourceMap, event_arg: A, span: crate::Span)
1517
where
1618
A: Borrow<str> + Into<String>;
1719
}
1820

1921
impl SpannedEventArgRecorder for EventArgRecorder<'_> {
20-
fn record_arg_with_span<A>(&mut self, event_arg: A, span: crate::Span)
22+
fn record_arg_with_span<A>(&mut self, source_map: &SourceMap, event_arg: A, span: crate::Span)
2123
where
2224
A: Borrow<str> + Into<String>,
2325
{
2426
self.record_arg(event_arg);
25-
26-
let span_arg = crate::with_session_globals(|session_globals| {
27-
if let Some(source_map) = &*session_globals.source_map.borrow() {
28-
source_map.span_to_embeddable_string(span)
29-
} else {
30-
format!("{span:?}")
31-
}
32-
});
33-
self.record_arg(span_arg);
27+
self.record_arg(source_map.span_to_embeddable_string(span));
3428
}
3529
}

0 commit comments

Comments
 (0)