Skip to content

Compiler should reject union field definitions having default values #142555

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

Open
zopsicle opened this issue Jun 16, 2025 · 1 comment · May be fixed by #142570
Open

Compiler should reject union field definitions having default values #142555

zopsicle opened this issue Jun 16, 2025 · 1 comment · May be fixed by #142570
Labels
C-bug Category: This is a bug. F-default_field_values `#![feature(default_field_values)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zopsicle
Copy link
Contributor

I tried this code:

#![feature(default_field_values)]

union U
{
    x: i32 = 1,
    y: f32 = 2.,
}

I expected to see this happen:

The compiler rejects the program. The default field values RFC does not mention unions, Default cannot be derived for unions, and there appears to be no way to use these default field values.

Instead, this happened:

The compiler accepts the program.

Meta

rustc --version --verbose:

1.89.0-nightly
(2025-06-11 e703dff8fe220b78195c)
@zopsicle zopsicle added the C-bug Category: This is a bug. label Jun 16, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 16, 2025
@fmease fmease added F-default_field_values `#![feature(default_field_values)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 16, 2025
@workingjubilee
Copy link
Member

workingjubilee commented Jun 16, 2025

I wonder if we can enable deriving Default for unions if a default field value is given and that union variant selected via something like #[default]?

Nonetheless, it seems far better to simply yeet this unintended consequence and figure that sort of thing out later. ( Notably, this would require that additional #[default] extension to begin with. )

@jieyouxu jieyouxu linked a pull request Jun 16, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-default_field_values `#![feature(default_field_values)]` 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