From ef02b2d28b11723fe3c1d7956bdfd8998495a4a8 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:53:52 -0500 Subject: [PATCH 1/2] Add the `type_allows_fewer_const_generic_params` lint. (#1054) It catches structs/enums/unions that no longer support the previous number of const generic parameters. --- ...type_allows_fewer_const_generic_params.ron | 76 +++++++++++++++++++ src/query.rs | 1 + .../new/Cargo.toml | 7 ++ .../new/src/lib.rs | 11 +++ .../old/Cargo.toml | 7 ++ .../old/src/lib.rs | 12 +++ ...ype_allows_fewer_const_generic_params.snap | 63 +++++++++++++++ 7 files changed, 177 insertions(+) create mode 100644 src/lints/type_allows_fewer_const_generic_params.ron create mode 100644 test_crates/type_allows_fewer_const_generic_params/new/Cargo.toml create mode 100644 test_crates/type_allows_fewer_const_generic_params/new/src/lib.rs create mode 100644 test_crates/type_allows_fewer_const_generic_params/old/Cargo.toml create mode 100644 test_crates/type_allows_fewer_const_generic_params/old/src/lib.rs create mode 100644 test_outputs/query_execution/type_allows_fewer_const_generic_params.snap diff --git a/src/lints/type_allows_fewer_const_generic_params.ron b/src/lints/type_allows_fewer_const_generic_params.ron new file mode 100644 index 00000000..ff2b77bb --- /dev/null +++ b/src/lints/type_allows_fewer_const_generic_params.ron @@ -0,0 +1,76 @@ +SemverQuery( + id: "type_allows_fewer_const_generic_params", + human_readable_name: "type now allows fewer const generic parameters", + description: "A type now allows fewer const generic parameters than before.", + required_update: Major, + lint_level: Deny, + reference_link: Some("https://doc.rust-lang.org/reference/items/generics.html#const-generics"), + query: r#" + { + CrateDiff { + baseline { + item { + ... on ImplOwner { + visibility_limit @filter(op: "=", value: ["$public"]) + name @output + owner_type: __typename @tag @output + + importable_path { + path @tag @output + public_api @filter(op: "=", value: ["$true"]) + } + + generic_parameter @fold + @transform(op: "count") + @tag(name: "old_allowed_const_count") + @output(name: "old_allowed_const_count") { + ... on GenericConstParameter { + old_allowed_consts: name @output + } + } + } + } + } + current { + item { + ... on ImplOwner { + visibility_limit @filter(op: "=", value: ["$public"]) @output + __typename @filter(op: "=", value: ["%owner_type"]) + + importable_path { + path @filter(op: "=", value: ["%path"]) + public_api @filter(op: "=", value: ["$true"]) + } + + generic_parameter @fold + @transform(op: "count") + @filter(op: "<", value: ["%old_allowed_const_count"]) + @output(name: "new_allowed_const_count") { + ... on GenericConstParameter { + new_allowed_consts: name @output + } + } + + span_: span @optional { + filename @output + begin_line @output + } + } + } + } + } + }"#, + arguments: { + "public": "public", + "true": true, + }, + error_message: "A type now allows fewer const generic parameters than it used to. Uses of this type that supplied all previously-supported const generics will be broken.", + per_result_error_template: Some("{{owner_type}} {{name}} allows {{old_allowed_const_count}} -> {{new_allowed_const_count}} const generics in {{span_filename}}:{{span_begin_line}}"), + // TODO: see https://github.com/obi1kenobi/cargo-semver-checks/blob/main/CONTRIBUTING.md#adding-a-witness + // for information about this field. + // + // The witness would be a type ascription with the old number + // of allowed const generics (including ones that provided default values), + // which will be too many generics for the new definition. + witness: None, +) diff --git a/src/query.rs b/src/query.rs index 6d5be258..18a119da 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1218,6 +1218,7 @@ add_lints!( trait_unsafe_added, trait_unsafe_removed, tuple_struct_to_plain_struct, + type_allows_fewer_const_generic_params, type_marked_deprecated, type_mismatched_generic_lifetimes, type_requires_more_const_generic_params, diff --git a/test_crates/type_allows_fewer_const_generic_params/new/Cargo.toml b/test_crates/type_allows_fewer_const_generic_params/new/Cargo.toml new file mode 100644 index 00000000..f85b45d2 --- /dev/null +++ b/test_crates/type_allows_fewer_const_generic_params/new/Cargo.toml @@ -0,0 +1,7 @@ +[package] +publish = false +name = "type_allows_fewer_const_generic_params" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/type_allows_fewer_const_generic_params/new/src/lib.rs b/test_crates/type_allows_fewer_const_generic_params/new/src/lib.rs new file mode 100644 index 00000000..0fad9de2 --- /dev/null +++ b/test_crates/type_allows_fewer_const_generic_params/new/src/lib.rs @@ -0,0 +1,11 @@ +pub struct Example { + data: [i64; N], +} + +pub enum NotGenericAnymore { + First([i64; 16]), +} + +pub union NotGenericEither { + left: std::mem::ManuallyDrop<[T; 4]>, +} diff --git a/test_crates/type_allows_fewer_const_generic_params/old/Cargo.toml b/test_crates/type_allows_fewer_const_generic_params/old/Cargo.toml new file mode 100644 index 00000000..f85b45d2 --- /dev/null +++ b/test_crates/type_allows_fewer_const_generic_params/old/Cargo.toml @@ -0,0 +1,7 @@ +[package] +publish = false +name = "type_allows_fewer_const_generic_params" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/type_allows_fewer_const_generic_params/old/src/lib.rs b/test_crates/type_allows_fewer_const_generic_params/old/src/lib.rs new file mode 100644 index 00000000..4b3cea3d --- /dev/null +++ b/test_crates/type_allows_fewer_const_generic_params/old/src/lib.rs @@ -0,0 +1,12 @@ +pub struct Example { + data: [i64; N], + data2: [i64; M], +} + +pub enum NotGenericAnymore { + First([i64; N]), +} + +pub union NotGenericEither { + left: std::mem::ManuallyDrop<[T; N]>, +} diff --git a/test_outputs/query_execution/type_allows_fewer_const_generic_params.snap b/test_outputs/query_execution/type_allows_fewer_const_generic_params.snap new file mode 100644 index 00000000..d38ac681 --- /dev/null +++ b/test_outputs/query_execution/type_allows_fewer_const_generic_params.snap @@ -0,0 +1,63 @@ +--- +source: src/query.rs +expression: "&query_execution_results" +snapshot_kind: text +--- +{ + "./test_crates/type_allows_fewer_const_generic_params/": [ + { + "name": String("Example"), + "new_allowed_const_count": Uint64(1), + "new_allowed_consts": List([ + String("N"), + ]), + "old_allowed_const_count": Uint64(2), + "old_allowed_consts": List([ + String("N"), + String("M"), + ]), + "owner_type": String("Struct"), + "path": List([ + String("type_allows_fewer_const_generic_params"), + String("Example"), + ]), + "span_begin_line": Uint64(1), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "name": String("NotGenericAnymore"), + "new_allowed_const_count": Uint64(0), + "new_allowed_consts": List([]), + "old_allowed_const_count": Uint64(1), + "old_allowed_consts": List([ + String("N"), + ]), + "owner_type": String("Enum"), + "path": List([ + String("type_allows_fewer_const_generic_params"), + String("NotGenericAnymore"), + ]), + "span_begin_line": Uint64(5), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "name": String("NotGenericEither"), + "new_allowed_const_count": Uint64(0), + "new_allowed_consts": List([]), + "old_allowed_const_count": Uint64(1), + "old_allowed_consts": List([ + String("N"), + ]), + "owner_type": String("Union"), + "path": List([ + String("type_allows_fewer_const_generic_params"), + String("NotGenericEither"), + ]), + "span_begin_line": Uint64(9), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], +} From e2159f8d30f51ea18c7a6c69c19704d40e2a7271 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:58:50 -0500 Subject: [PATCH 2/2] Rename test crate to match lint name. (#1053) --- .../new/Cargo.toml | 2 +- .../new/src/lib.rs | 0 .../old/Cargo.toml | 2 +- .../old/src/lib.rs | 0 .../type_requires_more_const_generic_params.snap | 8 ++++---- 5 files changed, 6 insertions(+), 6 deletions(-) rename test_crates/{type_requires_more_generic_consts => type_requires_more_const_generic_params}/new/Cargo.toml (61%) rename test_crates/{type_requires_more_generic_consts => type_requires_more_const_generic_params}/new/src/lib.rs (100%) rename test_crates/{type_requires_more_generic_consts => type_requires_more_const_generic_params}/old/Cargo.toml (61%) rename test_crates/{type_requires_more_generic_consts => type_requires_more_const_generic_params}/old/src/lib.rs (100%) diff --git a/test_crates/type_requires_more_generic_consts/new/Cargo.toml b/test_crates/type_requires_more_const_generic_params/new/Cargo.toml similarity index 61% rename from test_crates/type_requires_more_generic_consts/new/Cargo.toml rename to test_crates/type_requires_more_const_generic_params/new/Cargo.toml index 0fa29005..f9012e00 100644 --- a/test_crates/type_requires_more_generic_consts/new/Cargo.toml +++ b/test_crates/type_requires_more_const_generic_params/new/Cargo.toml @@ -1,6 +1,6 @@ [package] publish = false -name = "type_requires_more_generic_consts" +name = "type_requires_more_const_generic_params" version = "0.1.0" edition = "2021" diff --git a/test_crates/type_requires_more_generic_consts/new/src/lib.rs b/test_crates/type_requires_more_const_generic_params/new/src/lib.rs similarity index 100% rename from test_crates/type_requires_more_generic_consts/new/src/lib.rs rename to test_crates/type_requires_more_const_generic_params/new/src/lib.rs diff --git a/test_crates/type_requires_more_generic_consts/old/Cargo.toml b/test_crates/type_requires_more_const_generic_params/old/Cargo.toml similarity index 61% rename from test_crates/type_requires_more_generic_consts/old/Cargo.toml rename to test_crates/type_requires_more_const_generic_params/old/Cargo.toml index 0fa29005..f9012e00 100644 --- a/test_crates/type_requires_more_generic_consts/old/Cargo.toml +++ b/test_crates/type_requires_more_const_generic_params/old/Cargo.toml @@ -1,6 +1,6 @@ [package] publish = false -name = "type_requires_more_generic_consts" +name = "type_requires_more_const_generic_params" version = "0.1.0" edition = "2021" diff --git a/test_crates/type_requires_more_generic_consts/old/src/lib.rs b/test_crates/type_requires_more_const_generic_params/old/src/lib.rs similarity index 100% rename from test_crates/type_requires_more_generic_consts/old/src/lib.rs rename to test_crates/type_requires_more_const_generic_params/old/src/lib.rs diff --git a/test_outputs/query_execution/type_requires_more_const_generic_params.snap b/test_outputs/query_execution/type_requires_more_const_generic_params.snap index ade1ad48..8580615f 100644 --- a/test_outputs/query_execution/type_requires_more_const_generic_params.snap +++ b/test_outputs/query_execution/type_requires_more_const_generic_params.snap @@ -4,7 +4,7 @@ expression: "&query_execution_results" snapshot_kind: text --- { - "./test_crates/type_requires_more_generic_consts/": [ + "./test_crates/type_requires_more_const_generic_params/": [ { "name": String("NotGeneric"), "new_required_const_count": Uint64(1), @@ -15,7 +15,7 @@ snapshot_kind: text "old_required_consts": List([]), "owner_type": String("Struct"), "path": List([ - String("type_requires_more_generic_consts"), + String("type_requires_more_const_generic_params"), String("NotGeneric"), ]), "span_begin_line": Uint64(2), @@ -32,7 +32,7 @@ snapshot_kind: text "old_required_consts": List([]), "owner_type": String("Enum"), "path": List([ - String("type_requires_more_generic_consts"), + String("type_requires_more_const_generic_params"), String("DefaultBecomesRequired"), ]), "span_begin_line": Uint64(7), @@ -49,7 +49,7 @@ snapshot_kind: text "old_required_consts": List([]), "owner_type": String("Union"), "path": List([ - String("type_requires_more_generic_consts"), + String("type_requires_more_const_generic_params"), String("ConstGenericAdded"), ]), "span_begin_line": Uint64(12),