Skip to content

borrowck nested items in dead code #140590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_borrowck/src/root_cx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ rustc_queries! {
}
}

query stalled_generators_within(
query nested_bodies_within(
key: LocalDefId
) -> &'tcx ty::List<LocalDefId> {
desc {
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,15 +684,17 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.opaque_types_defined_by(defining_anchor)
}

fn opaque_types_and_generators_defined_by(
fn opaque_types_and_coroutines_defined_by(
self,
defining_anchor: Self::LocalDefId,
) -> Self::LocalDefIds {
if self.next_trait_solver_globally() {
let coroutines_defined_by = self
.nested_bodies_within(defining_anchor)
.iter()
.filter(|def_id| self.is_coroutine(def_id.to_def_id()));
self.mk_local_def_ids_from_iter(
self.opaque_types_defined_by(defining_anchor)
.iter()
.chain(self.stalled_generators_within(defining_anchor)),
self.opaque_types_defined_by(defining_anchor).iter().chain(coroutines_defined_by),
)
} else {
self.opaque_types_defined_by(defining_anchor)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ty_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ mod implied_bounds;
mod instance;
mod layout;
mod needs_drop;
mod nested_bodies;
mod opaque_types;
mod representability;
pub mod sig_types;
mod stalled_generators;
mod structural_match;
mod ty;

Expand All @@ -51,5 +51,5 @@ pub fn provide(providers: &mut Providers) {
ty::provide(providers);
instance::provide(providers);
structural_match::provide(providers);
stalled_generators::provide(providers);
nested_bodies::provide(providers);
}
34 changes: 34 additions & 0 deletions compiler/rustc_ty_utils/src/nested_bodies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt};

fn nested_bodies_within<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx ty::List<LocalDefId> {
let body = tcx.hir_body_owned_by(item);
let mut collector =
NestedBodiesVisitor { tcx, root_def_id: item.to_def_id(), nested_bodies: vec![] };
collector.visit_body(body);
tcx.mk_local_def_ids(&collector.nested_bodies)
}

struct NestedBodiesVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
root_def_id: DefId,
nested_bodies: Vec<LocalDefId>,
}

impl<'tcx> Visitor<'tcx> for NestedBodiesVisitor<'tcx> {
fn visit_nested_body(&mut self, id: hir::BodyId) {
let body_def_id = self.tcx.hir_body_owner_def_id(id);
if self.tcx.typeck_root_def_id(body_def_id.to_def_id()) == self.root_def_id {
self.nested_bodies.push(body_def_id);
let body = self.tcx.hir_body(id);
self.visit_body(body);
}
}
}

pub(super) fn provide(providers: &mut Providers) {
*providers = Providers { nested_bodies_within, ..*providers };
}
54 changes: 0 additions & 54 deletions compiler/rustc_ty_utils/src/stalled_generators.rs

This file was deleted.

2 changes: 1 addition & 1 deletion compiler/rustc_type_ir/src/infer_ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<I: Interner> TypingMode<I> {
pub fn typeck_for_body(cx: I, body_def_id: I::LocalDefId) -> TypingMode<I> {
TypingMode::Analysis {
defining_opaque_types_and_generators: cx
.opaque_types_and_generators_defined_by(body_def_id),
.opaque_types_and_coroutines_defined_by(body_def_id),
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub trait Interner:

fn opaque_types_defined_by(self, defining_anchor: Self::LocalDefId) -> Self::LocalDefIds;

fn opaque_types_and_generators_defined_by(
fn opaque_types_and_coroutines_defined_by(
self,
defining_anchor: Self::LocalDefId,
) -> Self::LocalDefIds;
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/nll/nested-bodies-in-dead-code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@ edition: 2024

// Regression test for #140583. We want to borrowck nested
// bodies even if they are in dead code. While not necessary for
// soundness, it is desirable to error in such cases.

fn main() {
return;
|x: &str| -> &'static str { x };
//~^ ERROR lifetime may not live long enough
|| {
|| {
let temp = 1;
let p: &'static u32 = &temp;
//~^ ERROR `temp` does not live long enough
};
};
const {
let temp = 1;
let p: &'static u32 = &temp;
//~^ ERROR `temp` does not live long enough
};
async {
let temp = 1;
let p: &'static u32 = &temp;
//~^ ERROR `temp` does not live long enough
};
}
50 changes: 50 additions & 0 deletions tests/ui/nll/nested-bodies-in-dead-code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error: lifetime may not live long enough
--> $DIR/nested-bodies-in-dead-code.rs:9:33
|
LL | |x: &str| -> &'static str { x };
| - ^ returning this value requires that `'1` must outlive `'static`
| |
| let's call the lifetime of this reference `'1`

error[E0597]: `temp` does not live long enough
--> $DIR/nested-bodies-in-dead-code.rs:14:35
|
LL | let temp = 1;
| ---- binding `temp` declared here
LL | let p: &'static u32 = &temp;
| ------------ ^^^^^ borrowed value does not live long enough
| |
| type annotation requires that `temp` is borrowed for `'static`
LL |
LL | };
| - `temp` dropped here while still borrowed

error[E0597]: `temp` does not live long enough
--> $DIR/nested-bodies-in-dead-code.rs:20:31
|
LL | let temp = 1;
| ---- binding `temp` declared here
LL | let p: &'static u32 = &temp;
| ------------ ^^^^^ borrowed value does not live long enough
| |
| type annotation requires that `temp` is borrowed for `'static`
LL |
LL | };
| - `temp` dropped here while still borrowed

error[E0597]: `temp` does not live long enough
--> $DIR/nested-bodies-in-dead-code.rs:25:31
|
LL | let temp = 1;
| ---- binding `temp` declared here
LL | let p: &'static u32 = &temp;
| ------------ ^^^^^ borrowed value does not live long enough
| |
| type annotation requires that `temp` is borrowed for `'static`
LL |
LL | };
| - `temp` dropped here while still borrowed

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0597`.
Loading