Skip to content

*mut T should coerce to *const T #16926

Closed
@petrochenkov

Description

@petrochenkov

Currently only the last line in the example below doesn't compile.

fn fref(_: &u8) {}
fn fptr(_: *const u8) {}

let mut v = 0u8;
fref(&v);
fref(&mut v);
fptr(&v as *const u8);
fptr(&mut v as *mut u8); // error: mismatched types: expected `*const u8`, found `*mut u8` (values differ in mutability)

The conversion *mut T -> *const T is safe and consistent both with Rust references (&mut T coerces to &T) and C pointers (T* is implicitly convertible to const T*). I don't see the reasons why it is not allowed.

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