Skip to content

offset_of returns a temporary #124478

@tgross35

Description

@tgross35
Contributor

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

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Apr 28, 2024
changed the title [-]`offset_of`[/-] [+]`offset_of` returns a temporary[/+] on Apr 28, 2024
tgross35

tgross35 commented on Apr 28, 2024

@tgross35
ContributorAuthor

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

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 it if it has been sufficiently triaged.
on Apr 28, 2024
GKFX

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.
asquared31415

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.

GKFX

GKFX commented on Apr 28, 2024

@GKFX
Contributor

@rustbot claim

added a commit that references this issue on Apr 28, 2024

Fix rust-lang#124478 - offset_of! returns a temporary

ca79086
added a commit that references this issue on Apr 29, 2024

Rollup merge of rust-lang#124484 - GKFX:offset_of_must_use, r=jieyouxu

0580588
added a commit that references this issue on Apr 29, 2024

Auto merge of rust-lang#124527 - jieyouxu:rollup-eslzncy, r=jieyouxu

a8a1d3a
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

    Participants

    @GKFX@tgross35@asquared31415@rustbot

    Issue actions

      `offset_of` returns a temporary · Issue #124478 · rust-lang/rust