Skip to content

ptr-as-ptr breaks inference #11066

Description

@matthiaskrgr

Summary

.

Reproducer

I tried this code:

#![allow(dead_code, unused_variables)]
#![allow(clippy::unnecessary_cast)]

/// Should not trigger an ICE in `SpanlessEq` / `consts::constant`
///
/// Issue: https://github.com/rust-lang/rust-clippy/issues/1782
use std::{mem, ptr};

fn spanless_eq_ice() {
    let txt = "something";
    match txt {
        "something" => unsafe {
            ptr::write(
                ptr::null_mut() as *mut u32,
                mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
            )
        },
        _ => unsafe {
            ptr::write(
                ptr::null_mut() as *mut u32,
                mem::transmute::<[u8; 4], _>([13, 246, 24, 255]),
            )
        },
    }
}

fn main() {}

I expected to see this happen:
Clippy suggests

warning: `as` casting between raw pointers without changing its mutability
  --> ./src/tools/clippy/tests/ui/crashes/ice-1782.rs:14:17
   |
14 |                 ptr::null_mut() as *mut u32,
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr::null_mut().cast::<u32>()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
   = note: requested on the command line with `-W clippy::ptr-as-ptr`

Instead, this happened:

This does not build:

#![allow(dead_code, unused_variables)]
#![allow(clippy::unnecessary_cast)]

/// Should not trigger an ICE in `SpanlessEq` / `consts::constant`
///
/// Issue: https://github.com/rust-lang/rust-clippy/issues/1782
use std::{mem, ptr};

fn spanless_eq_ice() {
    let txt = "something";
    match txt {
        "something" => unsafe {
            ptr::write(
				ptr::null_mut().cast::<u32>(),
                mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
            )
        },
        _ => unsafe {
            ptr::write(
                ptr::null_mut() as *mut u32,
                mem::transmute::<[u8; 4], _>([13, 246, 24, 255]),
            )
        },
    }
}

fn main() {}
warning: type annotations needed
  --> ./src/tools/clippy/tests/ui/crashes/ice-1782.rs:14:21
   |
14 |                 ptr::null_mut().cast::<u32>(),
   |                                 ^^^^
   |
   = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
   = note: for more information, see issue #46906 <https://github.com/rust-lang/rust/issues/46906>
   = note: `#[warn(tyvar_behind_raw_pointer)]` on by default

error[E0283]: type annotations needed
   --> ./src/tools/clippy/tests/ui/crashes/ice-1782.rs:14:5
    |
14  |                 ptr::null_mut().cast::<u32>(),
    |                 ^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `null_mut`
    |
    = note: cannot satisfy `_: std::ptr::Thin`
note: required by a bound in `std::ptr::null_mut`
   --> /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:543:35
    |
543 | pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
    |                                   ^^^^ required by this bound in `null_mut`
help: consider specifying the generic argument
    |
14  |                 ptr::null_mut::<T>().cast::<u32>(),
    |                              +++++

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0283`.

Version

rustc 1.72.0-nightly (6162f6f12 2023-07-01)
binary: rustc
commit-hash: 6162f6f12339aa81fe16b8a64644ead497e411b2
commit-date: 2023-07-01
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

Additional Labels

No response

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions