-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add matching lints for const-generics breakage in traits. (#1055)
Add the `trait_allows_fewer_const_generic_params` and `trait_requires_more_const_generic_params` lints, which check for traits that now require more, or support fewer, const generics than they used to. These are parallel lints to the type-related ones merged shortly prior, just over traits instead of structs/enums/unions.
- Loading branch information
1 parent
127ba43
commit 06b860c
Showing
13 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
SemverQuery( | ||
id: "trait_allows_fewer_const_generic_params", | ||
human_readable_name: "trait now allows fewer const generic parameters", | ||
description: "A trait 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 Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @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 Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
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 trait now allows fewer const generic parameters than it used to. Uses of this trait that supplied all previously-supported const generics will be broken.", | ||
per_result_error_template: Some("trait {{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 trait bound 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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
SemverQuery( | ||
id: "trait_requires_more_const_generic_params", | ||
human_readable_name: "trait now requires more const generic parameters", | ||
description: "A trait now requires more 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 | ||
importable_path { | ||
path @tag @output | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
generic_parameter @fold | ||
@transform(op: "count") | ||
@tag(name: "old_required_const_count") | ||
@output(name: "old_required_const_count") { | ||
... on GenericConstParameter { | ||
old_required_consts: name @output | ||
has_default @filter(op: "!=", value: ["$true"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on ImplOwner { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
generic_parameter @fold | ||
@transform(op: "count") | ||
@filter(op: ">", value: ["%old_required_const_count"]) | ||
@output(name: "new_required_const_count") { | ||
... on GenericConstParameter { | ||
new_required_consts: name @output | ||
has_default @filter(op: "!=", value: ["$true"]) | ||
} | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"true": true, | ||
}, | ||
error_message: "A trait now requires more const generic parameters than it used to. Uses of this trait that supplied the previously-required number of const generics will be broken. To fix this, consider supplying default values for newly-added const generics.", | ||
per_result_error_template: Some("trait {{name}} ({{old_required_const_count}} -> {{new_required_const_count}} required 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 required const generics, which is insufficient for the new definition. | ||
witness: None, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test_crates/trait_allows_fewer_const_generic_params/new/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
publish = false | ||
name = "trait_allows_fewer_const_generic_params" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
5 changes: 5 additions & 0 deletions
5
test_crates/trait_allows_fewer_const_generic_params/new/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub trait Example<const N: usize> {} | ||
|
||
pub trait NotGenericAnymore {} | ||
|
||
pub trait NotGenericEither<T> {} |
7 changes: 7 additions & 0 deletions
7
test_crates/trait_allows_fewer_const_generic_params/old/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
publish = false | ||
name = "trait_allows_fewer_const_generic_params" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
5 changes: 5 additions & 0 deletions
5
test_crates/trait_allows_fewer_const_generic_params/old/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub trait Example<const N: usize, const M: usize = 8> {} | ||
|
||
pub trait NotGenericAnymore<const N: usize> {} | ||
|
||
pub trait NotGenericEither<T, const N: usize> {} |
7 changes: 7 additions & 0 deletions
7
test_crates/trait_requires_more_const_generic_params/new/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
publish = false | ||
name = "trait_requires_more_const_generic_params" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
8 changes: 8 additions & 0 deletions
8
test_crates/trait_requires_more_const_generic_params/new/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pub trait NotGeneric<const COUNT: usize> {} | ||
|
||
pub trait DefaultBecomesRequired<const N: usize, const M: usize = 1> {} | ||
|
||
pub trait ConstGenericAdded<T, const N: usize> {} | ||
|
||
// This one isn't breaking, so it shouldn't be flagged! | ||
pub trait DefaultedConstGenericAdded<T, const N: usize = 16> {} |
7 changes: 7 additions & 0 deletions
7
test_crates/trait_requires_more_const_generic_params/old/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
publish = false | ||
name = "trait_requires_more_const_generic_params" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
8 changes: 8 additions & 0 deletions
8
test_crates/trait_requires_more_const_generic_params/old/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pub trait NotGeneric {} | ||
|
||
pub trait DefaultBecomesRequired<const N: usize = 1, const M: usize = 1> {} | ||
|
||
pub trait ConstGenericAdded<T> {} | ||
|
||
// This one isn't breaking, so it shouldn't be flagged! | ||
pub trait DefaultedConstGenericAdded<T> {} |
60 changes: 60 additions & 0 deletions
60
test_outputs/query_execution/trait_allows_fewer_const_generic_params.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
source: src/query.rs | ||
expression: "&query_execution_results" | ||
snapshot_kind: text | ||
--- | ||
{ | ||
"./test_crates/trait_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"), | ||
]), | ||
"path": List([ | ||
String("trait_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"), | ||
]), | ||
"path": List([ | ||
String("trait_allows_fewer_const_generic_params"), | ||
String("NotGenericAnymore"), | ||
]), | ||
"span_begin_line": Uint64(3), | ||
"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"), | ||
]), | ||
"path": List([ | ||
String("trait_allows_fewer_const_generic_params"), | ||
String("NotGenericEither"), | ||
]), | ||
"span_begin_line": Uint64(5), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
], | ||
} |
57 changes: 57 additions & 0 deletions
57
test_outputs/query_execution/trait_requires_more_const_generic_params.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
source: src/query.rs | ||
expression: "&query_execution_results" | ||
snapshot_kind: text | ||
--- | ||
{ | ||
"./test_crates/type_requires_more_const_generic_params/": [ | ||
{ | ||
"name": String("NotGeneric"), | ||
"new_required_const_count": Uint64(1), | ||
"new_required_consts": List([ | ||
String("COUNT"), | ||
]), | ||
"old_required_const_count": Uint64(0), | ||
"old_required_consts": List([]), | ||
"path": List([ | ||
String("type_requires_more_const_generic_params"), | ||
String("NotGeneric"), | ||
]), | ||
"span_begin_line": Uint64(2), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"name": String("DefaultBecomesRequired"), | ||
"new_required_const_count": Uint64(1), | ||
"new_required_consts": List([ | ||
String("N"), | ||
]), | ||
"old_required_const_count": Uint64(0), | ||
"old_required_consts": List([]), | ||
"path": List([ | ||
String("type_requires_more_const_generic_params"), | ||
String("DefaultBecomesRequired"), | ||
]), | ||
"span_begin_line": Uint64(7), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"name": String("ConstGenericAdded"), | ||
"new_required_const_count": Uint64(1), | ||
"new_required_consts": List([ | ||
String("N"), | ||
]), | ||
"old_required_const_count": Uint64(0), | ||
"old_required_consts": List([]), | ||
"path": List([ | ||
String("type_requires_more_const_generic_params"), | ||
String("ConstGenericAdded"), | ||
]), | ||
"span_begin_line": Uint64(12), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
], | ||
} |