-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
higher ranked lifetimes not recognized by type alias impl trait #96146
Comments
Amusingly you get a different error if the associated type actually uses the lifetime: #![feature(type_alias_impl_trait)]
trait Trait<'a> { type Out: 'a;}
impl<'a> Trait<'a> for i32 { type Out = &'a String;}
type A = impl for<'a> Trait<'a, Out = impl Sized + 'a>;
fn foo() -> A {
0_i32
} Fails with
|
I think this the right behavior. I started a thread on internals forum to discuss this. |
cc #96194 |
To rule out the ambiguity of nested impls as shown in #96194, I tried splitting the opaque type into two, inner and outer, and that showed an interesting pattern. If the inner type has a lifetime paramter #![feature(type_alias_impl_trait)]
trait Trait<'a> { type Out; }
impl<'a> Trait<'a> for () { type Out = (); }
type Inner<'a> = impl Sized;
fn outer_impl() -> impl for<'a> Trait<'a, Out = Inner<'a>> {} This yields the same error of this issue: And when the lifetime is used by the associated type: impl<'a> Trait<'a> for () { type Out = &'a (); } the compiler produces the same ICE in #95647 . This behavior is consistent regardless of of whether the inner impl have a lifetime bound Surprisingly, if #![feature(type_alias_impl_trait)]
type Inner<'a> = impl Sized;
fn outer_impl() -> impl for<'a> Fn(&'a ()) -> Inner<'a> { |x| x } If the #![feature(type_alias_impl_trait)]
trait Trait<'a> { type Out; }
impl<'a> Trait<'a> for () { type Out = &'a (); }
type Inner = impl Sized;
fn outer_impl() -> impl for<'a> Trait<'a, Out = Inner> {} This is rejected, but but behavior depends on the outer impl:
@rustbot claim |
this has been changed to explicitly forbid nested opaques from referencing higher ranked lifetimes: #![feature(type_alias_impl_trait)]
trait Trait<'a> { type Out: 'a;}
impl<'a> Trait<'a> for i32 { type Out = String;}
type A = impl for<'a> Trait<'a, Out = impl Sized + 'a>;
fn foo() -> A {
0_i32
} results in error: cannot capture late-bound lifetime in type alias impl trait
--> src/lib.rs:4:52
|
4 | type A = impl for<'a> Trait<'a, Out = impl Sized + 'a>;
| -- lifetime defined here ^^ we get a different error when using the #![feature(type_alias_impl_trait)]
type Inner<'a> = impl Sized;
fn outer_impl() -> impl for<'a> Fn(&'a ()) -> Inner<'a> { |x| x }
|
…r=lcnr test that we do not support higher-ranked regions in opaque type inference We already do all the right checks in `check_opaque_type_parameter_valid`, and we have done so since at least 2 years. I collected the tests from rust-lang#116935 and rust-lang#100503 and added some more cc rust-lang#96146 r? `@lcnr`
…r=lcnr test that we do not support higher-ranked regions in opaque type inference We already do all the right checks in `check_opaque_type_parameter_valid`, and we have done so since at least 2 years. I collected the tests from rust-lang#116935 and rust-lang#100503 and added some more cc rust-lang#96146 r? `@lcnr`
Rollup merge of rust-lang#121386 - oli-obk:no_higher_ranked_opaques, r=lcnr test that we do not support higher-ranked regions in opaque type inference We already do all the right checks in `check_opaque_type_parameter_valid`, and we have done so since at least 2 years. I collected the tests from rust-lang#116935 and rust-lang#100503 and added some more cc rust-lang#96146 r? `@lcnr`
The following snippet fails
The error is
Originally posted by @oli-obk in #96094 (comment)
The text was updated successfully, but these errors were encountered: