Skip to content

Commit f1e691d

Browse files
committed
Auto merge of #86138 - FabianWolff:issue-85871, r=nikomatsakis
Check whether the closure's owner is an ADT in thir-unsafeck This pull request fixes #85871. The code in `rustc_mir_build/src/check_unsafety.rs` incorrectly assumes that a closure's owner always has a body, but only functions, closures, and constants have bodies, whereas a closure can also appear inside a struct or enum: ```rust struct S { arr: [(); match || 1 { _ => 42 }] } enum E { A([(); { || 1; 42 }]) } ``` This pull request fixes the resulting ICE by checking whether the closure's owner is an ADT and only deferring to `thir_check_unsafety(owner)` if it isn't.
2 parents 5a78340 + 433c1ae commit f1e691d

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
@@ -375,10 +375,15 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
375375
return;
376376
}
377377

378-
// Closures are handled by their parent function
378+
// Closures are handled by their owner, if it has a body
379379
if tcx.is_closure(def.did.to_def_id()) {
380-
tcx.ensure().thir_check_unsafety(tcx.hir().local_def_id_to_hir_id(def.did).owner);
381-
return;
380+
let owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
381+
let owner_hir_id = tcx.hir().local_def_id_to_hir_id(owner);
382+
383+
if tcx.hir().maybe_body_owned_by(owner_hir_id).is_some() {
384+
tcx.ensure().thir_check_unsafety(owner);
385+
return;
386+
}
382387
}
383388

384389
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)