Skip to content

Commit f8a229b

Browse files
committed
bevy_reflect: Add compile fail tests for bevy_reflect (#7041)
# Objective There isn't really a way to test that code using bevy_reflect compiles or doesn't compile for certain scenarios. This would be especially useful for macro-centric PRs like #6511 and #6042. ## Solution Using `bevy_ecs_compile_fail_tests` as reference, added the `bevy_reflect_compile_fail_tests` crate. Currently, this crate contains a very simple test case. This is so that we can get the basic foundation of this crate agreed upon and merged so that more tests can be added by other PRs. ### Open Questions - [x] Should this be added to CI? (Answer: Yes) --- ## Changelog - Added the `bevy_reflect_compile_fail_tests` crate for testing compilation errors
1 parent 290d636 commit f8a229b

File tree

10 files changed

+79
-7
lines changed

10 files changed

+79
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ jobs:
108108
~/.cargo/git/db/
109109
target/
110110
crates/bevy_ecs_compile_fail_tests/target/
111+
crates/bevy_reflect_compile_fail_tests/target/
111112
key: ${{ runner.os }}-cargo-check-compiles-${{ hashFiles('**/Cargo.toml') }}
112113
- uses: dtolnay/rust-toolchain@stable
113114
with:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ readme = "README.md"
1212
repository = "https://github.com/bevyengine/bevy"
1313

1414
[workspace]
15-
exclude = ["benches", "crates/bevy_ecs_compile_fail_tests"]
15+
exclude = ["benches", "crates/bevy_ecs_compile_fail_tests", "crates/bevy_reflect_compile_fail_tests"]
1616
members = [
1717
"crates/*",
1818
"examples/android",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "bevy_reflect_compile_fail_tests"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Compile fail tests for Bevy Engine's reflection system"
6+
homepage = "https://bevyengine.org"
7+
repository = "https://github.com/bevyengine/bevy"
8+
license = "MIT OR Apache-2.0"
9+
publish = false
10+
11+
[dev-dependencies]
12+
bevy_reflect = { path = "../bevy_reflect" }
13+
trybuild = "1.0.71"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Compile fail tests for bevy_reflect
2+
3+
This crate is separate from `bevy_reflect` and not part of the Bevy workspace in order to not fail `crater` tests for
4+
Bevy.
5+
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).
6+
7+
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/reflect_derive/*.fail.rs");
5+
t.pass("tests/reflect_derive/*.pass.rs");
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bevy_reflect::Reflect;
2+
3+
#[derive(Reflect)]
4+
struct Foo<'a> {
5+
#[reflect(ignore)]
6+
value: &'a str,
7+
}
8+
9+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0478]: lifetime bound not satisfied
2+
--> tests/reflect_derive/lifetimes.fail.rs:3:10
3+
|
4+
3 | #[derive(Reflect)]
5+
| ^^^^^^^
6+
|
7+
note: lifetime parameter instantiated with the lifetime `'a` as defined here
8+
--> tests/reflect_derive/lifetimes.fail.rs:4:12
9+
|
10+
4 | struct Foo<'a> {
11+
| ^^
12+
= note: but lifetime parameter must outlive the static lifetime
13+
= note: this error originates in the derive macro `Reflect` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use bevy_reflect::Reflect;
2+
3+
// Reason: Reflection relies on `Any` which requires `'static`
4+
#[derive(Reflect)]
5+
struct Foo<'a: 'static> {
6+
#[reflect(ignore)]
7+
value: &'a str,
8+
}
9+
10+
fn main() {}

tools/ci/src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,24 @@ fn main() {
8888
}
8989

9090
if what_to_run.contains(Check::COMPILE_FAIL) {
91-
// Run UI tests (they do not get executed with the workspace tests)
92-
// - See crates/bevy_ecs_compile_fail_tests/README.md
93-
let _subdir = sh.push_dir("crates/bevy_ecs_compile_fail_tests");
94-
cmd!(sh, "cargo test --target-dir ../../target")
95-
.run()
96-
.expect("Compiler errors of the ECS compile fail tests seem to be different than expected! Check locally and compare rust versions.");
91+
{
92+
// ECS Compile Fail Tests
93+
// Run UI tests (they do not get executed with the workspace tests)
94+
// - See crates/bevy_ecs_compile_fail_tests/README.md
95+
let _subdir = sh.push_dir("crates/bevy_ecs_compile_fail_tests");
96+
cmd!(sh, "cargo test --target-dir ../../target")
97+
.run()
98+
.expect("Compiler errors of the ECS compile fail tests seem to be different than expected! Check locally and compare rust versions.");
99+
}
100+
{
101+
// Reflect Compile Fail Tests
102+
// Run tests (they do not get executed with the workspace tests)
103+
// - See crates/bevy_reflect_compile_fail_tests/README.md
104+
let _subdir = sh.push_dir("crates/bevy_reflect_compile_fail_tests");
105+
cmd!(sh, "cargo test --target-dir ../../target")
106+
.run()
107+
.expect("Compiler errors of the Reflect compile fail tests seem to be different than expected! Check locally and compare rust versions.");
108+
}
97109
}
98110

99111
if what_to_run.contains(Check::TEST) {

0 commit comments

Comments
 (0)