-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for #[repr(i*)]/#[repr(u*)] semver violations. (#29)
* Add type info to schema and use it in the query. * Update query and add more test data. * Support PhantomData markers in #[repr(transparent)] check. * Check for removal of repr(u*) and repr(i*) attributes. * Check for #[repr(i/u*)] changing to another integer repr.
- Loading branch information
1 parent
b05bfaf
commit 9d0cfa6
Showing
12 changed files
with
670 additions
and
286 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
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
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,71 @@ | ||
#[cfg(not(feature = "enum_repr_int_changed"))] | ||
#[repr(u8)] | ||
pub enum U8ToU16Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_changed")] | ||
#[repr(u16)] | ||
pub enum U8ToU16Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(not(feature = "enum_repr_int_changed"))] | ||
#[repr(i32)] | ||
pub enum I32ToI8Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_changed")] | ||
#[repr(i8)] | ||
pub enum I32ToI8Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(not(feature = "enum_repr_int_changed"))] | ||
#[repr(i32)] | ||
pub enum I32ToU32Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_changed")] | ||
#[repr(u32)] | ||
pub enum I32ToU32Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
// The following enums have *removals* of repr(i*) and repr(u*), | ||
// not changes to another repr(i*) or repr(u*). | ||
// They should not be reported by this rule, because they have their own rule. | ||
|
||
#[cfg(not(feature = "enum_repr_int_changed"))] | ||
#[repr(u8)] | ||
pub enum U8Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_changed")] | ||
pub enum U8Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(not(feature = "enum_repr_int_changed"))] | ||
#[repr(i32)] | ||
pub enum I32Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_changed")] | ||
pub enum I32Enum { | ||
Bar, | ||
Baz, | ||
} |
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,25 @@ | ||
#[cfg(not(feature = "enum_repr_int_removed"))] | ||
#[repr(u8)] | ||
pub enum U8Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_removed")] | ||
pub enum U8Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(not(feature = "enum_repr_int_removed"))] | ||
#[repr(i32)] | ||
pub enum I32Enum { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
#[cfg(feature = "enum_repr_int_removed")] | ||
pub enum I32Enum { | ||
Bar, | ||
Baz, | ||
} |
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
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
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,66 @@ | ||
SemverQuery( | ||
id: "enum_repr_int_changed", | ||
human_readable_name: "enum repr(u*)/repr(i*) changed", | ||
description: "The repr(u*) or repr(i*) attribute on an enum was changed to another integer type. This can cause its memory representation to change, breaking FFI use cases.", | ||
required_update: Major, | ||
|
||
// TODO: Change the reference link to point to the cargo semver reference | ||
// once it has a section on repr(u*)/repr(i*). | ||
reference_link: Some("https://doc.rust-lang.org/nomicon/other-reprs.html#repru-repri"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
baseline { | ||
item { | ||
... on Enum { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
name @tag @output | ||
attribute { | ||
attr_value: value @filter(op: "regex", value: ["$repr_regex"]) | ||
@tag @output(name: "old_attr") | ||
} | ||
path { | ||
path @tag @output | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Enum { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @filter(op: "=", value: ["%name"]) | ||
path { | ||
path @filter(op: "=", value: ["%path"]) | ||
} | ||
# Check that there exists an attribute that: | ||
# - looks like repr(i*) or repr(u*) | ||
# - but is not the same repr(i*) or repr(u*) that we had before. | ||
# This is the breaking change. | ||
attribute @fold @transform(op: "count") @filter(op: ">", value: ["$zero"]) { | ||
value @filter(op: "regex", value: ["$repr_regex"]) | ||
@filter(op: "!=", value: ["%attr_value"]) | ||
@output(name: "new_attr") | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"repr_regex": "#\\[repr\\([ui]\\d+\\)\\]", | ||
"zero": 0, | ||
}, | ||
error_message: "The repr(u*) or repr(i*) attribute on an enum was changed to another integer type. This can cause its memory representation to change, breaking FFI use cases.", | ||
per_result_error_template: Some("enum {{name}} {{old_attr}} -> {{new_attr.0}} in {{span_filename}}:{{span_begin_line}}"), | ||
) |
Oops, something went wrong.