Skip to content

Misaligned reference from drop field in packed struct #99838

Closed
@JakobDegen

Description

@JakobDegen

I tried this code:

struct U16(u16);

impl Drop for U16 {
    fn drop(&mut self) {
        println!("{:p}", self);
    }
}

struct HasDrop;

impl Drop for HasDrop {
    fn drop(&mut self) {}
}

#[repr(packed)]
struct Misalign(u8, Wrapper);

struct Wrapper {
    _a: U16,
    b: HasDrop,
}

fn main() {
    let m = Misalign(
        0,
        Wrapper {
            _a: U16(10),
            b: HasDrop,
        },
    );
    let _x = m.1.b;
}

I expected to see this happen: The reference is aligned

Instead, this happened:

$ rustc +nightly -Copt-level=3 test.rs
$ ./test
0x7ffc5e1dad79

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (2643b1646 2022-07-27)
binary: rustc
commit-hash: 2643b16468fda787470340890212591d8bc832b7
commit-date: 2022-07-27
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

@rustbot label I-unsound A-mir

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityregression-untriagedUntriaged performance or correctness regression.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions