rust-lang / rust-lang/rust

`core::hint::must_use` returns a temporary

Open
#124,493 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes C-bug requires-nightly T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Originally noticed in https://github.com/rust-lang/rust/issues/124478#issue-2267570324, @GKFX identified it as a problem in must_use in https://github.com/rust-lang/rust/issues/124478#issuecomment-2081492040

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/lib.rs and the reported E0515 diagnostic, then trace how core::hint::must_use is handled in this expression. Done means the example no longer reports a reference to a temporary while preserving the intended constant behavior, with a regression test covering it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.