about summary refs log tree commit diff
path: root/compiler/rustc_span
diff options
context:
space:
mode:
authorKevin Reid <kpreid@switchb.org>2023-04-18 10:47:41 -0700
committerKevin Reid <kpreid@switchb.org>2023-04-19 08:55:21 -0700
commitf17c80570163650bcac4e75088f11d2c87e20383 (patch)
tree02a6717a6c121e2390e6d4b1e9a829f1b63dcc26 /compiler/rustc_span
parent56e28e904d6b3404abaaeb65d33636cc35ca3f4b (diff)
downloadrust-f17c80570163650bcac4e75088f11d2c87e20383.tar.gz
rust-f17c80570163650bcac4e75088f11d2c87e20383.zip
Make `impl Debug for Span` not panic on not having session globals.
Diffstat (limited to 'compiler/rustc_span')
-rw-r--r--compiler/rustc_span/src/lib.rs31
1 files changed, 20 insertions, 11 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 911d6902c98..74a07a119e7 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)
+        }
     }
 }