Skip to content

Commit 615669d

Browse files
authored
Rollup merge of #110548 - kpreid:span, r=WaffleLapkin
Make `impl Debug for Span` not panic on not having session globals. I hit the panic that this patch avoids while messing with the early lints in `rustc_session::config::build_session_options()`. The rest of that project is not finished, but this seemed like a self-contained improvement. (Should changes like this add tests? I don't see similar unit tests.)
2 parents f71e78e + 883606f commit 615669d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

compiler/rustc_span/src/lib.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -1044,17 +1044,26 @@ impl fmt::Debug for Span {
10441044
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10451045
// Use the global `SourceMap` to print the span. If that's not
10461046
// available, fall back to printing the raw values.
1047-
with_session_globals(|session_globals| {
1048-
if let Some(source_map) = &*session_globals.source_map.borrow() {
1049-
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
1050-
} else {
1051-
f.debug_struct("Span")
1052-
.field("lo", &self.lo())
1053-
.field("hi", &self.hi())
1054-
.field("ctxt", &self.ctxt())
1055-
.finish()
1056-
}
1057-
})
1047+
1048+
fn fallback(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1049+
f.debug_struct("Span")
1050+
.field("lo", &span.lo())
1051+
.field("hi", &span.hi())
1052+
.field("ctxt", &span.ctxt())
1053+
.finish()
1054+
}
1055+
1056+
if SESSION_GLOBALS.is_set() {
1057+
with_session_globals(|session_globals| {
1058+
if let Some(source_map) = &*session_globals.source_map.borrow() {
1059+
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
1060+
} else {
1061+
fallback(*self, f)
1062+
}
1063+
})
1064+
} else {
1065+
fallback(*self, f)
1066+
}
10581067
}
10591068
}
10601069

0 commit comments

Comments
 (0)