Assert compiler errors for compile_fail tests (#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. #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.
This commit is contained in:
Niklas Eicker 2021-11-13 22:43:19 +00:00
parent 94f5d194f4
commit d0f423d653
19 changed files with 274 additions and 174 deletions

View file

@ -12,7 +12,7 @@ readme = "README.md"
repository = "https://github.com/bevyengine/bevy" repository = "https://github.com/bevyengine/bevy"
[workspace] [workspace]
exclude = ["benches"] exclude = ["benches", "crates/bevy_ecs_compile_fail_tests"]
members = ["crates/*", "examples/ios", "tools/ci", "errors"] members = ["crates/*", "examples/ios", "tools/ci", "errors"]
[features] [features]

View file

@ -786,172 +786,3 @@ mod tests {
} }
} }
} }
/// ```compile_fail E0499
/// use bevy_ecs::prelude::*;
/// #[derive(Component)]
/// struct A(usize);
/// fn system(mut query: Query<&mut A>, e: Res<Entity>) {
/// let mut iter = query.iter_mut();
/// let a = &mut *iter.next().unwrap();
///
/// let mut iter2 = query.iter_mut();
/// let b = &mut *iter2.next().unwrap();
///
/// // this should fail to compile
/// println!("{}", a.0);
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_query_iter_lifetime_safety_test() {}
/// ```compile_fail E0499
/// use bevy_ecs::prelude::*;
/// #[derive(Component)]
/// struct A(usize);
/// fn system(mut query: Query<&mut A>, e: Res<Entity>) {
/// let mut a1 = query.get_mut(*e).unwrap();
/// let mut a2 = query.get_mut(*e).unwrap();
/// // this should fail to compile
/// println!("{} {}", a1.0, a2.0);
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_query_get_lifetime_safety_test() {}
/// ```compile_fail E0499
/// 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 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
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_query_set_iter_lifetime_safety_test() {}
/// ```compile_fail E0499
/// use bevy_ecs::prelude::*;
/// #[derive(Component)]
/// struct A(usize);
/// fn query_set(mut queries: QuerySet<(QueryState<&mut A>, QueryState<&A>)>, e: Res<Entity>) {
/// 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;
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_query_set_iter_flip_lifetime_safety_test() {}
/// ```compile_fail E0499
/// 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
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_query_set_get_lifetime_safety_test() {}
/// ```compile_fail E0499
/// use bevy_ecs::prelude::*;
/// #[derive(Component)]
/// struct A(usize);
/// fn query_set(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
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_query_set_get_flip_lifetime_safety_test() {}
/// ```compile_fail E0502
/// use bevy_ecs::prelude::*;
/// use bevy_ecs::system::SystemState;
/// #[derive(Component)]
/// struct A(usize);
/// #[derive(Component)]
/// struct B(usize);
/// struct State {
/// state_r: SystemState<Query<'static, 'static, &'static A>>,
/// state_w: SystemState<Query<'static, 'static, &'static mut A>>,
/// }
///
/// impl State {
/// fn get_component<'w>(&mut self, world: &'w mut World, entity: Entity) {
/// let q1 = self.state_r.get(&world);
/// let a1 = q1.get(entity).unwrap();
///
/// let mut q2 = self.state_w.get_mut(world);
/// let a2 = q2.get_mut(entity).unwrap();
///
/// // this should fail to compile
/// println!("{}", a1.0);
/// }
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_state_get_lifetime_safety_test() {}
/// ```compile_fail E0502
/// use bevy_ecs::prelude::*;
/// use bevy_ecs::system::SystemState;
/// #[derive(Component)]
/// struct A(usize);
/// #[derive(Component)]
/// struct B(usize);
/// struct State {
/// state_r: SystemState<Query<'static, 'static, &'static A>>,
/// state_w: SystemState<Query<'static, 'static, &'static mut A>>,
/// }
///
/// impl State {
/// fn get_components<'w>(&mut self, world: &'w mut World) {
/// let q1 = self.state_r.get(&world);
/// let a1 = q1.iter().next().unwrap();
/// let mut q2 = self.state_w.get_mut(world);
/// let a2 = q2.iter_mut().next().unwrap();
/// // this should fail to compile
/// println!("{}", a1.0);
/// }
/// }
/// ```
#[allow(unused)]
#[cfg(doctest)]
fn system_state_iter_lifetime_safety_test() {}

