Skip to content

Borrows of packed fields in const fn error for want of an unsafe block even when in an unsafe block #71181

New issue

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

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

Already on GitHub? # to your account

Closed
CAD97 opened this issue Apr 16, 2020 · 4 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Apr 16, 2020

Example

/// Safety: must be aligned to 8 (u64)
#[repr(packed)]
pub struct Struct {
    field: u64,
}

impl Struct {
    pub const fn field(&self) -> &u64 {
        unsafe { &self.field }
    }
}

Error

error[E0133]: borrow of packed field is unsafe and requires unsafe function or block
 --> src/lib.rs:9:18
  |
9 |         unsafe { &self.field }
  |                  ^^^^^^^^^^^ borrow of packed field
  |
  = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior

error: aborting due to previous error

Meta

rustc --version --verbose:

rustc 1.44.0-nightly (94d346360 2020-04-09)
binary: rustc
commit-hash: 94d346360da50f159e0dc777dc9bc3c5b6b51a00
commit-date: 2020-04-09
host: x86_64-pc-windows-msvc
release: 1.44.0-nightly
LLVM version: 9.0
@CAD97 CAD97 added the C-bug Category: This is a bug. label Apr 16, 2020
@RalfJung
Copy link
Member

Cc #27060 (the entire "packed fields reference" situation is still rather sad, this is probably related)

@CAD97
Copy link
Contributor Author

CAD97 commented Apr 16, 2020

#61365 is a similar "unsafe in const fn" issue where union field access had a similar unsatisfiable requirement for an unsafe block, but that one appears to have been incidentally fixed at some point.

@CAD97
Copy link
Contributor Author

CAD97 commented Apr 16, 2020

@rustbot modify labels: +A-const-fn +T-compiler

@rustbot rustbot added A-const-fn T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 16, 2020
@CAD97
Copy link
Contributor Author

CAD97 commented May 30, 2023

This was fixed at some point and now just produces the error

error[E0793]: reference to packed field is unaligned
 --> src/lib.rs:9:18
  |
9 |         unsafe { &self.field }
  |                  ^^^^^^^^^^^
  |
  = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
  = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

@CAD97 CAD97 closed this as completed May 30, 2023
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants