Skip to content

offset_of returns a temporary #124478

Description

@tgross35

I tried this code:

struct Foo {
    a: u32,
}

impl Foo {
    fn bar() -> &'static [usize] {
        &[std::mem::offset_of!(Self, a)]
    }
}

Playground link

I expected to see this happen: no error

Instead, this happened:

error[E0515]: cannot return reference to temporary value
 --> src/lib.rs:7:9
  |
7 |         &[std::mem::offset_of!(Self, a)]
  |         ^-------------------------------
  |         ||
  |         |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. changed the title [-]`offset_of`[/-] [+]`offset_of` returns a temporary[/+] on Apr 28, 2024
  3. tgross35 commented on Apr 28, 2024

    @tgross35
    MemberAuthor

    I can't think of any reason why it would create the temporary here, considering you can assign the result to a const just fine.

    cc @GKFX who worked on this most recently

    @rustbot label +T-compiler +F-offset_of -needs-triage

  4. added
    F-offset_of`#![feature(offset_of)]`
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 28, 2024
  5. GKFX commented on Apr 28, 2024

    @GKFX
    Contributor

    It's the core::hint::must_use that does it: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bd518763786b97f87434d5c986cc52cf. builtin # offset_of alone doesn't cause the problem.

    I'm not sure what the original reason was for making this must_use, or whether it would be OK to just remove it? Wrapping the macro in const {} works, but that would interfere with array indexing in offset_of if that ever gets supported.

    let x = offset_of!(S, a[y]); // not const.
  6. asquared31415 commented on Apr 28, 2024

    @asquared31415
    Contributor

    making it must_use makes sense, but if it's not possible to keep that behavior and make it not a temporary, i think it's better UX to remove the must_use hint.

  7. GKFX commented on Apr 28, 2024

    @GKFX
    Contributor

    @rustbot claim

  8. added a commit that references this issue on Apr 28, 2024
    ca79086
  9. added a commit that references this issue on Apr 29, 2024
    0580588
  10. added a commit that references this issue on Apr 29, 2024
    a8a1d3a
  11. added a commit that references this issue on Apr 29, 2024
    07e8ef2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.F-offset_of`#![feature(offset_of)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions