Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

core::hint::must_use returns a temporary #124493

Open
tgross35 opened this issue Apr 28, 2024 · 4 comments
Open

core::hint::must_use returns a temporary #124493

tgross35 opened this issue Apr 28, 2024 · 4 comments
Labels
A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tgross35
Copy link
Contributor

Originally noticed in #124478 (comment), @GKFX identified it as a problem in must_use in #124478 (comment)

I tried this code:

#![feature(hint_must_use)]

fn foo() -> &'static [usize] {
    &[core::hint::must_use(42)]
}

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

Meta

rustc 1.79 nightly 2024-04-27

@tgross35 tgross35 added the C-bug Category: This is a bug. label Apr 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 28, 2024
@tgross35
Copy link
Contributor Author

tgross35 commented Apr 28, 2024

@rustbot label +T-compiler +A-lifetimes +requires-nightly -needs-triage

@rustbot rustbot added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 28, 2024
@GKFX
Copy link
Contributor

GKFX commented Apr 28, 2024

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.

@tgross35
Copy link
Contributor Author

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 tgross35 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
@tgross35
Copy link
Contributor Author

tgross35 commented May 3, 2024

Per @RalfJung in #124484 (comment), this may indeed be fixable by using #[rustc_promotable]

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants