rust-lang / rust-lang/rust-clippy
const assert in struct is not always evaluated
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
I would expect this not to compile because of the const assert, But it actually compiles fine, leading me to believe that I have a compile-time check in place, when actually I don't.
Advantage
Protects users from assuming that some const code is evaluated when it actually isnt.
Drawbacks
No response
Example
struct Foo<const C: char>;
impl<const C: char> Foo<C> {
// This const check should fail for 'x', but it isn't evaluated.
const _CHECK: () = assert!(C != 'x');
fn new() -> Self {
// Uncommenting the next line triggers the compile-time error as expected:
// let _ = Self::CHECK;
Foo
}
}
fn main() {
// This should fail (C == 'x'), but compiles fine.
let _ = Foo::<'x'>::new();
println!("it compiled!")
}
This also does not work:
struct Foo<const C: char>;
impl<const C: char> Foo<C> {
const fn _check() {
assert!(C != 'x');
}
fn new() -> Self {
Foo
}
}
fn main() {
// This should fail (C == 'x'), but compiles fine.
let _ = Foo::<'x'>::new();
println!("it compiled!")
}
Comparison with existing lints
No response
Additional Context
I searched for duplicate issues: https://github.com/rust-lang/rust-clippy/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20const%20struct%20eval
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce both examples and compare them with the explicitly referenced Self::CHECK access, confirming when const evaluation occurs. Then determine the intended Clippy diagnostic and its scope for const assertions in generic structs and associated const functions; done requires an agreed behavior and regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100