Skip to content

Missing index out of bounds error #48920

Closed

Description

@varkor

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

Activity

  1. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Mar 10, 2018
  2. oli-obk commented on Mar 11, 2018

    @oli-obk
    Contributor

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

  3. self-assigned this
    on Mar 11, 2018
  4. varkor commented on Mar 11, 2018

    @varkor
    ContributorAuthor

    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.

  5. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on May 14, 2018
  6. added a commit that references this issue on Jun 5, 2018
    54cb13d
  7. steveklabnik commented on Oct 21, 2019

    @steveklabnik
    Contributor

    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?

  8. oli-obk commented on Oct 21, 2019

    @oli-obk
    Contributor

    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.

  9. paolieri commented on Sep 5, 2022

    @paolieri

    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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions