-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Report const index out-of-bound condition earlier #3170
Comments
Apparently the “error” this produces doesn't cause compilation to fail — code will be generated and rustc will return 0. LLVM will complain if the index is physically out of bounds, and this is currently always equivalent to an out-of-bounds index (I think), but that won't be the case for const slices. This is especially unexpected during testing, which (except for check-fast, apparently?) doesn't show the output/error on success. |
Not critical for 0.6; de-milestoning |
Nominating for milestone 5, production-ready |
accepted for production-ready milestone |
As an example of what I believe that this is talking about: static a: &'static [int] = &[];
static b: int = a[1];
fn main() {} yields
Seems bad that we're hitting an LLVM assertion at all. |
Accepted for P-low. |
Triage: @alexcrichton's (9 month old) example is still syntactically valid (yay!) and still fails with that assertion (boo!). |
I believe this is fixed. |
If you update @alexcrichton's example: #![allow(dead_code)]
const A: &'static [usize] = &[];
const B: usize = A[1];
fn main() {} It now compiles successfully without any complaints. Only once you attempt to use the invalid value #![allow(dead_code)]
const A: &'static [usize] = &[];
const B: usize = A[1];
fn main() {
println!("B={}", B);
}
|
Yup, seems good. |
wait, @JustAPerson 's example does not seem okay to me. Reopening. |
I think this is now fixed for real: $ cat foo.rs
#![allow(dead_code)]
const A: &'static [usize] = &[];
const B: usize = A[1];
fn main() {
println!("B={}", B);
}
$ rustc foo.rs
foo.rs:3:18: 3:22 error: const index-expr is out of bounds
foo.rs:3 const B: usize = A[1];
^~~~
error: aborting due to previous error
$ rustc --version
rustc 1.0.0-dev (a691f1eef 2015-04-15) (built 2015-04-15) |
So, sometime in the last 11 days:
soooo seems like it's already regressed? |
Guh, this was my mistake. I made the above report using a toolchain compiled with LLVM assertions disabled (which I didn't know was the default). |
(Hi, I'm trying to help make E-easy issues more accessible to newcomers 😸)
It sounds like in order to reproduce this issue, you have to be using a toolchain compiled with a non-default setting? If so, how does one go about doing that? It's kind of confusing what's supposed to happen here, with all the closing-reopening stuff going on. Could someone clarify what the expected behavior is and how it differs from the current behavior? |
However, you can just use a Rust nightly, since LLVM assertions are enabled there. http://is.gd/X2RztV still fails the assertion on nightly:
|
It looks like the thing I was distressed by in 2013 is fixed: if rustc gives the The other thing my past self mentioned… it sounds as if we used to just pass the index to LLVM even if we knew it was out-of-range, but now we construct an As for the LLVM assertion we're seeing today, I have a guess. After reporting the error, we're doing this: C_undef(type_of::type_of(cx, bt).element_type()) and I think we want something like this (untested): C_undef(val_ty(arr).element_type()) Because if the value being indexed is a slice or pointer, then But, the thing @graydon opened this issue for, and what the comment in the code referencing this issue suggests doing, is to move that check to an earlier stage of compilation. And it looks like #25370/#25570 might wind up more or less accomplishing that? |
This turned up as part of rust-lang#3170. When constructing an `undef` value to return in the error case, we were trying to get the element type of the Rust-level value being indexed instead of the underlying array; when indexing a slice, that's not an array and the LLVM assertion failure reflects this. The regression test is a lightly altered copy of `const-array-oob.rs`.
This turned up as part of rust-lang#3170. When constructing an `undef` value to return in the error case, we were trying to get the element type of the Rust-level value being indexed instead of the underlying array; when indexing a slice, that's not an array and the LLVM assertion failure reflects this. The regression test is a lightly altered copy of `const-array-oob.rs`.
This turned up as part of #3170. When constructing an `undef` value to return in the error case, we were trying to get the element type of the Rust-level value being indexed instead of the underlying array; when indexing a slice, that's not an array and the LLVM assertion failure reflects this. The regression test is a lightly altered copy of `const-array-oob.rs`.
I think this is fixed completely now. @oli-obk ? |
nope, it needs to be solved in |
@oli-obk Is this an easy fix, and if so, do you want to mentor it and leave some hints so that newcomers can try a crack at it? |
While it's an easy fix (you can basically steal https://github.com/rust-lang/rust/blob/master/src/librustc/middle/check_const.rs#L470-L490 and do it for But it'd also be a breaking change, because unused Also it would end up doing all const evaluation twice. Which could be remedied once it becomes a problem by caching constants. Of course the breaking change can be remedied by simply emitting the |
this seems to have been 'solved' (possibly due to MIRI) as it now throws E0080 when used, rather than an LLVM assertion. though it still passes through if the access is unused. |
The missing lint when the const is unused will be fixed by #50110 |
Yea I think we're done with this issue after 6 years 😆 |
a bit of libc test reorganization
This PR modifies the pattern used to exclude files from the copyright check for `expected` files. This ensures we check the copyright in files under `tests/expected/` while it skips the check for `expected` and `*.expected` files. It also adds/modifies copyright headers for some files that weren't being checked until now. Resolves rust-lang#3141
Currently if you try indexing into a const vector, in a const expr, we report the bounds-overrun late in compilation -- during translation. We should notice it earlier, in the const evaluation pass.
The text was updated successfully, but these errors were encountered: