The Rust project has a special set of tests called compile-fail that do exactly what you want.
compiletest crate is an extraction of this idea that allows other libraries to do the same:
fn main() { let x: (u64, bool) = (true, 42u64);
One of the ideas that appears halfway is to use the Cargo features.
Define tests using the function flag:
#[test] #[cfg(feature = "compile_failure")] fn bogus_test() {}
Add this to Cargo.toml:
[features] compile_failure = []
And run the tests like
cargo test --features compile_failure
The obvious thing missing from this is the automatic check "if it was a correct mistake." If nothing else, this allows me to have tests that half live in my code base.
source share