Skip to content

Document the behavior of pointer to smaller int casts #729

Open
@gnzlbg

Description

@gnzlbg

Playground:

let x = 42;
let z = &x as *const _ as u8;

As @rkruppe pointed out, this snippet casts a pointer to an int in which the pointer does not fit. AFAICT, the behavior of this operation is not guaranteed anywhere - I suppose it does a truncation. We should document what it does.

cc @Centril @RalfJung


I personally would probably prefer if it was at least a warning that suggests people to write as X as u8 instead, where X is a type in which the pointer precisely fits, e.g., usize and/or u{target_pointer_width} (e.g. u64) - if that is what that cast actually happens to mean, but this is an issue that should be better discussed in rust-lang/rust, once we know what that cast means.

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