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.)
This commit is contained in:
commit
615669dbb2
@ -1044,17 +1044,26 @@ impl fmt::Debug for Span {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// Use the global `SourceMap` to print the span. If that's not
|
// Use the global `SourceMap` to print the span. If that's not
|
||||||
// available, fall back to printing the raw values.
|
// available, fall back to printing the raw values.
|
||||||
with_session_globals(|session_globals| {
|
|
||||||
if let Some(source_map) = &*session_globals.source_map.borrow() {
|
fn fallback(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
|
f.debug_struct("Span")
|
||||||
} else {
|
.field("lo", &span.lo())
|
||||||
f.debug_struct("Span")
|
.field("hi", &span.hi())
|
||||||
.field("lo", &self.lo())
|
.field("ctxt", &span.ctxt())
|
||||||
.field("hi", &self.hi())
|
.finish()
|
||||||
.field("ctxt", &self.ctxt())
|
}
|
||||||
.finish()
|
|
||||||
}
|
if SESSION_GLOBALS.is_set() {
|
||||||
})
|
with_session_globals(|session_globals| {
|
||||||
|
if let Some(source_map) = &*session_globals.source_map.borrow() {
|
||||||
|
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
|
||||||
|
} else {
|
||||||
|
fallback(*self, f)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
fallback(*self, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user