-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
GAT type Assoc<T: ?Sized>
implicitly requires Self
to be 'static
#131008
Comments
This might be helpful: https://blog.rust-lang.org/2022/10/28/gats-stabilization.html |
This is not a bug, but the error message here is terrible. Due to how trait object lifetime elision works,
So with all inferred lifetimes annotated explicitly, your program becomes this: trait Layout {}
trait Desc {
type Children<A: ?Sized>;
fn stage<'a>(_children: &'a Self::Children<dyn Layout + 'a>);
}
fn stage<D: Desc>(children: D::Children<dyn Layout + 'static>) {
// here we unify the argument `&'b D::Children<dyn Layout + 'static>`
// with the function parameter `&'a D::Children<dyn Layout + 'a>`
// and end up with `'b == 'a == 'static`.
D::stage(&/*'b */ children);
} So the borrowchecker thinks that the borrow must be valid for (In this example it is also not possible to shorten the trait object lifetime from The diagnostic here should probably mention default trait object lifetimes and offer the two workarounds that you already managed to come up with. @rustbot label -C-bug +A-diagnostics +A-borrow-checker +A-trait-objects +D-terse -needs-triage |
@lukas-code, thank you for the explanation! Although I don't understand why |
Yeah and struct Wrapper<T>(T);
impl<T> Desc for Wrapper<T> {
type Children<A: ?Sized> = Wrapper<T>;
} In that case when the dependant calls your function |
I tried this code:
This doesn't compile with two errors, and I can't explain why:
Why is the
D
required to be'static
here?Why does the
&children
need to have a'static
lifetime here as well?Here are variations that do compile, but I also can't explain why they compile:
Adding `dyn Layout + 'static` in the trait definition
Adding `dyn Layout + '_` in the trait definition
This variation doesn't compile, but it removes the
`D` must be valid for the static lifetime
error and I also don't understand why that is:There is definitely something implicit going on here which I don't know. Some helpful people suggested this may be related to #87479, but I don't see how.
@nikomatsakis do you have an idea if this is related? Is this some compiler bug or smth not documented?
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: