-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Associated type with a 'static
bound still captures lifetimes when using impl Trait
.
#106684
Comments
Bump. Also ran into this issue. |
I am hitting this too. Additionally, adding an associated type bound like trait Dummy {}
impl Dummy for () {}
trait AsStatic {
type Static: 'static + Dummy;
fn as_static(self) -> Self::Static;
}
struct Concrete<'a>(&'a ());
impl<'a> AsStatic for Concrete<'a> {
type Static = ();
fn as_static(self) -> Self::Static {}
}
fn ret_concrete(r: &()) -> Concrete {
Concrete(r)
}
trait Captures<U> {}
impl<T: ?Sized, U> Captures<U> for T {}
fn ret_impl(r: &()) -> impl Captures<&'_ ()> + AsStatic<Static: 'static> {
Concrete(r)
}
fn use_concrete(r: &()) -> impl Dummy {
ret_concrete(&r).as_static()
}
fn use_impl_rpit(r: &()) -> impl Dummy {
ret_impl(&r).as_static() // ERROR
}
type Ret = impl Dummy;
fn use_impl_tait(r: &()) -> Ret {
ret_impl(&r).as_static() // ERROR
} Error
Meta
|
The code from the issue description compiles in the next edition — Rust 2024 — which is not stable yet. That's because the capturing rules for have been adjusted. See rust-lang/rfcs#3498 for details. I can't reproduce your supposed The lifetime bound Further note that the code from the issue description can be “fixed” today by making If you enable the feature |
@kmicklas I'm not sure what you mean by “the 2024 edition lifetime capture rules are likely to make this even worse”. In Rust 2024, In Rust 2021, you can make In the majority of cases, we do want to capture all in-scope generic params. The plan is to accept, finalize & stabilize the feature To make your TAIT snippet compile, you need to apply the following change: - type Ret = impl Dummy;
+ type Ret<'a> = impl Dummy;
fn use_impl_tait(r: &()) -> Ret { Or under - type Ret = impl Dummy;
+ type Ret<'a> = impl Dummy;
- fn use_impl_tait(r: &()) -> Ret {
+ fn use_impl_tait(r: &()) -> Ret<'_> { |
@fmease Maybe example wasn't clear, but explicitly capturing the lifetimes is not a solution because the goal is to have a I've since realized there is a solution that was not obvious to me before - nested use of fn ret_impl(r: &()) -> impl Captures<&'_ ()> + AsStatic<Static = impl Dummy> {
Concrete(r)
} This achieves the goal of capturing lifetimes in the top level return type, but not its associated type. My naive intuition is still that the |
Current output:
|
Using a returned
impl Trait
for a trait with an associated type and not specifying the associated type leads to the following error:..even when
+ 'static
is specified, meaning no lifetimes should be captured. Adding#![feature(associated_type_bounds)]
and instead usingimpl Trait<AssocType: 'static>
as a return type solves the issue. I'm not sure if this is intended behavior - it seems pretty strange sinceAssocType
is defined with a'static
bound. See the code below for a reproducible example.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: