Skip to content
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

Do not substitute nonexistent lifetime in RPIT in recursive function #110844

Closed
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
10 changes: 3 additions & 7 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
Some(region) => {
let vid = self.universal_regions.to_region_vid(region);
subst_regions.push(vid);
region
Some(region)
}
None => {
subst_regions.push(vid);
ty::Region::new_error_with_message(
infcx.tcx,
concrete_type.span,
"opaque type with non-universal region substs",
)
None
}
}
};
Expand All @@ -118,7 +114,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
return region;
}
let vid = self.to_region_vid(region);
to_universal_region(vid, &mut subst_regions)
to_universal_region(vid, &mut subst_regions).unwrap_or(region)
});
debug!(?universal_substs);
debug!(?subst_regions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(unconditional_recursion)]

fn foo<'a: 'a>() -> impl Sized {
let _: *mut &'a () = foo::<'a>();
//~^ ERROR
loop {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
--> $DIR/recursive-impl-trait-type-direct-with-earlybound-lifetime-2.rs:4:12
|
LL | fn foo<'a: 'a>() -> impl Sized {
| -- ---------- opaque type defined here
| |
| hidden type `*mut &'a ()` captures the lifetime `'a` as defined here
LL | let _: *mut &'a () = foo::<'a>();
| ^^^^^^^^^^^
|
help: to declare that `impl Sized` captures `'a`, you can add an explicit `'a` lifetime bound
|
LL | fn foo<'a: 'a>() -> impl Sized + 'a {
| ++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0700`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// check-pass

#![allow(unconditional_recursion)]

fn foo<'a: 'a>() -> impl Sized + 'a {
let _: *mut &'a () = foo::<'a>();
loop {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(unconditional_recursion)]

trait Trait<'a, 'b> {}
impl<'a, 'b, T> Trait<'a, 'b> for T {}

fn foo<'a: 'a, 'b: 'b>() -> impl Trait<'a, 'b> {
let _: *mut &'a () = foo::<'a, 'b>();
let _: *mut &'b () = foo::<'a, 'b>();
//~^ ERROR
loop {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: concrete type differs from previous defining opaque type use
--> $DIR/recursive-impl-trait-type-direct-with-earlybound-lifetime-4.rs:8:12
|
LL | let _: *mut &'b () = foo::<'a, 'b>();
| ^^^^^^^^^^^ expected `*mut &'a ()`, got `*mut &'b ()`
|
note: previous use here
--> $DIR/recursive-impl-trait-type-direct-with-earlybound-lifetime-4.rs:7:12
|
LL | let _: *mut &'a () = foo::<'a, 'b>();
| ^^^^^^^^^^^

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// check-pass

#![allow(unconditional_recursion)]

fn foo<'a: 'a>() -> impl Sized {
let _: () = foo::<'a>();
loop {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// check-pass

#![allow(unconditional_recursion)]

fn test<'a>() -> impl Sized + 'a {
let r: i32 = test();
r
}

fn main() {}