about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-01-27 15:38:25 +0100
committerGitHub <noreply@github.com>2025-01-27 15:38:25 +0100
commita02e78871dece4edcc7e29ba5123b258b624c1cd (patch)
tree6448b78867aabac330ba5bea7dbc9c76c620c83d
parent4d5c8bdc9df8db23983d5be0fe0fa98c89414e40 (diff)
parent6f543d5cebe220595174a135eca92765df8c97e3 (diff)
downloadrust-a02e78871dece4edcc7e29ba5123b258b624c1cd.tar.gz
rust-a02e78871dece4edcc7e29ba5123b258b624c1cd.zip
Rollup merge of #135988 - bjorn3:workaround_parallel_rustc_crash, r=lqd
Add a workaround for parallel rustc crashing when there are delayed bugs

This doesn't fix the root cause of this crash, but at least stops it from happening for the time being.

Workaround for https://github.com/rust-lang/rust/issues/135870
-rw-r--r--compiler/rustc_middle/src/ty/context.rs45
-rw-r--r--tests/ui/parallel-rustc/cycle_crash.rs5
-rw-r--r--tests/ui/parallel-rustc/cycle_crash.stderr18
3 files changed, 53 insertions, 15 deletions
diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs
index d1079743004..1f0710e2415 100644
--- a/compiler/rustc_middle/src/ty/context.rs
+++ b/compiler/rustc_middle/src/ty/context.rs
@@ -1349,6 +1349,33 @@ pub struct GlobalCtxt<'tcx> {
 
     /// Stores memory for globals (statics/consts).
     pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
+
+    current_gcx: CurrentGcx,
+}
+
+impl<'tcx> GlobalCtxt<'tcx> {
+    /// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
+    /// `f`.
+    pub fn enter<F, R>(&'tcx self, f: F) -> R
+    where
+        F: FnOnce(TyCtxt<'tcx>) -> R,
+    {
+        let icx = tls::ImplicitCtxt::new(self);
+
+        // Reset `current_gcx` to `None` when we exit.
+        let _on_drop = defer(move || {
+            *self.current_gcx.value.write() = None;
+        });
+
+        // Set this `GlobalCtxt` as the current one.
+        {
+            let mut guard = self.current_gcx.value.write();
+            assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
+            *guard = Some(self as *const _ as *const ());
+        }
+
+        tls::enter_context(&icx, || f(icx.tcx))
+    }
 }
 
 /// This is used to get a reference to a `GlobalCtxt` if one is available.
@@ -1539,23 +1566,11 @@ impl<'tcx> TyCtxt<'tcx> {
             canonical_param_env_cache: Default::default(),
             data_layout,
             alloc_map: Lock::new(interpret::AllocMap::new()),
+            current_gcx,
         });
 
-        let icx = tls::ImplicitCtxt::new(&gcx);
-
-        // Reset `current_gcx` to `None` when we exit.
-        let _on_drop = defer(|| {
-            *current_gcx.value.write() = None;
-        });
-
-        // Set this `GlobalCtxt` as the current one.
-        {
-            let mut guard = current_gcx.value.write();
-            assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
-            *guard = Some(&gcx as *const _ as *const ());
-        }
-
-        tls::enter_context(&icx, || f(icx.tcx))
+        // This is a separate function to work around a crash with parallel rustc (#135870)
+        gcx.enter(f)
     }
 
     /// Obtain all lang items of this crate and all dependencies (recursively)
diff --git a/tests/ui/parallel-rustc/cycle_crash.rs b/tests/ui/parallel-rustc/cycle_crash.rs
new file mode 100644
index 00000000000..94ae11aef39
--- /dev/null
+++ b/tests/ui/parallel-rustc/cycle_crash.rs
@@ -0,0 +1,5 @@
+//@ compile-flags: -Z threads=2
+
+const FOO: usize = FOO; //~ERROR cycle detected when simplifying constant for the type system `FOO`
+
+fn main() {}
diff --git a/tests/ui/parallel-rustc/cycle_crash.stderr b/tests/ui/parallel-rustc/cycle_crash.stderr
new file mode 100644
index 00000000000..7af3b8ee532
--- /dev/null
+++ b/tests/ui/parallel-rustc/cycle_crash.stderr
@@ -0,0 +1,18 @@
+error[E0391]: cycle detected when simplifying constant for the type system `FOO`
+  --> $DIR/cycle_crash.rs:3:1
+   |
+LL | const FOO: usize = FOO;
+   | ^^^^^^^^^^^^^^^^
+   |
+note: ...which requires const-evaluating + checking `FOO`...
+  --> $DIR/cycle_crash.rs:3:20
+   |
+LL | const FOO: usize = FOO;
+   |                    ^^^
+   = note: ...which again requires simplifying constant for the type system `FOO`, completing the cycle
+   = note: cycle used when running analysis passes on this crate
+   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0391`.