-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
False positive assertions_on_constants
#8159
Comments
assert!(true)
will be optimized out by the compilerassertions_on_constants
Thanks for the report. This is currently expected behavior. The question is if it is the right behavior. I lean towards, that it isn't. |
I agree that this doesn't seem like the correct behavior inside constants. For example, if we want to check that const _: () = assert!(usize::BITS >= 32); But this triggers the |
These values were taken from the Blis kernel configuration for Haswell [1], Setting NR to use 2 AVX registers takes advantage of the 2 FMA execution ports [2]. The clippy lint about the compiler optimizing away constant assertions was disabled because that is exactly the behavior that we want. See also rust-lang/rust-clippy#8159. [1] https:/flame/blis/blob/f956b79922da412791e4c8b8b846b3aafc0a5ee0/kernels/haswell/bli_kernels_haswell.h#L55 [2] bluss/matrixmultiply#34 (comment)
imo, it is ok to trigger on code like assert!(FOO > BAR); but compile-time assertion variants like const _: () = assert!(FOO > BAR); should be accepted. |
This isn't fully fixed (cc @StackOverflowExcept1on (wrote PR), @Jarcho (r+'d PR)): const X1P_20: f64 = 9.536_743_164_062_5e-7; // 2^-20 ≅ 10^-6
const _: () = assert!(X1P_20 <= 1.0e-6); // no longer warns :+1:
const _: () = {
assert!(X1P_20 <= 1.0e-6); // warns :disappointed:
}; As an additional aside, it's probably preferable to not suppress the lint when the |
@CAD97 Good catch. We need to somehow check if rust-clippy/clippy_lints/src/assertions_on_constants.rs Lines 48 to 56 in a71211d
|
We should be able to get the enclosing body with |
Summary
assert!(true) will be optimized out by the compiler
is given when asserting on constants.Asserting on constants is very important for ensuring various kinds of invariants are upheld.
The example given in the recent announcement for panic in const contexts functionality doesn't reproduce the issue but the issue can still be reproduced in const contexts.
Lint Name
assertions_on_constants
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen: No warnings
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: