Skip to content

Commit 6719f28

Browse files
committed
Added bevy_macros_compile_fail_tests crate
Created with compile tests for the Deref/DerefMut derives
1 parent 6a556f0 commit 6719f28

31 files changed

+521
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rust-version = "1.67.0"
1616
exclude = [
1717
"benches",
1818
"crates/bevy_ecs_compile_fail_tests",
19+
"crates/bevy_macros_compile_fail_tests",
1920
"crates/bevy_reflect_compile_fail_tests",
2021
]
2122
members = [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "bevy_macros_compile_fail_tests"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Compile fail tests for Bevy Engine's various macros"
6+
homepage = "https://bevyengine.org"
7+
repository = "https://github.com/bevyengine/bevy"
8+
license = "MIT OR Apache-2.0"
9+
publish = false
10+
11+
[dependencies]
12+
bevy_derive = { path = "../bevy_derive" }
13+
trybuild = "1.0.71"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Compile fail tests for Bevy macros
2+
3+
This crate is not part of the Bevy workspace in order to not fail `crater` tests for Bevy.
4+
The tests assert on the exact compiler errors and can easily fail for new Rust versions due to updated compiler errors (e.g. changes in spans).
5+
6+
The `CI` workflow executes these tests on the stable rust toolchain (see [tools/ci](../../tools/ci/src/main.rs)).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Nothing here, check out the integration tests
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[test]
2+
fn test() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/deref_derive/*.fail.rs");
5+
t.pass("tests/deref_derive/*.pass.rs");
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use bevy_derive::Deref;
2+
3+
// Reason: `#[deref]` doesn't take any arguments
4+
5+
#[derive(Deref)]
6+
struct TupleStruct(usize, #[deref()] String);
7+
8+
#[derive(Deref)]
9+
struct Struct {
10+
foo: usize,
11+
#[deref()]
12+
bar: String,
13+
}
14+
15+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: unexpected token in attribute
2+
--> tests/deref_derive/invalid_attribute.fail.rs:6:34
3+
|
4+
6 | struct TupleStruct(usize, #[deref()] String);
5+
| ^
6+
7+
error: unexpected token in attribute
8+
--> tests/deref_derive/invalid_attribute.fail.rs:11:12
9+
|
10+
11 | #[deref()]
11+
| ^
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bevy_derive::Deref;
2+
3+
#[derive(Deref)]
4+
struct UnitStruct;
5+
6+
#[derive(Deref)]
7+
enum Enum {}
8+
9+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Deref cannot be derived on field-less structs
2+
--> tests/deref_derive/invalid_item.fail.rs:3:10
3+
|
4+
3 | #[derive(Deref)]
5+
| ^^^^^
6+
|
7+
= note: this error originates in the derive macro `Deref` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: Deref can only be derived on structs
10+
--> tests/deref_derive/invalid_item.fail.rs:6:10
11+
|
12+
6 | #[derive(Deref)]
13+
| ^^^^^
14+
|
15+
= note: this error originates in the derive macro `Deref` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use bevy_derive::Deref;
2+
3+
#[derive(Deref)]
4+
struct TupleStruct(usize, String);
5+
6+
#[derive(Deref)]
7+
struct Struct {
8+
foo: usize,
9+
bar: String,
10+
}
11+
12+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: deriving Deref on multi-field structs requires one field to have the `#[deref]` attribute
2+
--> tests/deref_derive/missing_attribute.fail.rs:3:10
3+
|
4+
3 | #[derive(Deref)]
5+
| ^^^^^
6+
|
7+
= note: this error originates in the derive macro `Deref` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: deriving Deref on multi-field structs requires one field to have the `#[deref]` attribute
10+
--> tests/deref_derive/missing_attribute.fail.rs:6:10
11+
|
12+
6 | #[derive(Deref)]
13+
| ^^^^^
14+
|
15+
= note: this error originates in the derive macro `Deref` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use bevy_derive::Deref;
2+
3+
#[derive(Deref)]
4+
struct TupleStruct(#[deref] usize, #[deref] String);
5+
6+
#[derive(Deref)]
7+
struct Struct {
8+
#[deref]
9+
foo: usize,
10+
#[deref]
11+
bar: String,
12+
}
13+
14+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: `#[deref]` attribute can only be used on a single field
2+
--> tests/deref_derive/multiple_attributes.fail.rs:4:36
3+
|
4+
4 | struct TupleStruct(#[deref] usize, #[deref] String);
5+
| ^^^^^^^^
6+
7+
error: `#[deref]` attribute can only be used on a single field
8+
--> tests/deref_derive/multiple_attributes.fail.rs:10:5
9+
|
10+
10 | #[deref]
11+
| ^^^^^^^^
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use bevy_derive::Deref;
2+
3+
#[derive(Deref)]
4+
struct TupleStruct(usize, #[deref] String);
5+
6+
#[derive(Deref)]
7+
struct Struct {
8+
foo: usize,
9+
#[deref]
10+
bar: String,
11+
}
12+
13+
fn main() {
14+
let value = TupleStruct(123, "Hello world!".to_string());
15+
let _: &String = &*value;
16+
17+
let value = Struct {
18+
foo: 123,
19+
bar: "Hello world!".to_string(),
20+
};
21+
let _: &String = &*value;
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use bevy_derive::Deref;
2+
3+
#[derive(Deref)]
4+
struct TupleStruct(String);
5+
6+
#[derive(Deref)]
7+
struct Struct {
8+
bar: String,
9+
}
10+
11+
fn main() {
12+
let value = TupleStruct("Hello world!".to_string());
13+
let _: &String = &*value;
14+
15+
let value = Struct {
16+
bar: "Hello world!".to_string(),
17+
};
18+
let _: &String = &*value;
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[test]
2+
fn test() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/deref_mut_derive/*.fail.rs");
5+
t.pass("tests/deref_mut_derive/*.pass.rs");
6+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use bevy_derive::DerefMut;
2+
use std::ops::Deref;
3+
4+
// Reason: `#[deref]` doesn't take any arguments
5+
6+
#[derive(DerefMut)]
7+
struct TupleStruct(usize, #[deref()] String);
8+
9+
impl Deref for TupleStruct {
10+
type Target = String;
11+
12+
fn deref(&self) -> &Self::Target {
13+
&self.1
14+
}
15+
}
16+
17+
#[derive(DerefMut)]
18+
struct Struct {
19+
foo: usize,
20+
#[deref()]
21+
bar: String,
22+
}
23+
24+
impl Deref for Struct {
25+
type Target = String;
26+
27+
fn deref(&self) -> &Self::Target {
28+
&self.bar
29+
}
30+
}
31+
32+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: unexpected token in attribute
2+
--> tests/deref_mut_derive/invalid_attribute.fail.rs:7:34
3+
|
4+
7 | struct TupleStruct(usize, #[deref()] String);
5+
| ^
6+
7+
error: unexpected token in attribute
8+
--> tests/deref_mut_derive/invalid_attribute.fail.rs:20:12
9+
|
10+
20 | #[deref()]
11+
| ^
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bevy_derive::DerefMut;
2+
3+
#[derive(DerefMut)]
4+
struct UnitStruct;
5+
6+
#[derive(DerefMut)]
7+
enum Enum {}
8+
9+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: DerefMut cannot be derived on field-less structs
2+
--> tests/deref_mut_derive/invalid_item.fail.rs:3:10
3+
|
4+
3 | #[derive(DerefMut)]
5+
| ^^^^^^^^
6+
|
7+
= note: this error originates in the derive macro `DerefMut` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: DerefMut can only be derived on structs
10+
--> tests/deref_mut_derive/invalid_item.fail.rs:6:10
11+
|
12+
6 | #[derive(DerefMut)]
13+
| ^^^^^^^^
14+
|
15+
= note: this error originates in the derive macro `DerefMut` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use bevy_derive::DerefMut;
2+
use std::ops::Deref;
3+
4+
#[derive(DerefMut)]
5+
struct TupleStruct(#[deref] usize, String);
6+
7+
impl Deref for TupleStruct {
8+
type Target = String;
9+
10+
fn deref(&self) -> &Self::Target {
11+
&self.1
12+
}
13+
}
14+
15+
#[derive(DerefMut)]
16+
struct Struct {
17+
#[deref]
18+
foo: usize,
19+
bar: String,
20+
}
21+
22+
impl Deref for Struct {
23+
type Target = String;
24+
25+
fn deref(&self) -> &Self::Target {
26+
&self.bar
27+
}
28+
}
29+
30+
fn main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0308]: mismatched types
2+
--> tests/deref_mut_derive/mismatched_target_type.fail.rs:4:10
3+
|
4+
4 | #[derive(DerefMut)]
5+
| ^^^^^^^^
6+
| |
7+
| expected `&mut String`, found `&mut usize`
8+
| expected `&mut String` because of return type
9+
|
10+
= note: expected mutable reference `&mut String`
11+
found mutable reference `&mut usize`
12+
= note: this error originates in the derive macro `DerefMut` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error[E0308]: mismatched types
15+
--> tests/deref_mut_derive/mismatched_target_type.fail.rs:15:10
16+
|
17+
15 | #[derive(DerefMut)]
18+
| ^^^^^^^^
19+
| |
20+
| expected `&mut String`, found `&mut usize`
21+
| expected `&mut String` because of return type
22+
|
23+
= note: expected mutable reference `&mut String`
24+
found mutable reference `&mut usize`
25+
= note: this error originates in the derive macro `DerefMut` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use bevy_derive::DerefMut;
2+
use std::ops::Deref;
3+
4+
#[derive(DerefMut)]
5+
struct TupleStruct(usize, String);
6+
7+
impl Deref for TupleStruct {
8+
type Target = String;
9+
10+
fn deref(&self) -> &Self::Target {
11+
&self.1
12+
}
13+
}
14+
15+
#[derive(DerefMut)]
16+
struct Struct {
17+
foo: usize,
18+
bar: String,
19+
}
20+
21+
impl Deref for Struct {
22+
type Target = String;
23+
24+
fn deref(&self) -> &Self::Target {
25+
&self.bar
26+
}
27+
}
28+
29+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: deriving DerefMut on multi-field structs requires one field to have the `#[deref]` attribute
2+
--> tests/deref_mut_derive/missing_attribute.fail.rs:4:10
3+
|
4+
4 | #[derive(DerefMut)]
5+
| ^^^^^^^^
6+
|
7+
= note: this error originates in the derive macro `DerefMut` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: deriving DerefMut on multi-field structs requires one field to have the `#[deref]` attribute
10+
--> tests/deref_mut_derive/missing_attribute.fail.rs:15:10
11+
|
12+
15 | #[derive(DerefMut)]
13+
| ^^^^^^^^
14+
|
15+
= note: this error originates in the derive macro `DerefMut` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use bevy_derive::DerefMut;
2+
3+
#[derive(DerefMut)]
4+
struct TupleStruct(usize, #[deref] String);
5+
6+
#[derive(DerefMut)]
7+
struct Struct {
8+
foo: usize,
9+
#[deref]
10+
bar: String,
11+
}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)