You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I expected to see this happen: no error, the value is a constant
Instead, this happened:
error[E0515]: cannot return reference to temporary value
--> src/lib.rs:4:5
|
4 | &[core::hint::must_use(42)]
| ^--------------------------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
For more information about this error, try `rustc --explain E0515`.
error: could not compile `playground` (lib) due to 1 previous error
Is this likely to be fixable with must_use being a function? As I understand it, foo only knows the signature of must_use, not its implementation, so cannot know that must_use is an identity function and cannot avoid a temporary. It would be ideal if the #[must_use] attribute applied to expressions.
A magic #[rustc_identity_function] attribute on must_use could be used to let the compiler know in foo that it can avoid the temporary, but seems like overkill.
git grep 'hint.*must_use' doesn't show any other usage (besides offset_of) of core::hint::must_use which makes me wonder how useful it is. Tagging #94745.
Yup you are right, I guess we can't actually do anything with the function signature so I will close this. Agreed that it may not be overly useful in its current form.
Activity
tgross35 commentedon Apr 28, 2024
@rustbot label +T-compiler +A-lifetimes +requires-nightly -needs-triage
GKFX commentedon Apr 28, 2024
Is this likely to be fixable with
must_usebeing a function? As I understand it,fooonly knows the signature ofmust_use, not its implementation, so cannot know thatmust_useis an identity function and cannot avoid a temporary. It would be ideal if the#[must_use]attribute applied to expressions.A magic
#[rustc_identity_function]attribute onmust_usecould be used to let the compiler know infoothat it can avoid the temporary, but seems like overkill.git grep 'hint.*must_use'doesn't show any other usage (besides offset_of) ofcore::hint::must_usewhich makes me wonder how useful it is. Tagging #94745.tgross35 commentedon Apr 28, 2024
Yup you are right, I guess we can't actually do anything with the function signature so I will close this. Agreed that it may not be overly useful in its current form.
tgross35 commentedon May 3, 2024
Per @RalfJung in #124484 (comment), this may indeed be fixable by using
#[rustc_promotable]dlight commentedon Apr 26, 2025
Maybe turning
core::hint::must_useinto a macro could fix this issue?