Skip to content

Commit 433c1ae

Browse files
committed
Check whether the closure's owner has a body before deferring to it in thir-unsafeck
1 parent a50d721 commit 433c1ae

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,15 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
368368
return;
369369
}
370370

371-
// Closures are handled by their parent function
371+
// Closures are handled by their owner, if it has a body
372372
if tcx.is_closure(def.did.to_def_id()) {
373-
tcx.ensure().thir_check_unsafety(tcx.hir().local_def_id_to_hir_id(def.did).owner);
374-
return;
373+
let owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
374+
let owner_hir_id = tcx.hir().local_def_id_to_hir_id(owner);
375+
376+
if tcx.hir().maybe_body_owned_by(owner_hir_id).is_some() {
377+
tcx.ensure().thir_check_unsafety(owner);
378+
return;
379+
}
375380
}
376381

377382
let (thir, expr) = tcx.thir_body(def);
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Tests that no ICE occurs when a closure appears inside a node
2+
// that does not have a body when compiling with
3+
// compile-flags: -Zthir-unsafeck=yes
4+
// check-pass
5+
6+
#![allow(dead_code)]
7+
8+
struct Bug {
9+
inner: [(); match || 1 {
10+
_n => 42, // we may not call the closure here (E0015)
11+
}],
12+
}
13+
14+
enum E {
15+
V([(); { let _ = || 1; 42 }]),
16+
}
17+
18+
type Ty = [(); { let _ = || 1; 42 }];
19+
20+
fn main() {}

0 commit comments

Comments
 (0)