From f17c80570163650bcac4e75088f11d2c87e20383 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Tue, 18 Apr 2023 10:47:41 -0700 Subject: [PATCH 1/2] Make `impl Debug for Span` not panic on not having session globals. --- compiler/rustc_span/src/lib.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 911d6902c981a..74a07a119e77e 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1044,17 +1044,26 @@ impl fmt::Debug for Span { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Use the global `SourceMap` to print the span. If that's not // available, fall back to printing the raw values. - 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 { - f.debug_struct("Span") - .field("lo", &self.lo()) - .field("hi", &self.hi()) - .field("ctxt", &self.ctxt()) - .finish() - } - }) + + 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| { + 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) + } } } From 883606f2c06821e0a2c0e7a8c6219a1225d9bea4 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Wed, 19 Apr 2023 10:06:30 -0700 Subject: [PATCH 2/2] fmt fix --- compiler/rustc_span/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 74a07a119e77e..072b53f211494 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1052,7 +1052,7 @@ impl fmt::Debug for Span { .field("ctxt", &span.ctxt()) .finish() } - + if SESSION_GLOBALS.is_set() { with_session_globals(|session_globals| { if let Some(source_map) = &*session_globals.source_map.borrow() {