Skip to content

as-ptr-cast-mut causes type error #15259

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::as-ptr-cast-mut

this code:

// FIXME: Without retagging, optimization kills finding this problem
// compile-flags: -Zmir-opt-level=0

#![allow(unused_variables)]

mod safe {
    use std::slice::from_raw_parts_mut;

    pub fn as_mut_slice<T>(self_: &Vec<T>) -> &mut [T] {
        unsafe {
            from_raw_parts_mut(self_.as_ptr() as *mut T, self_.len())
        }
    }
}

fn main() {
    let v = vec![0,1,2];
    let v1 = safe::as_mut_slice(&v);
    let v2 = safe::as_mut_slice(&v);
    v1[1] = 5; //~ ERROR Mut reference with non-reactivatable tag Mut(Uniq
    v1[1] = 6;
}

caused the following diagnostics:

    Checking _buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0 v0.1.0 (/tmp/icemaker_global_tempdir.ROAms2Xd0g8l/icemaker_clippyfix_tempdir.3ep0VPTsXiq1/_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0)
warning: casting the result of `as_ptr` to *mut T
  --> src/main.rs:11:32
   |
11 |             from_raw_parts_mut(self_.as_ptr() as *mut T, self_.len())
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `self_.as_mut_ptr()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#as_ptr_cast_mut
   = note: requested on the command line with `--force-warn clippy::as-ptr-cast-mut`

warning: `_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0` (bin "_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s

However after applying these diagnostics, the resulting code:

// FIXME: Without retagging, optimization kills finding this problem
// compile-flags: -Zmir-opt-level=0

#![allow(unused_variables)]

mod safe {
    use std::slice::from_raw_parts_mut;

    pub fn as_mut_slice<T>(self_: &mut Vec<T>) -> &mut [T] {
        unsafe {
            from_raw_parts_mut(self_.as_mut_ptr(), self_.len())
        }
    }
}

fn main() {
    let v = vec![0,1,2];
    let v1 = safe::as_mut_slice(&v);
    let v2 = safe::as_mut_slice(&v);
    v1[1] = 5; //~ ERROR Mut reference with non-reactivatable tag Mut(Uniq
    v1[1] = 6;
}

no longer compiled:

    Checking _buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0 v0.1.0 (/tmp/icemaker_global_tempdir.ROAms2Xd0g8l/icemaker_clippyfix_tempdir.3ep0VPTsXiq1/_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0)
error[E0308]: mismatched types
  --> src/main.rs:18:33
   |
18 |     let v1 = safe::as_mut_slice(&v);
   |              ------------------ ^^ types differ in mutability
   |              |
   |              arguments to this function are incorrect
   |
   = note: expected mutable reference `&mut std::vec::Vec<_>`
                      found reference `&std::vec::Vec<{integer}>`
note: function defined here
  --> src/main.rs:9:12
   |
9  |     pub fn as_mut_slice<T>(self_: &mut Vec<T>) -> &mut [T] {
   |            ^^^^^^^^^^^^    ------------------

error[E0308]: mismatched types
  --> src/main.rs:19:33
   |
19 |     let v2 = safe::as_mut_slice(&v);
   |              ------------------ ^^ types differ in mutability
   |              |
   |              arguments to this function are incorrect
   |
   = note: expected mutable reference `&mut std::vec::Vec<_>`
                      found reference `&std::vec::Vec<{integer}>`
note: function defined here
  --> src/main.rs:9:12
   |
9  |     pub fn as_mut_slice<T>(self_: &mut Vec<T>) -> &mut [T] {
   |            ^^^^^^^^^^^^    ------------------

For more information about this error, try `rustc --explain E0308`.
error: could not compile `_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0` (bin "_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0` (bin "_buggy_as_mut_slice_9e94aa8885d2c6d92c921cd8785172ff811cc7a0" test) due to 2 previous errors

Version:

rustc 1.90.0-nightly (855e0fe46 2025-07-11)
binary: rustc
commit-hash: 855e0fe46e68d94e9f6147531b75ac2d488c548e
commit-date: 2025-07-11
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions