Skip to content

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

Open

Description

@tgross35

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

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 28, 2024
  2. tgross35 commented on Apr 28, 2024

    @tgross35
    MemberAuthor

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

  3. added
    A-lifetimesArea: Lifetimes / regions
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 28, 2024
  4. GKFX commented on Apr 28, 2024

    @GKFX
    Contributor

    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.

  5. tgross35 commented on Apr 28, 2024

    @tgross35
    MemberAuthor

    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.

  6. tgross35 commented on May 3, 2024

    @tgross35
    MemberAuthor

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

  7. dlight commented on Apr 26, 2025

    @dlight

    Maybe turning core::hint::must_use into a macro could fix this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions