Skip to content

Shared reference to a fixed length vector in a struct can be cast to a *mut directly #14845

Closed
@huonw

Description

@huonw
struct X { a: [u8, .. 1], b: u8 }

fn main() { 
    let x = X { a: [0], b: 0 };

    let _fixed = &x.a as *mut u8;

    // let _other = &x.b as *mut _;
    // let local_fixed = [0u8]; let _v = &local_fixed as *mut _;
}

compiles without error. Uncommenting the two similar lines gives the errors I was expecting for _fixed:

<anon>:8:19: 8:23 error: mismatched types: expected `*mut <generic #12>` but found `&u8` (values differ in mutability)
<anon>:8      let _other = &x.b as *mut _;
                           ^~~~
<anon>:9:40: 9:52 error: mismatched types: expected `*mut <generic #14>` but found `&[u8, .. 1]` (values differ in mutability)
<anon>:9      let local_fixed = [0u8]; let _v = &local_fixed as *mut _;
                                                ^~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions