Skip to content

Generate synthetic region from impl even in closure body within an associated fn #102490

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

Merged
merged 1 commit into from
Oct 1, 2022
Merged
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: 3 additions & 5 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,15 +864,13 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
};

let tcx = self.infcx.tcx;
let body_parent_did = tcx.opt_parent(self.mir_def_id().to_def_id())?;
if tcx.parent(region.def_id) != body_parent_did
|| tcx.def_kind(body_parent_did) != DefKind::Impl
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically this check was too strict -- we don't care if the direct parent of the body is an impl, we only care that the region originates from the impl.

{
let region_parent = tcx.parent(region.def_id);
if tcx.def_kind(region_parent) != DefKind::Impl {
return None;
}

let mut found = false;
tcx.fold_regions(tcx.type_of(body_parent_did), |r: ty::Region<'tcx>, _| {
tcx.fold_regions(tcx.type_of(region_parent), |r: ty::Region<'tcx>, _| {
if *r == ty::ReEarlyBound(region) {
found = true;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/ui/borrowck/issue-102209.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::marker::PhantomData;

pub struct NfaBuilder<'brand> {
brand: PhantomData<&'brand mut &'brand mut ()>,
}

impl NfaBuilder<'_> {
pub fn with<R, F: FnOnce(NfaBuilder<'_>) -> R>(f: F) -> R {
Brand::with(|brand| {
f(Self { brand: brand.lt })
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
})
}
}

#[derive(Clone, Copy)]
pub struct Brand<'brand> {
lt: PhantomData<&'brand mut &'brand mut ()>,
}

impl Brand<'_> {
pub fn with<R, F: FnOnce(Brand<'_>) -> R>(f: F) -> R {
f(Self { lt: PhantomData })
}
}

fn main() {}
22 changes: 22 additions & 0 deletions src/test/ui/borrowck/issue-102209.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: lifetime may not live long enough
--> $DIR/issue-102209.rs:10:29
|
LL | impl NfaBuilder<'_> {
| -- lifetime `'2` appears in the `impl`'s self type
LL | pub fn with<R, F: FnOnce(NfaBuilder<'_>) -> R>(f: F) -> R {
LL | Brand::with(|brand| {
| ----- has type `Brand<'1>`
LL | f(Self { brand: brand.lt })
| ^^^^^^^^ this usage requires that `'1` must outlive `'2`

error: lifetime may not live long enough
--> $DIR/issue-102209.rs:10:29
|
LL | impl NfaBuilder<'_> {
| -- lifetime `'1` appears in the `impl`'s self type
...
LL | f(Self { brand: brand.lt })
| ^^^^^^^^ this usage requires that `'1` must outlive `'static`

error: aborting due to 2 previous errors