Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

offset_of returns a temporary #124478

Closed
tgross35 opened this issue Apr 28, 2024 · 4 comments · Fixed by #124484 or #124527
Closed

offset_of returns a temporary #124478

tgross35 opened this issue Apr 28, 2024 · 4 comments · Fixed by #124484 or #124527
Assignees
Labels
C-bug Category: This is a bug. F-offset_of `#![feature(offset_of)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tgross35
Copy link
Contributor

tgross35 commented Apr 28, 2024

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

@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 tgross35 changed the title offset_of offset_of returns a temporary Apr 28, 2024
@tgross35
Copy link
Contributor Author

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

@rustbot rustbot added F-offset_of `#![feature(offset_of)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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

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
Copy link
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
Copy link
Contributor

GKFX commented Apr 28, 2024

@rustbot claim

jieyouxu added a commit to jieyouxu/rust that referenced this issue Apr 29, 2024
Fix rust-lang#124478 - offset_of! returns a temporary

This was due to the must_use() call. Adding HIR's `OffsetOf` to the must_use checking within the compiler avoids this issue while maintaining the lint output.

Fixes rust-lang#124478. `@tgross35`
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 29, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#124269 (Pretty-print parenthesis around binary in postfix match)
 - rust-lang#124415 (Use probes more aggressively in new solver)
 - rust-lang#124475 (Remove direct dependencies on lazy_static, once_cell and byteorder)
 - rust-lang#124484 (Fix rust-lang#124478 - offset_of! returns a temporary)
 - rust-lang#124504 (Mark unions non-const-propagatable in `KnownPanicsLint` without calling layout)
 - rust-lang#124508 (coverage: Avoid hard-coded values when visiting logical ops)
 - rust-lang#124522 ([Refactor] Rename `Lint` and `LintGroup`'s `is_loaded` to `is_externally_loaded` )

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in ca79086 Apr 29, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 29, 2024
Rollup merge of rust-lang#124484 - GKFX:offset_of_must_use, r=jieyouxu

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

This was due to the must_use() call. Adding HIR's `OffsetOf` to the must_use checking within the compiler avoids this issue while maintaining the lint output.

Fixes rust-lang#124478. `@tgross35`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-offset_of `#![feature(offset_of)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants