Skip to content

Reject union default field values #142570

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

jieyouxu
Copy link
Member

@jieyouxu jieyouxu commented Jun 16, 2025

Fixes #142555.

The default_field_values RFC does not specify that default field values may be used on unions, and it's not clear how default field values may be used with unions without an design extension to the RFC. So, for now, reject trying to use default field values with unions.

Review notes

  • The first commit adds the union with default field values test case to tests/ui/structs/default-field-values/failures.rs, where unions with default field values are currently accepted.
  • The second commit rejects trying to supply default field values to union definitions.
  • When default_field_values feature gate is disabled, we show the feature gate error when the user tries to write unions with default field values. When the feature gate is enabled, we reject this usage with

    unions cannot have default field values

@rustbot label: +F-default_field_values

In particular, there should be no additional errors (default field
values for `union` fields are currently erroneously accepted).
@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-default_field_values `#![feature(default_field_values)]` labels Jun 16, 2025
@estebank
Copy link
Contributor

r? @estebank

r=me after addressing typo

@rustbot rustbot assigned estebank and unassigned petrochenkov Jun 16, 2025
@jieyouxu
Copy link
Member Author

jieyouxu commented Jun 16, 2025

Fixed typo.
@bors r=estebank rollup

@bors
Copy link
Collaborator

bors commented Jun 16, 2025

📌 Commit 2dd9cc1 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 17, 2025
Reject union default field values

Fixes rust-lang#142555.

The [`default_field_values` RFC][rfc] does not specify that default field values may be used on `union`s, and it's not clear how default field values may be used with `union`s without an design extension to the RFC. So, for now, reject trying to use default field values with `union`s.

### Review notes

- The first commit adds the `union` with default field values test case to `tests/ui/structs/default-field-values/failures.rs`, where `union`s with default field values are currently accepted.
- The second commit rejects trying to supply default field values to `union` definitions.
- When `default_field_values` feature gate is disabled, we show the feature gate error when the user tries to write `union`s with default field values. When the feature gate is enabled, we reject this usage with
   > unions cannot have default field values

`@rustbot` label: +F-default_field_values

[rfc]: https://rust-lang.github.io/rfcs/3681-default-field-values.html
bors added a commit that referenced this pull request Jun 17, 2025
Rollup of 11 pull requests

Successful merges:

 - #140809 (Reduce special casing for the panic runtime)
 - #141608 (Add support for repetition to `proc_macro::quote`)
 - #141864 (Handle win32 separator for cygwin paths)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142517 (Windows: Use anonymous pipes in Command)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)
 - #142585 (Update books)
 - #142586 (Fold unnecessary `visit_struct_field_def` in AstValidator)
 - #142595 (Revert overeager warning for misuse of `--print native-static-libs`)
 - #142598 (Set elf e_flags on ppc64 targets according to abi)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 17, 2025
Reject union default field values

Fixes rust-lang#142555.

The [`default_field_values` RFC][rfc] does not specify that default field values may be used on `union`s, and it's not clear how default field values may be used with `union`s without an design extension to the RFC. So, for now, reject trying to use default field values with `union`s.

### Review notes

- The first commit adds the `union` with default field values test case to `tests/ui/structs/default-field-values/failures.rs`, where `union`s with default field values are currently accepted.
- The second commit rejects trying to supply default field values to `union` definitions.
- When `default_field_values` feature gate is disabled, we show the feature gate error when the user tries to write `union`s with default field values. When the feature gate is enabled, we reject this usage with
   > unions cannot have default field values

``@rustbot`` label: +F-default_field_values

[rfc]: https://rust-lang.github.io/rfcs/3681-default-field-values.html
rust-bors bot added a commit that referenced this pull request Jun 17, 2025
Rollup of 13 pull requests

Successful merges:

 - #138538 (Make performance description of String::{insert,insert_str,remove} more precise)
 - #141946 (std: refactor explanation of `NonNull`)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142542 (Manually invalidate caches in SimplifyCfg.)
 - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)
 - #142585 (Update books)
 - #142586 (Fold unnecessary `visit_struct_field_def` in AstValidator)
 - #142587 (Make sure to propagate result from `visit_expr_fields`)
 - #142595 (Revert overeager warning for misuse of `--print native-static-libs`)
 - #142598 (Set elf e_flags on ppc64 targets according to abi)
 - #142601 (Add a comment to `FORMAT_VERSION`.)

r? `@ghost`
`@rustbot` modify labels: rollup
<!-- homu-ignore:start -->
[Create a similar rollup](https://bors.rust-lang.org/queue/rust?prs=138538,141946,142216,142542,142563,142570,142584,142585,142586,142587,142595,142598,142601)
<!-- homu-ignore:end -->
try-job: dist-apple-various
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 17, 2025
Reject union default field values

Fixes rust-lang#142555.

The [`default_field_values` RFC][rfc] does not specify that default field values may be used on `union`s, and it's not clear how default field values may be used with `union`s without an design extension to the RFC. So, for now, reject trying to use default field values with `union`s.

### Review notes

- The first commit adds the `union` with default field values test case to `tests/ui/structs/default-field-values/failures.rs`, where `union`s with default field values are currently accepted.
- The second commit rejects trying to supply default field values to `union` definitions.
- When `default_field_values` feature gate is disabled, we show the feature gate error when the user tries to write `union`s with default field values. When the feature gate is enabled, we reject this usage with
   > unions cannot have default field values

```@rustbot``` label: +F-default_field_values

[rfc]: https://rust-lang.github.io/rfcs/3681-default-field-values.html
bors added a commit that referenced this pull request Jun 17, 2025
Rollup of 10 pull requests

Successful merges:

 - #138538 (Make performance description of String::{insert,insert_str,remove} more precise)
 - #141946 (std: refactor explanation of `NonNull`)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142371 (avoid `&mut P<T>` in `visit_expr` etc methods)
 - #142377 (Try unremapping compiler sources)
 - #142517 (Windows: Use anonymous pipes in Command)
 - #142542 (Manually invalidate caches in SimplifyCfg.)
 - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-default_field_values `#![feature(default_field_values)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler should reject union field definitions having default values
5 participants