Skip to content

[Nightly Regression] False positive warning with constants in conditional #47354

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

Closed
leonardo-m opened this issue Jan 11, 2018 · 7 comments
Closed
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

leonardo-m commented Jan 11, 2018

This is modified code from a real regression in my code:

#![allow(unused_variables)]
fn main() {
    const N: usize = 1_000;
    const M: usize = 4;
    const V: [u32; M] = [1, 2, 3, 4];

    let x = if N <= M {
        V[N - 1]
    } else {
        0
    };
}
warning: this expression will panic at run-time
 --> C:\lavoro\bugs\test.rs:9:9
  |
9 |         INIT[N - 1]
  |         ^^^^^^^^^^^ index out of bounds: the len is 4 but the index is 999



rustc 1.25.0-nightly (f62f77403 2018-01-10)
binary: rustc
commit-hash: f62f774035735a06c880c48c0b9017fcc0577e33
commit-date: 2018-01-10
host: x86_64-pc-windows-gnu
release: 1.25.0-nightly
LLVM version: 4.0

D language and C++17 avoid such problems using "static if" and "if constexpr".

@leonardo-m leonardo-m changed the title False positive warning with constants in conditional [Nightly Regression] False positive warning with constants in conditional Jan 11, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Jan 11, 2018

That's very likely from my miri changes. I touched the very code emitting this message and improved the code that can be evaluated. This needs an RFC (which I'm currently writing).

@pietroalbini pietroalbini added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-fn C-bug Category: This is a bug. labels Feb 6, 2018
@leonardo-m
Copy link
Author

Fixed since few Nightlies.

@leonardo-m
Copy link
Author

leonardo-m commented Jul 22, 2018

The problem is back (rustc 1.29.0-nightly 874dec2 2018-07-21), but it's not visible compiling with just --emit=metadata (as in Issue #51491 ):

error: index out of bounds: the len is 4 but the index is 999
 --> test.rs:8:9
  |
8 |         V[N - 1]
  |         ^^^^^^^^
  |
  = note: #[deny(const_err)] on by default

error: aborting due to previous error

@leonardo-m leonardo-m reopened this Jul 22, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Jul 22, 2018

I'm surprised it was ever fixed. We have a test reproducing this and it hasn't been touched

@kovaxis
Copy link

kovaxis commented Mar 1, 2019

I've just hit this issue in my own code, and the problem is reproducible just by copy-pasting the above code into the Rust playground in 1.33.0-stable.

I've looked around and found #52966, which was about a similar regression and was closed. If this behavior is intended, then this issue can be closed.

But just to clarify, are lints causing compile-time errors a thing now? It feels like they should not, given that it makes ie. changing a let binding to a const binding a compile-time error that is not a run-time error. In my mind (ideally) lints would err on the "false positive" side and assume things about the code (since they can be easily supressed). On the other hand errors would err on the "safe" side and only block compilation when it is certain that something is wrong.

@steveklabnik
Copy link
Member

Triage: no change

@RalfJung RalfJung added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed A-const-fn labels Dec 1, 2024
@zmt00
Copy link

zmt00 commented Apr 18, 2025

This appears to be fixed on stable rust, dating back to rust 1.70: godbolt

@oli-obk oli-obk closed this as completed Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants