-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add remove_repr macro for testing: This macro removes `#[repr(_)]` from an enum, this is useful for testing the bug described in #1. `#[remove_repr]` is only available behind the feature flag test-utils. * Add a fake `repr` attribute in test-utils: This patch adds a fake `repr` and add tests to try to confuse the compiler into thinking that it is the real repr. If possible, that would cause problems with safe-discriminant. That is because we assume that there is always a repr attributed. * Reject enums with attribute macros: This patch checks if there is any top level attribute macro expansion, and if there it, we report it as an error. * test marcro ordering fixes: #1 --------- Signed-off-by: Ahmed Abdelraoof <ahmed.abdelraoof@huawei.com>
- Loading branch information
Showing
17 changed files
with
187 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
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,12 @@ | ||
use safe_discriminant::Discriminant; | ||
use safe_discriminant_derive::do_nothing; | ||
|
||
#[repr(u8)] | ||
#[derive(Discriminant)] | ||
#[do_nothing] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |
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 @@ | ||
error: Discriminant is not compatiable with any top level `#[attr]` except `#[repr(_)]`. | ||
--> tests/fail/do_nothing_last.rs:7:10 | ||
| | ||
7 | pub enum Foo { | ||
| ^^^ |
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,13 @@ | ||
// attempting to confuse Discriminant into thinking | ||
// the fake repr is the real repr | ||
use safe_discriminant::Discriminant; | ||
use safe_discriminant_derive::repr; | ||
|
||
#[derive(Discriminant)] | ||
#[repr(u8)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |
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,14 @@ | ||
error[E0659]: `repr` is ambiguous | ||
--> tests/fail/fake_repr1.rs:7:3 | ||
| | ||
7 | #[repr(u8)] | ||
| ^^^^ ambiguous name | ||
| | ||
= note: ambiguous because of a name conflict with a builtin attribute | ||
= note: `repr` could refer to a built-in attribute | ||
note: `repr` could also refer to the attribute macro imported here | ||
--> tests/fail/fake_repr1.rs:4:5 | ||
| | ||
4 | use safe_discriminant_derive::repr; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= help: use `crate::repr` to refer to this attribute macro unambiguously |
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,13 @@ | ||
// attempting to confuse Discriminant into thinking | ||
// the fake repr is the real repr | ||
use safe_discriminant::Discriminant; | ||
use safe_discriminant_derive::repr; | ||
|
||
#[derive(Discriminant)] | ||
#[crate::repr(u8)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |
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 @@ | ||
error: Discriminant requires a `#[repr(x)] where x is one of u8, i8, u16, i16, u32, i32, u64, i64, u128, i128. | ||
--> tests/fail/fake_repr2.rs:8:10 | ||
| | ||
8 | pub enum Foo { | ||
| ^^^ |
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,12 @@ | ||
// attempting to confuse Discriminant into thinking | ||
// the fake repr is the real repr | ||
use safe_discriminant::Discriminant; | ||
|
||
#[derive(Discriminant)] | ||
#[safe_discriminant_derive::repr(u8)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |
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 @@ | ||
error: Discriminant requires a `#[repr(x)] where x is one of u8, i8, u16, i16, u32, i32, u64, i64, u128, i128. | ||
--> tests/fail/fake_repr3.rs:7:10 | ||
| | ||
7 | pub enum Foo { | ||
| ^^^ |
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,12 @@ | ||
use safe_discriminant::Discriminant; | ||
use safe_discriminant_derive::remove_repr; | ||
|
||
#[remove_repr] | ||
#[derive(Discriminant)] | ||
#[repr(u8)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |
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 @@ | ||
error: Discriminant requires a `#[repr(x)] where x is one of u8, i8, u16, i16, u32, i32, u64, i64, u128, i128. | ||
--> tests/fail/remove_repr_disc1.rs:7:10 | ||
| | ||
7 | pub enum Foo { | ||
| ^^^ |
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,12 @@ | ||
use safe_discriminant::Discriminant; | ||
use safe_discriminant_derive::remove_repr; | ||
|
||
#[derive(Discriminant)] | ||
#[remove_repr] | ||
#[repr(u8)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |
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 @@ | ||
error: Discriminant is not compatiable with any top level `#[attr]` except `#[repr(_)]`. | ||
--> tests/fail/remove_repr_disc2.rs:7:10 | ||
| | ||
7 | pub enum Foo { | ||
| ^^^ |
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,15 @@ | ||
use safe_discriminant::Discriminant; | ||
use safe_discriminant_derive::do_nothing; | ||
|
||
#[do_nothing] | ||
#[repr(u8)] | ||
#[derive(Discriminant)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() { | ||
assert_eq!(Foo::A.discriminant(), 0); | ||
assert_eq!(Foo::B.discriminant(), 1); | ||
} |
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,11 @@ | ||
// this test makes sure that remove repr actually removes repr | ||
use safe_discriminant_derive::remove_repr; | ||
|
||
#[remove_repr] | ||
#[repr(FOO_BAR_TYPE_DOES_NOT_EXIST)] | ||
pub enum Foo { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() {} |