Skip to content

Commit a02e788

Browse files
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 #135870
2 parents 4d5c8bd + 6f543d5 commit a02e788

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

Diff for: compiler/rustc_middle/src/ty/context.rs

+30-15
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,33 @@ pub struct GlobalCtxt<'tcx> {
13491349

13501350
/// Stores memory for globals (statics/consts).
13511351
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
1352+
1353+
current_gcx: CurrentGcx,
1354+
}
1355+
1356+
impl<'tcx> GlobalCtxt<'tcx> {
1357+
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
1358+
/// `f`.
1359+
pub fn enter<F, R>(&'tcx self, f: F) -> R
1360+
where
1361+
F: FnOnce(TyCtxt<'tcx>) -> R,
1362+
{
1363+
let icx = tls::ImplicitCtxt::new(self);
1364+
1365+
// Reset `current_gcx` to `None` when we exit.
1366+
let _on_drop = defer(move || {
1367+
*self.current_gcx.value.write() = None;
1368+
});
1369+
1370+
// Set this `GlobalCtxt` as the current one.
1371+
{
1372+
let mut guard = self.current_gcx.value.write();
1373+
assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
1374+
*guard = Some(self as *const _ as *const ());
1375+
}
1376+
1377+
tls::enter_context(&icx, || f(icx.tcx))
1378+
}
13521379
}
13531380

13541381
/// This is used to get a reference to a `GlobalCtxt` if one is available.
@@ -1539,23 +1566,11 @@ impl<'tcx> TyCtxt<'tcx> {
15391566
canonical_param_env_cache: Default::default(),
15401567
data_layout,
15411568
alloc_map: Lock::new(interpret::AllocMap::new()),
1569+
current_gcx,
15421570
});
15431571

1544-
let icx = tls::ImplicitCtxt::new(&gcx);
1545-
1546-
// Reset `current_gcx` to `None` when we exit.
1547-
let _on_drop = defer(|| {
1548-
*current_gcx.value.write() = None;
1549-
});
1550-
1551-
// Set this `GlobalCtxt` as the current one.
1552-
{
1553-
let mut guard = current_gcx.value.write();
1554-
assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
1555-
*guard = Some(&gcx as *const _ as *const ());
1556-
}
1557-
1558-
tls::enter_context(&icx, || f(icx.tcx))
1572+
// This is a separate function to work around a crash with parallel rustc (#135870)
1573+
gcx.enter(f)
15591574
}
15601575

15611576
/// Obtain all lang items of this crate and all dependencies (recursively)

Diff for: tests/ui/parallel-rustc/cycle_crash.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@ compile-flags: -Z threads=2
2+
3+
const FOO: usize = FOO; //~ERROR cycle detected when simplifying constant for the type system `FOO`
4+
5+
fn main() {}

Diff for: tests/ui/parallel-rustc/cycle_crash.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0391]: cycle detected when simplifying constant for the type system `FOO`
2+
--> $DIR/cycle_crash.rs:3:1
3+
|
4+
LL | const FOO: usize = FOO;
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires const-evaluating + checking `FOO`...
8+
--> $DIR/cycle_crash.rs:3:20
9+
|
10+
LL | const FOO: usize = FOO;
11+
| ^^^
12+
= note: ...which again requires simplifying constant for the type system `FOO`, completing the cycle
13+
= note: cycle used when running analysis passes on this crate
14+
= 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
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)