-
-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Const assert prevents compilation but trybuild thinks it succeeds #225
Comments
Ok, so it looks like internally If I update my test suite with a basic test that compiles: // tests/ui/pass.rs
fn main() {} Then re-write the // tests/ui.rs
#[test]
fn ui() {
let t = trybuild::TestCases::new();
- t.compile_fail("tests/ui/*.rs");
+ t.compile_fail("tests/ui/gteq_1_2.rs");
+ t.pass("tests/ui/pass.rs");
} Everything works as expected.
I think this is a In the shorter term I think we can solve this in
I'd be happy to submit a PR if I could get a steer on how you think this should be tackled. |
I think this is behaving correctly. What you've written is not a reliable way to elicit a compile time panic because a const is not necessarily evaluated at compile time, as long as it type-checks. |
Interesting -- in case anyone stumbles across this in the future: https://doc.rust-lang.org/reference/const_eval.html
I suppose what's written in my example above would be a let statement or const expression: /// Asserts `L >= R` at compile-time.
#[allow(path_statements)]
pub const fn const_assert_gteq<const L: usize, const R: usize>() {
Assert::<L, R>::GREATER_EQ;
} In this case it is actually being evaluated at compile-time ( @dtolnay This seems like a good regression to catch with What are your thoughts on possibly adding an env var override to Unfortunately I didn't see any documentation for when I suppose usage of build/check could be considered an implementation detail, but if they can produce different results I think it would be good to allow users to opt in to which they would prefer. |
Now that we have have const blocks, we have a guaranteed way of performing assertions during compile time. For example: const {
assert_eq!(1, 0);
} This currently results in a compile failure with |
Thanks for building
trybuild
, David!I was using the crate to test some const assertions were failing. Despite the fact that I can't actually compile the example code, when I include the same code as a
trybuild
test case to check for compile failure it fails. In other words,trybuild
thinks some code compiles when it definitely doesn't.I've reduced my problem down to:
Then, my test case:
Running the test case with:
Results in:
However, if I try and build the test case
gteq_1_2.rs
as an example I get a compile fail:If you'd like to run this I've pushed the minimum reproducible example to https://github.com/rjsberry/trybuild-mre.
I'm going to take a look into this myself, but my best guess right now (without reading the
trybuild
source) is that the compile failure is actually occuring in the crate which defines the const assert, rather than the example itself.The text was updated successfully, but these errors were encountered: