Skip to content

Commit

Permalink
Add semver check for unit structs becoming plain structs. (#3)
Browse files Browse the repository at this point in the history
* Add semver check for unit structs becoming plain structs.

* Fix test output format.

* Ensure the reported struct was not #[non_exhaustive].
  • Loading branch information
obi1kenobi authored Jul 19, 2022
1 parent c6b1eb5 commit dde1a79
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/regenerate_test_rustdocs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ features=(
'struct_pub_field_missing'
'enum_missing'
'enum_variant_missing'
'unit_struct_changed_kind'
)
for feat in "${features[@]}"
do
Expand Down
1 change: 1 addition & 0 deletions semver_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ struct_missing = []
struct_pub_field_missing = []
enum_missing = []
enum_variant_missing = []
unit_struct_changed_kind = []
14 changes: 14 additions & 0 deletions semver_tests/src/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ pub enum VariantWillBeRemoved {
#[cfg(not(feature = "enum_variant_missing"))]
Bar,
}

#[cfg(not(feature = "unit_struct_changed_kind"))]
pub struct UnitStructToPlain;

#[cfg(feature = "unit_struct_changed_kind")]
pub struct UnitStructToPlain {}

#[cfg(not(feature = "unit_struct_changed_kind"))]
#[non_exhaustive]
pub struct NonExhaustiveUnitStructToPlain;

#[cfg(feature = "unit_struct_changed_kind")]
#[non_exhaustive]
pub struct NonExhaustiveUnitStructToPlain {}
1 change: 1 addition & 0 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ mod tests {
enum_variant_missing,
struct_missing,
struct_pub_field_missing,
unit_struct_changed_kind,
struct_marked_non_exhaustive,
);
}
59 changes: 59 additions & 0 deletions src/queries/unit_struct_changed_kind.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
SemverQuery(
id: "unit_struct_changed_kind",
human_readable_name: "unit struct changed kind",
description: "A public struct that was previously a unit struct is now a plain struct. The unit struct was not marked #[non_exhaustive], so it could be constructed outside of the defining crate. Plain structs cannot be constructed using the syntax allowed for unit structs, so this is a major breaking change for code that depends on it.",
required_update: Major,

// TODO: Change the reference link once this cargo docs PR merges:
// https://github.com/rust-lang/cargo/pull/10871
//
// Change to this link:
// https://doc.rust-lang.org/cargo/reference/semver.html#struct-unit-to-normal
reference_link: Some("https://github.com/rust-lang/cargo/pull/10871"),
query: r#"
{
CrateDiff {
baseline {
item {
... on Struct {
visibility_limit @filter(op: "=", value: ["$public"]) @output
name @output @tag
struct_type @filter(op: "=", value: ["$unit"])
attrs @filter(op: "not_contains", value: ["$non_exhaustive"])
path {
path @output @tag
}
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
current @fold @transform(op: "count") @filter(op: ">=", value: ["$one"]) {
item {
... on Struct {
visibility_limit @filter(op: "=", value: ["$public"])
name @filter(op: "=", value: ["%name"])
struct_type @filter(op: "=", value: ["$plain"])
path {
path @filter(op: "=", value: ["%path"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"unit": "unit",
"plain": "plain",
"one": 1,
"non_exhaustive": "#[non_exhaustive]",
},
error_message: "A public unit struct has been changed to a normal (curly-braces) struct, which cannot be constructed using the same struct literal syntax.",
per_result_error_template: Some("struct {{name}}, previously in file {{span_filename}}:{{span_begin_line}}"),
)
9 changes: 9 additions & 0 deletions src/test_data/unit_struct_changed_kind.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"name": String("UnitStructToPlain"),
"path": List([String("semver_tests"), String("test_cases"), String("UnitStructToPlain")]),
"visibility_limit": String("public"),
"span_filename": String("src/test_cases/mod.rs"),
"span_begin_line": Uint64(28),
}
]

0 comments on commit dde1a79

Please sign in to comment.