Skip to content

Inconsistent const_err on index access to array in polymorphic functions #65005

Closed
@197g

Description

@197g

Stable currently allows indexing an array with invalid constant indices in polymorphic functions without warnings. A recently nightly change has apparently changed this behaviour into an error. The following builds on stable without warnings but errors on nightly:

pub fn bugs(a: &mut [u8; 4], _: impl Fn(u8)) {
    a[4];
}

It should be noted that in monomorphic functions this is already an error in both version, though I do not know when this was introduced. That is, this is an error:

pub fn bugs(a: &mut [u8; 4]) {
    a[4];
    // error: index out of bounds: the len is 4 but the index is 4
}

It is also an error (in both versions) if the actual access is statically unreachable:

pub fn bugs(a: &mut [u8; 4]) {
    if false {
        a[4];
    }
}

Note: This breaks image and everything that depends on it, many versions.

The image crate makes use of the index access in a macro where the alpha components of a pixel are accessed (see here). It properly guards this with a check on the component number but, statically speaking, the access exists in the CFG even for types without alpha components where the index is of course invalid. This seems like a regression that would usually have an incompatibility warning period.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions