-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Lifetime checker only checks the signature of a type and not its content #35852
Comments
Note that when HKLs/HKTs are implemented, this could eventually cause a soundness problem if left unfixed, as we will be able to add non-static associated type on static structs. |
cc @nikomatsakis Would lazy normalization result in |
fn example<'a>(data: &'a u32) {
let foo_impl = FooImpl { inside: &data };
let carrier: Carrier<FooImpl<'a>> = foo_impl.through_carrier();
must_be_static(carrier);
} The struct can't outlive its type parameter. I don't see the bug here. |
@arielb1 Is only variance of parameters taken into account for |
According to RFC 1214, a struct like |
@arielb1 And "components" refers to "type and lifetime parameters, according to their variance"? |
The rule is
It requires all type parameters and all lifetime parameters to outlive Also, the type parameter of |
As @arielb1 notes, this is very much by design (it is kind of crucial to having a usable outlives relation for projection types). |
That said, I do find some cases of errors unfortunate. It may be that we can evolve more here, though I'm not quire sure how (in some ways, I worry we are not always strict enough, as well, as it makes me uncomfortable to have lifetimes in types that can ever be "out of scope", and I think that may be possible in some particular scenarios). |
This fails to compile at the call to
must_be_static
because:data
does not live long enough, reference must be valid for the static lifetime.When we call
must_be_static(carrier)
, the type ofcarrier
isCarrier<FooImpl<'a>>
. Rustc presumably sees that there's a'a
and fails the compilation, since the parameter ofmust_be_static
must be static.However the inside of
Carrier<FooImpl<'a>>
is actually just au32
, which is static. Even though the type signature contains a lifetime, the actual content of the struct does not. Therefore this code should in theory compile.The text was updated successfully, but these errors were encountered: