Skip to content

Commit 345862d

Browse files
authored
Rollup merge of #87645 - LeSeulArtichaut:issue-87414, r=oli-obk
Properly find owner of closure in THIR unsafeck Previously, when encountering a closure in a constant, the THIR unsafeck gets invoked on the owner of the constant instead of the constant itself, producing cycles. Supersedes #87492. ```@FabianWolff``` thanks for your work on that PR, I copied your test file and added you as a co-author. Fixes #87414. r? ```@oli-obk```
2 parents e91405b + 1280423 commit 345862d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,10 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
599599

600600
// Closures are handled by their owner, if it has a body
601601
if tcx.is_closure(def.did.to_def_id()) {
602-
let owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
603-
let owner_hir_id = tcx.hir().local_def_id_to_hir_id(owner);
604-
605-
if tcx.hir().maybe_body_owned_by(owner_hir_id).is_some() {
606-
tcx.ensure().thir_check_unsafety(owner);
607-
return;
608-
}
602+
let hir = tcx.hir();
603+
let owner = hir.enclosing_body_owner(hir.local_def_id_to_hir_id(def.did));
604+
tcx.ensure().thir_check_unsafety(hir.local_def_id(owner));
605+
return;
609606
}
610607

611608
let (thir, expr) = tcx.thir_body(def);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #87414.
2+
3+
// check-pass
4+
// compile-flags: -Zthir-unsafeck
5+
6+
fn bad<T>() -> Box<dyn Iterator<Item = [(); { |x: u32| { x }; 4 }]>> { todo!() }
7+
8+
fn foo() -> [(); { |x: u32| { x }; 4 }] { todo!() }
9+
fn bar() { let _: [(); { |x: u32| { x }; 4 }]; }
10+
11+
// This one should not cause any errors either:
12+
unsafe fn unsf() {}
13+
fn bad2<T>() -> Box<dyn Iterator<Item = [(); { unsafe { || { unsf() } }; 4 }]>> { todo!() }
14+
15+
fn main() {}

0 commit comments

Comments
 (0)