-
Notifications
You must be signed in to change notification settings - Fork 13.3k
rustc considers casting to violate lifetime analysis #19716
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
Comments
The result of a cast expression is an rvalue, and taking a reference to an rvalue is roughly equivalent to hoisting the result to a temporary variable that is only in scope for that statement and taking a reference to it. This makes the inner code block something akin to: {
ptrref1 = &ptr;
ptrref2 = { let tmp = ptr; &tmp as *const u8 };
} Since |
That it is the current behavior is distinct issue from whether or not it is desired behavior. It seems like there ought to be an easier way to perform a cast on something you need a reference to than transmuting it. It would be a different story if I could just directly cast the reference itself: ptrref2 = &ptr as &*const u8; |
In general a cast has to be an r-value, since it can change the in-memory representation of a value ( I suspect we don't want to have the l-value-ness of a cast expression depend on the types being cast, but others on the @rust-lang/lang team may think it is OK. |
I like the current behavior. It doesn't seem like casting the type of a variable and getting a direct reference to it in one action is so common, nor is transmute so inappropriate. In general, taking a reference to something and then changing the type of that something is pretty risky, since it can easily cause us to load or store values of the wrong size, or lead to variance problems, etc. I'm going to close this issue -- if you'd care to re-open, I'd suggest re-opening on the RFC repo, since I think that this sort of language change requires an RFC in any case. Thanks. |
In this case,
ptrref1
is able to be assigned to a ref of the pointer. However,ptrref2
cannot because of the cast. It appears the only way to work around this currently is withtransmute
:Try it yourself.
The text was updated successfully, but these errors were encountered: