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

Unions with ZSTs are no longer usable #59126

Closed
mjbshaw opened this issue Mar 12, 2019 · 1 comment · Fixed by #59139
Closed

Unions with ZSTs are no longer usable #59126

mjbshaw opened this issue Mar 12, 2019 · 1 comment · Fixed by #59139
Assignees
Labels
A-const-eval Area: constant evaluation (mir interpretation) C-bug Category: This is a bug. P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mjbshaw
Copy link
Contributor

mjbshaw commented Mar 12, 2019

It looks like this was broken in the release of 1.32.0 (and is still broken, even on nightly) (checked on https://rust.godbolt.org/). The following example program:

union Uninit {
    _never_use: *const u8,
    uninit: (),
}

const UNINIT: Uninit = Uninit { uninit: () };

fn main() {
    let _ = UNINIT; // This line isn't actually necessary.
}

Fails to compile:

$ rustc --edition=2018 t.rs
error[E0080]: it is undefined behavior to use this value
 --> t.rs:6:1
  |
6 | const UNINIT: Uninit = Uninit { uninit: () };
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes
  |
  = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.

Interestingly, making the union #[repr(C)] fixes it.

My Rust version:

$ rustc --version
rustc 1.35.0-nightly (e68bf8ae1 2019-03-11)

I know union field accesses aren't stable, but I don't think this qualifies as a field access. And yes, I'm aware of MaybeUninit.

cc @RalfJung

@RalfJung
Copy link
Member

Interesting. Looks like a validation issue?

Cc @oli-obk

@oli-obk oli-obk added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. C-bug Category: This is a bug. A-const-eval Area: constant evaluation (mir interpretation) labels Mar 12, 2019
@pnkfelix pnkfelix added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. P-high High priority labels Mar 12, 2019
pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 13, 2019
…RalfJung

Unregress using scalar unions in constants.

fixes rust-lang#59126

r? @RalfJung

I also made a bunch of these invariants panic instead of report an error.
kennytm added a commit to kennytm/rust that referenced this issue Mar 15, 2019
…RalfJung

Unregress using scalar unions in constants.

fixes rust-lang#59126

r? @RalfJung

I also made a bunch of these invariants panic instead of report an error.
kennytm added a commit to kennytm/rust that referenced this issue Mar 16, 2019
…RalfJung

Unregress using scalar unions in constants.

fixes rust-lang#59126

r? @RalfJung

I also made a bunch of these invariants panic instead of report an error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: constant evaluation (mir interpretation) C-bug Category: This is a bug. P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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