Skip to content

"nonexistent struct field" warning when type-punning in a const definition #48746

Closed
@sfackler

Description

@sfackler
#[repr(C)]
union Foo {
    a: u32,
    b: i32,
}

const FOO: i32 = unsafe { Foo { a: 15 }.b };

fn main() {
    println!("{}", FOO);
}
warning: constant evaluation error: nonexistent struct field
 --> src/main.rs:8:18
  |
8 | const FOO: i32 = unsafe { Foo { a: 15 }.b };
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(const_err)] on by default

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-enhancementCategory: An issue proposing an enhancement or a PR with one.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