about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2023-04-20 17:03:25 +0900
committerGitHub <noreply@github.com>2023-04-20 17:03:25 +0900
commit615669dbb2c508da3738845fff1ad527f4ef8a4a (patch)
tree49f7f76d9ea6871331e47ae7d5ff267d525a397a
parentf71e78e0df2a3bbac30f4a1873afc8d5c180def7 (diff)
parent883606f2c06821e0a2c0e7a8c6219a1225d9bea4 (diff)
downloadrust-615669dbb2c508da3738845fff1ad527f4ef8a4a.tar.gz
rust-615669dbb2c508da3738845fff1ad527f4ef8a4a.zip
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.)
-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 83f4907d517..8a900ca427e 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)
+        }
     }
 }