View file

@ -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"

View file

@ -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)).

View file

@ -0,0 +1 @@
// Nothing here, check out the integration tests

View file

@ -0,0 +1,5 @@
#[test]
fn test() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}

View file

@ -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() {}

View file

@ -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

View file

@ -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() {}

View file

@ -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

View file

@ -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() {}

View file

@ -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

View file

@ -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() {}

View file

@ -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

View file

@ -0,0 +1,28 @@
use bevy_ecs::prelude::*;
use bevy_ecs::system::SystemState;
#[derive(Component)]
struct A(usize);
#[derive(Component)]
struct B(usize);
struct State {
state_r: SystemState<Query<'static, 'static, &'static A>>,
state_w: SystemState<Query<'static, 'static, &'static mut A>>,
}
impl State {
fn get_component(&mut self, world: &mut World, entity: Entity) {
let q1 = self.state_r.get(&world);
let a1 = q1.get(entity).unwrap();
let mut q2 = self.state_w.get_mut(world);
let _ = q2.get_mut(entity).unwrap();
// this should fail to compile
println!("{}", a1.0);
}
}
fn main() {}

View file

@ -0,0 +1,11 @@
error[E0502]: cannot borrow `*world` as mutable because it is also borrowed as immutable
--> tests/ui/system_state_get_lifetime_safety.rs:20:22
|
17 | let q1 = self.state_r.get(&world);
| ------ immutable borrow occurs here
...
20 | let mut q2 = self.state_w.get_mut(world);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
24 | println!("{}", a1.0);
| ---- immutable borrow later used here

View file

@ -0,0 +1,28 @@
use bevy_ecs::prelude::*;
use bevy_ecs::system::SystemState;
#[derive(Component)]
struct A(usize);
#[derive(Component)]
struct B(usize);
struct State {
state_r: SystemState<Query<'static, 'static, &'static A>>,
state_w: SystemState<Query<'static, 'static, &'static mut A>>,
}
impl State {
fn get_components(&mut self, world: &mut World) {
let q1 = self.state_r.get(&world);
let a1 = q1.iter().next().unwrap();
let mut q2 = self.state_w.get_mut(world);
let _ = q2.iter_mut().next().unwrap();
// this should fail to compile
println!("{}", a1.0);
}
}
fn main() {}

View file

@ -0,0 +1,11 @@
error[E0502]: cannot borrow `*world` as mutable because it is also borrowed as immutable
--> tests/ui/system_state_iter_lifetime_safety.rs:20:22
|
17 | let q1 = self.state_r.get(&world);
| ------ immutable borrow occurs here
...
20 | let mut q2 = self.state_w.get_mut(world);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
24 | println!("{}", a1.0);
| ---- immutable borrow later used here

View file

@ -1,4 +1,4 @@
use xshell::cmd; use xshell::{cmd, pushd};
fn main() { fn main() {
// When run locally, results may differ from actual CI runs triggered by // When run locally, results may differ from actual CI runs triggered by
@ -16,4 +16,14 @@ fn main() {
cmd!("cargo clippy --workspace --all-targets --all-features -- -D warnings -A clippy::type_complexity") cmd!("cargo clippy --workspace --all-targets --all-features -- -D warnings -A clippy::type_complexity")
.run() .run()
.expect("Please fix clippy errors in output above."); .expect("Please fix clippy errors in output above.");
// Run UI tests (they do not get executed with the workspace tests)
// - See crates/bevy_ecs_compile_fail_tests/README.md
{
let _bevy_ecs_compile_fail_tests = pushd("crates/bevy_ecs_compile_fail_tests")
.expect("Failed to navigate to the 'bevy_ecs_compile_fail_tests' crate");
cmd!("cargo test")
.run()
.expect("Compiler errors of the ECS compile fail tests seem to be different than expected! Check locally and compare rust versions.");
}
} }