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

rework opaque type region inference #116891

Merged
merged 12 commits into from
Mar 28, 2024
Prev Previous commit
Next Next commit
reject external lifetimes as invalid arguments
  • Loading branch information
aliemjay committed Mar 28, 2024
commit 6b6ed2ea28e591ccae99048f226f98abfcd2e087
14 changes: 10 additions & 4 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_trait_selection::traits::ObligationCtxt;

use crate::session_diagnostics::LifetimeMismatchOpaqueParam;
use crate::session_diagnostics::NonGenericOpaqueTypeParam;
use crate::universal_regions::RegionClassification;

use super::RegionInferenceContext;

Expand Down Expand Up @@ -162,10 +163,15 @@ impl<'tcx> RegionInferenceContext<'tcx> {
NllRegionVariableOrigin::FreeRegion => self
.universal_regions
.universal_regions()
.filter(|&ur| self.universal_region_relations.equal(vid, ur))
// FIXME(aliemjay): universal regions with no `external_name`
// are extenal closure regions, which should be rejected eventually.
.find_map(|ur| self.definitions[ur].external_name),
.filter(|&ur| {
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
// See [rustc-dev-guide chapter] § "Closure restrictions".
!matches!(
self.universal_regions.region_classification(ur),
Some(RegionClassification::External)
)
})
.find(|&ur| self.universal_region_relations.equal(vid, ur))
.map(|ur| self.definitions[ur].external_name.unwrap()),
NllRegionVariableOrigin::Placeholder(placeholder) => {
Some(ty::Region::new_placeholder(infcx.tcx, placeholder))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![feature(type_alias_impl_trait)]

mod case1 {
type Opaque<'x> = impl Sized + 'x;
fn foo<'s>() -> Opaque<'s> {
let _ = || { let _: Opaque<'s> = (); };
//~^ ERROR expected generic lifetime parameter, found `'_`
}
}

mod case2 {
type Opaque<'x> = impl Sized + 'x;
fn foo<'s>() -> Opaque<'s> {
let _ = || -> Opaque<'s> {};
//~^ ERROR expected generic lifetime parameter, found `'_`
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/defined-in-closure-external-lifetime.rs:6:29
|
LL | type Opaque<'x> = impl Sized + 'x;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn foo<'s>() -> Opaque<'s> {
LL | let _ = || { let _: Opaque<'s> = (); };
| ^^^^^^^^^^

error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/defined-in-closure-external-lifetime.rs:14:34
|
LL | type Opaque<'x> = impl Sized + 'x;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn foo<'s>() -> Opaque<'s> {
LL | let _ = || -> Opaque<'s> {};
| ^^

error: aborting due to 2 previous errors

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