reject external lifetimes as invalid arguments

This commit is contained in:
Ali MJ Al-Nasrawy 2023-10-19 10:42:25 +00:00
parent f4940e4d22
commit 6b6ed2ea28
3 changed files with 50 additions and 4 deletions

View File

@ -18,6 +18,7 @@
use crate::session_diagnostics::LifetimeMismatchOpaqueParam;
use crate::session_diagnostics::NonGenericOpaqueTypeParam;
use crate::universal_regions::RegionClassification;
use super::RegionInferenceContext;
@ -162,10 +163,15 @@ pub(crate) fn infer_opaque_types(
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| {
// 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))
}

View File

@ -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() {}

View File

@ -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`.