Skip to content

Commit 808f197

Browse files
authored
Rollup merge of #102490 - compiler-errors:closure-body-impl-lifetime, r=cjgillot
Generate synthetic region from `impl` even in closure body within an associated fn Fixes #102209
2 parents 05b9f0e + c1c3dac commit 808f197

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -864,15 +864,13 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
864864
};
865865

866866
let tcx = self.infcx.tcx;
867-
let body_parent_did = tcx.opt_parent(self.mir_def_id().to_def_id())?;
868-
if tcx.parent(region.def_id) != body_parent_did
869-
|| tcx.def_kind(body_parent_did) != DefKind::Impl
870-
{
867+
let region_parent = tcx.parent(region.def_id);
868+
if tcx.def_kind(region_parent) != DefKind::Impl {
871869
return None;
872870
}
873871

874872
let mut found = false;
875-
tcx.fold_regions(tcx.type_of(body_parent_did), |r: ty::Region<'tcx>, _| {
873+
tcx.fold_regions(tcx.type_of(region_parent), |r: ty::Region<'tcx>, _| {
876874
if *r == ty::ReEarlyBound(region) {
877875
found = true;
878876
}

src/test/ui/borrowck/issue-102209.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::marker::PhantomData;
2+
3+
pub struct NfaBuilder<'brand> {
4+
brand: PhantomData<&'brand mut &'brand mut ()>,
5+
}
6+
7+
impl NfaBuilder<'_> {
8+
pub fn with<R, F: FnOnce(NfaBuilder<'_>) -> R>(f: F) -> R {
9+
Brand::with(|brand| {
10+
f(Self { brand: brand.lt })
11+
//~^ ERROR lifetime may not live long enough
12+
//~| ERROR lifetime may not live long enough
13+
})
14+
}
15+
}
16+
17+
#[derive(Clone, Copy)]
18+
pub struct Brand<'brand> {
19+
lt: PhantomData<&'brand mut &'brand mut ()>,
20+
}
21+
22+
impl Brand<'_> {
23+
pub fn with<R, F: FnOnce(Brand<'_>) -> R>(f: F) -> R {
24+
f(Self { lt: PhantomData })
25+
}
26+
}
27+
28+
fn main() {}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/issue-102209.rs:10:29
3+
|
4+
LL | impl NfaBuilder<'_> {
5+
| -- lifetime `'2` appears in the `impl`'s self type
6+
LL | pub fn with<R, F: FnOnce(NfaBuilder<'_>) -> R>(f: F) -> R {
7+
LL | Brand::with(|brand| {
8+
| ----- has type `Brand<'1>`
9+
LL | f(Self { brand: brand.lt })
10+
| ^^^^^^^^ this usage requires that `'1` must outlive `'2`
11+
12+
error: lifetime may not live long enough
13+
--> $DIR/issue-102209.rs:10:29
14+
|
15+
LL | impl NfaBuilder<'_> {
16+
| -- lifetime `'1` appears in the `impl`'s self type
17+
...
18+
LL | f(Self { brand: brand.lt })
19+
| ^^^^^^^^ this usage requires that `'1` must outlive `'static`
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)