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

Missing index out of bounds error #48920

Closed
varkor opened this issue Mar 10, 2018 · 5 comments · Fixed by #51308
Closed

Missing index out of bounds error #48920

varkor opened this issue Mar 10, 2018 · 5 comments · Fixed by #51308
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@varkor
Copy link
Member

varkor commented Mar 10, 2018

The following example no longer throws a warning about indices being out of bounds, on master:

fn main() {
    let a: [i32; 1] = [0; 1];
    a[1];
}

I expect a index out of bounds: the len is 1 but the index is 1 error. I suspect this is due to #46882.

cc @oli-obk

@Centril Centril added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints labels Mar 10, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Mar 11, 2018

Yea, there's a FIXME comment in librustc_mir::transform:: const_prop for this.

@varkor
Copy link
Member Author

varkor commented Mar 11, 2018

I imagine it was specific to the old implementation, but just a note that we want to make sure #48900 doesn't reappear when this is reimplemented.

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 14, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 5, 2018
…ck, r=oli-obk

Check array indices in constant propagation

Previously, uses of constant weren't correctly propagated.
This fixes rust-lang#48920.

r? @oli-obk because you suggested it
@steveklabnik
Copy link
Member

This code still errors, but this case doesn't:

fn main() {
    let a = [1, 2, 3];
    let index = 10;
    let element = a[index];
    println!("element is {}", element);
}

is this expected?

@oli-obk
Copy link
Contributor

oli-obk commented Oct 21, 2019

yes. Constant propagation does not cross variables (for now). To do this correctly we'll need to detect loops and conditions. Without such a detection mechanism our current implemementation will do a few bogus things.

@paolieri
Copy link

paolieri commented Sep 5, 2022

Since rustc 1.61, the following code is not giving a compile-time error:

fn main() {
    let xs = [10, 20, 30];
    println!("{}", xs[10]);

    let _n = xs.len();
}

If I uncomment the last line, the compile-time error shows up.
Is this right? I would expect the compile-time error to show up in both cases (this is what happens for rustc < 1.61).

cc @oli-obk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants