Skip to content

Compiler should reject union field definitions having default values #142555

Closed
@zopsicle

Description

@zopsicle
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)

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 16, 2025
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 16, 2025
workingjubilee

workingjubilee commented on Jun 16, 2025

@workingjubilee
Member

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. )

added a commit that references this issue on Jun 17, 2025

Rollup merge of rust-lang#142570 - jieyouxu:disunion, r=estebank

78e2974
added 2 commits that reference this issue on Jun 17, 2025

Rollup merge of rust-lang#142570 - jieyouxu:disunion, r=estebank

d4f23cd

Rollup merge of rust-lang#142570 - jieyouxu:disunion, r=estebank

0945b2b
added a commit that references this issue on Jun 17, 2025
52d8393
added a commit that references this issue on Jun 18, 2025
6e593bf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-default_field_values`#![feature(default_field_values)]`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

      Participants

      @fmease@workingjubilee@rustbot@zopsicle

      Issue actions

        Compiler should reject union field definitions having default values · Issue #142555 · rust-lang/rust