forked from bevyengine/bevy
-
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.
Assert compiler errors for compile_fail tests (bevyengine#3067)
# Objective bevy_ecs has several compile_fail tests that assert lifetime safety. In the past, these tests have been green for the wrong reasons (see e.g. bevyengine#2984). This PR makes sure, that they will fail if the compiler error changes. ## Solution Use [trybuild](https://crates.io/crates/trybuild) to assert the compiler errors. The UI tests are in a separate crate that is not part of the Bevy workspace. This is to ensure that they do not break Bevy's crater builds. The tests get executed by the CI workflow on the stable toolchain.
- Loading branch information
Showing
19 changed files
with
274 additions
and
174 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,13 @@ | ||
[package] | ||
name = "bevy_ecs_compile_fail_tests" | ||
version = "0.5.0" | ||
edition = "2021" | ||
description = "Compile fail tests for Bevy Engine's entity component system" | ||
homepage = "https://bevyengine.org" | ||
repository = "https://github.com/bevyengine/bevy" | ||
license = "MIT OR Apache-2.0" | ||
publish = false | ||
|
||
[dev-dependencies] | ||
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" } | ||
trybuild = "1.0" |
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 @@ | ||
# Compile fail tests for bevy_ecs | ||
|
||
This crate is separate from `bevy_ecs` and not part of the Bevy workspace in order to not fail `crater` tests for Bevy. 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). | ||
|
||
The `CI` workflow executes these tests on the stable rust toolchain (see [tools/ci](../../tools/ci/src/main.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 @@ | ||
// Nothing here, check out the integration tests |
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 @@ | ||
#[test] | ||
fn test() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/ui/*.rs"); | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_get_lifetime_safety.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,13 @@ | ||
use bevy_ecs::prelude::*; | ||
|
||
#[derive(Component)] | ||
struct A(usize); | ||
|
||
fn system(mut query: Query<&mut A>, e: Res<Entity>) { | ||
let a1 = query.get_mut(*e).unwrap(); | ||
let a2 = query.get_mut(*e).unwrap(); | ||
// this should fail to compile | ||
println!("{} {}", a1.0, a2.0); | ||
} | ||
|
||
fn main() {} |
10 changes: 10 additions & 0 deletions
10
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_get_lifetime_safety.stderr
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,10 @@ | ||
error[E0499]: cannot borrow `query` as mutable more than once at a time | ||
--> tests/ui/system_query_get_lifetime_safety.rs:8:14 | ||
| | ||
7 | let a1 = query.get_mut(*e).unwrap(); | ||
| ----- first mutable borrow occurs here | ||
8 | let a2 = query.get_mut(*e).unwrap(); | ||
| ^^^^^ second mutable borrow occurs here | ||
9 | // this should fail to compile | ||
10 | println!("{} {}", a1.0, a2.0); | ||
| -- first borrow later used here |
17 changes: 17 additions & 0 deletions
17
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_iter_lifetime_safety.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,17 @@ | ||
use bevy_ecs::prelude::*; | ||
|
||
#[derive(Component)] | ||
struct A(usize); | ||
|
||
fn system(mut query: Query<&mut A>) { | ||
let mut iter = query.iter_mut(); | ||
let a = &mut *iter.next().unwrap(); | ||
|
||
let mut iter2 = query.iter_mut(); | ||
let _ = &mut *iter2.next().unwrap(); | ||
|
||
// this should fail to compile | ||
println!("{}", a.0); | ||
} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_iter_lifetime_safety.stderr
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 @@ | ||
error[E0499]: cannot borrow `query` as mutable more than once at a time | ||
--> tests/ui/system_query_iter_lifetime_safety.rs:10:21 | ||
| | ||
7 | let mut iter = query.iter_mut(); | ||
| ----- first mutable borrow occurs here | ||
... | ||
10 | let mut iter2 = query.iter_mut(); | ||
| ^^^^^ second mutable borrow occurs here | ||
... | ||
14 | println!("{}", a.0); | ||
| --- first borrow later used here |
28 changes: 28 additions & 0 deletions
28
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_set_get_lifetime_safety.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,28 @@ | ||
use bevy_ecs::prelude::*; | ||
|
||
#[derive(Component)] | ||
struct A(usize); | ||
|
||
fn query_set(mut queries: QuerySet<(QueryState<&mut A>, QueryState<&A>)>, e: Res<Entity>) { | ||
let mut q2 = queries.q0(); | ||
let mut b = q2.get_mut(*e).unwrap(); | ||
|
||
let q1 = queries.q1(); | ||
let a = q1.get(*e).unwrap(); | ||
|
||
// this should fail to compile | ||
b.0 = a.0 | ||
} | ||
|
||
fn query_set_flip(mut queries: QuerySet<(QueryState<&mut A>, QueryState<&A>)>, e: Res<Entity>) { | ||
let q1 = queries.q1(); | ||
let a = q1.get(*e).unwrap(); | ||
|
||
let mut q2 = queries.q0(); | ||
let mut b = q2.get_mut(*e).unwrap(); | ||
|
||
// this should fail to compile | ||
b.0 = a.0 | ||
} | ||
|
||
fn main() {} |
23 changes: 23 additions & 0 deletions
23
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_set_get_lifetime_safety.stderr
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,23 @@ | ||
error[E0499]: cannot borrow `queries` as mutable more than once at a time | ||
--> tests/ui/system_query_set_get_lifetime_safety.rs:10:14 | ||
| | ||
7 | let mut q2 = queries.q0(); | ||
| ------- first mutable borrow occurs here | ||
... | ||
10 | let q1 = queries.q1(); | ||
| ^^^^^^^ second mutable borrow occurs here | ||
... | ||
14 | b.0 = a.0 | ||
| - first borrow later used here | ||
|
||
error[E0499]: cannot borrow `queries` as mutable more than once at a time | ||
--> tests/ui/system_query_set_get_lifetime_safety.rs:21:18 | ||
| | ||
18 | let q1 = queries.q1(); | ||
| ------- first mutable borrow occurs here | ||
... | ||
21 | let mut q2 = queries.q0(); | ||
| ^^^^^^^ second mutable borrow occurs here | ||
... | ||
25 | b.0 = a.0 | ||
| --- first borrow later used here |
32 changes: 32 additions & 0 deletions
32
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_set_iter_lifetime_safety.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,32 @@ | ||
use bevy_ecs::prelude::*; | ||
|
||
#[derive(Component)] | ||
struct A(usize); | ||
|
||
fn query_set(mut queries: QuerySet<(QueryState<&mut A>, QueryState<&A>)>) { | ||
let mut q2 = queries.q0(); | ||
let mut iter2 = q2.iter_mut(); | ||
let mut b = iter2.next().unwrap(); | ||
|
||
let q1 = queries.q1(); | ||
let mut iter = q1.iter(); | ||
let a = &*iter.next().unwrap(); | ||
|
||
// this should fail to compile | ||
b.0 = a.0 | ||
} | ||
|
||
fn query_set_flip(mut queries: QuerySet<(QueryState<&mut A>, QueryState<&A>)>) { | ||
let q1 = queries.q1(); | ||
let mut iter = q1.iter(); | ||
let a = &*iter.next().unwrap(); | ||
|
||
let mut q2 = queries.q0(); | ||
let mut iter2 = q2.iter_mut(); | ||
let mut b = iter2.next().unwrap(); | ||
|
||
// this should fail to compile | ||
b.0 = a.0; | ||
} | ||
|
||
fn main() {} |
23 changes: 23 additions & 0 deletions
23
crates/bevy_ecs_compile_fail_tests/tests/ui/system_query_set_iter_lifetime_safety.stderr
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,23 @@ | ||
error[E0499]: cannot borrow `queries` as mutable more than once at a time | ||
--> tests/ui/system_query_set_iter_lifetime_safety.rs:11:14 | ||
| | ||
7 | let mut q2 = queries.q0(); | ||
| ------- first mutable borrow occurs here | ||
... | ||
11 | let q1 = queries.q1(); | ||
| ^^^^^^^ second mutable borrow occurs here | ||
... | ||
16 | b.0 = a.0 | ||
| - first borrow later used here | ||
|
||
error[E0499]: cannot borrow `queries` as mutable more than once at a time | ||
--> tests/ui/system_query_set_iter_lifetime_safety.rs:24:18 | ||
| | ||
20 | let q1 = queries.q1(); | ||
| ------- first mutable borrow occurs here | ||
... | ||
24 | let mut q2 = queries.q0(); | ||
| ^^^^^^^ second mutable borrow occurs here | ||
... | ||
29 | b.0 = a.0; | ||
| --- first borrow later used here |
Oops, something went wrong.