Skip to content
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

Skip lint for assertion evaluated to false #119958

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/rustc_mir_transform/src/const_prop_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
let value = &self.eval_operand(cond, location)?;
trace!("assertion on {:?} should be {:?}", value, expected);

let expected = Scalar::from_bool(expected);
let value_const = self.use_ecx(location, |this| this.ecx.read_scalar(value))?;

if value_const == Scalar::from_bool(false) {
// Skip the assertion if the condition is evaluated to false.
return None;
}

let expected = Scalar::from_bool(expected);

if expected != value_const {
// Poison all places this operand references so that further code
// doesn't use the invalid value
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/const_prop/issue-119908.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// run-pass
fn main() {
let x: [i32; 0] = [];
if x.len() > 0 {
x[0];
Copy link
Member

@matthiaskrgr matthiaskrgr Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this trigger an unreachable/dead code warning?

Copy link
Author

@barabadzhi barabadzhi Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, lint is omitted for the whole block, as it will never run anyway.

}
}
Loading