Skip to content

Should unions use type based const qualification? #90268

Closed
@tmiasko

Description

@tmiasko

For example, after laundering interior mutability through a union, a resulting constant undergoes promotion:

#![feature(untagged_unions)]
use std::cell::Cell;

pub const CELL: Option<Cell<u32>> = {
    union U { i: u32, c: Cell<u32> }
    Some(unsafe { U { i: 0 }.c })
};

fn main() {
    let _ = &CELL;
}
error[E0080]: it is undefined behavior to use this value
 --> u.rs:9:1
  |
9 | fn main() {
  | ^^^^^^^^^ type validation failed at .<deref>.<enum-variant(Some)>.0.value: encountered `UnsafeCell` in a `const`
...

@rustbot label +A-const-eval +F-untagged_unions

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.F-untagged_unions`#![feature(untagged_unions)]`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions