Skip to content

No subobject provenance for arrays/slices #622

Description

@RalfJung

#419 is a big topic, but there is one specific form of subobject provenance which is violated particularly often: using a reference to one element of an array/slice to access another. Even C allows this, so IMO we should commit to also allowing this in Rust.

Concretely, this means the following code has no UB:

fn main() {
    let x = &mut [1, 2, 3];
    let ptr = &mut x[0] as *mut i32;
    unsafe { ptr.add(1).write(5) };
    
    let ptr = &x[1] as *const i32;
    let _val = unsafe { ptr.sub(1).read() };
}

This is based on the observation that violations of such subobject provenance are the major kind of Stacked Borrows UB that Miri finds. I'd like to make Miri stop report this as UB, and for this I'd like to be sure that indeed we don't want to make this UB.

See rust-lang/reference#2338 for Reference-level wording of this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions