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

Add check for adding #[non_exhaustive] causing breakage. #4

Merged
merged 6 commits into from
Jul 19, 2022

Conversation

obi1kenobi
Copy link
Owner

Per this page in the Rust language reference, it seems that adding #[non_exhaustive] to a non-unit struct that has no private fields is a major breaking change.

This is because structs with no private fields can have struct literal be used to construct them outside their own crate, but the addition of #[non_exhaustive] will prevent struct literals from being used to construct them outside their own crate. Unit structs are not affected since, based on a quick test on rustc 1.62.0 (a8314ef7d 2022-06-27), #[non_exhaustive] is ignored on unit structs and they can still be constructed externally using a struct literal.

Will link this to a PR against the cargo semver reference page when I've had a chance to write a PR for it.

@obi1kenobi obi1kenobi marked this pull request as draft July 16, 2022 02:18
@obi1kenobi obi1kenobi marked this pull request as ready for review July 18, 2022 15:52
@obi1kenobi obi1kenobi merged commit c6b1eb5 into main Jul 19, 2022
@obi1kenobi obi1kenobi deleted the struct_marked_non_exhaustive branch July 19, 2022 15:20
obi1kenobi pushed a commit that referenced this pull request Feb 25, 2023
* Add failing Windows test to CI (#2)

* Remove trailing whitespaces.

* Explain removing the index

* Add caching (#3)

* Convert slashes to back-slashes (#4)

* Bump crates-index version to 0.19.6
bors added a commit to rust-lang/cargo that referenced this pull request May 1, 2023
Document that adding `#[non_exhaustive]` on existing items is breaking.

### What does this PR try to resolve?

Adding `#[non_exhaustive]` to an existing struct, enum, or variant is almost always a breaking change and requires a major version bump for semver purposes. This PR adds a section to the semver reference page that describes this and provides examples showing how `#[non_exhaustive]` can break code.

### Additional information

Adding `#[non_exhaustive]` to a unit struct currently has no effect on whether that struct can be constructed in downstream crates. This is inconsistent with the behavior of `#[non_exhaustive]` on unit enum variants, which may not be constructed outside their own crate. This might be due to a similar underlying cause as: rust-lang/rust#78586

The confusing "variant is private" error messages for non-exhaustive unit and tuple variants are a known issue tracked in: rust-lang/rust#82788

Checking for the struct portion of this semver rule is done in: obi1kenobi/cargo-semver-checks#4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant