Make impl Debug for Span
not panic on not having session globals.
This commit is contained in:
parent
56e28e904d
commit
f17c805701
1 changed files with 20 additions and 11 deletions
|
@ -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.
|
||||||
|
|
||||||
|
fn fallback(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("Span")
|
||||||
|
.field("lo", &span.lo())
|
||||||
|
.field("hi", &span.hi())
|
||||||
|
.field("ctxt", &span.ctxt())
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
if SESSION_GLOBALS.is_set() {
|
||||||
with_session_globals(|session_globals| {
|
with_session_globals(|session_globals| {
|
||||||
if let Some(source_map) = &*session_globals.source_map.borrow() {
|
if let Some(source_map) = &*session_globals.source_map.borrow() {
|
||||||
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
|
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
|
||||||
} else {
|
} else {
|
||||||
f.debug_struct("Span")
|
fallback(*self, f)
|
||||||
.field("lo", &self.lo())
|
|
||||||
.field("hi", &self.hi())
|
|
||||||
.field("ctxt", &self.ctxt())
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
fallback(*self, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue