about summary refs log tree commit diff
path: root/compiler/rustc_borrowck
diff options
context:
space:
mode:
authorlcnr <rust@lcnr.de>2025-05-02 16:24:28 +0000
committerlcnr <rust@lcnr.de>2025-05-02 18:45:28 +0000
commitffa7d1ee5dba9d0a9979d784a5c38e3a05a5ff9e (patch)
tree0bb55d6921f0f2b35f6784deb9233d6efb793167 /compiler/rustc_borrowck
parentf97b3c6044a67e0b5d0d0891ca3a6c5d982b2285 (diff)
downloadrust-ffa7d1ee5dba9d0a9979d784a5c38e3a05a5ff9e.tar.gz
rust-ffa7d1ee5dba9d0a9979d784a5c38e3a05a5ff9e.zip
borrowck nested items in dead code
Diffstat (limited to 'compiler/rustc_borrowck')
-rw-r--r--compiler/rustc_borrowck/src/lib.rs8
-rw-r--r--compiler/rustc_borrowck/src/root_cx.rs5
2 files changed, 12 insertions, 1 deletions
diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs
index 44af1b76539..f9d8d858f16 100644
--- a/compiler/rustc_borrowck/src/lib.rs
+++ b/compiler/rustc_borrowck/src/lib.rs
@@ -127,6 +127,14 @@ fn mir_borrowck(
         Ok(tcx.arena.alloc(opaque_types))
     } else {
         let mut root_cx = BorrowCheckRootCtxt::new(tcx, def);
+        // We need to manually borrowck all nested bodies from the HIR as
+        // we do not generate MIR for dead code. Not doing so causes us to
+        // never check closures in dead code.
+        let nested_bodies = tcx.nested_bodies_within(def);
+        for def_id in nested_bodies {
+            root_cx.get_or_insert_nested(def_id);
+        }
+
         let PropagatedBorrowCheckResults { closure_requirements, used_mut_upvars } =
             do_mir_borrowck(&mut root_cx, def, None).0;
         debug_assert!(closure_requirements.is_none());
diff --git a/compiler/rustc_borrowck/src/root_cx.rs b/compiler/rustc_borrowck/src/root_cx.rs
index 13daa5c7221..66b526fa02a 100644
--- a/compiler/rustc_borrowck/src/root_cx.rs
+++ b/compiler/rustc_borrowck/src/root_cx.rs
@@ -62,7 +62,10 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
         self.tainted_by_errors = Some(guar);
     }
 
-    fn get_or_insert_nested(&mut self, def_id: LocalDefId) -> &PropagatedBorrowCheckResults<'tcx> {
+    pub(super) fn get_or_insert_nested(
+        &mut self,
+        def_id: LocalDefId,
+    ) -> &PropagatedBorrowCheckResults<'tcx> {
         debug_assert_eq!(
             self.tcx.typeck_root_def_id(def_id.to_def_id()),
             self.root_def_id.to_def_id()