Skip to content

ptr-as-ptr breaks inference #11066

Closed
@matthiaskrgr

Description

@matthiaskrgr
Member

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

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Jul 2, 2023
asquared31415

asquared31415 commented on Jul 4, 2023

@asquared31415
Contributor

Note for anyone reproducing: add #![warn(clippy::ptr_as_ptr)] to the file and use edition 2015.

KisaragiEffective

KisaragiEffective commented on Dec 3, 2023

@KisaragiEffective
Contributor

@rustbot claim

added a commit that references this issue on Dec 3, 2023
2e0afcf
added a commit that references this issue on Dec 3, 2023
8eea8b1
added 2 commits that reference this issue on Dec 3, 2023

Auto merge of #11913 - KisaragiEffective:fix/ptr-as-ptr-with-null, r=…

018cf8b

Auto merge of #11913 - KisaragiEffective:fix/ptr-as-ptr-with-null, r=…

2793e8d
added a commit that references this issue on Dec 12, 2023
0226fad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @matthiaskrgr@asquared31415@KisaragiEffective

    Issue actions

      ptr-as-ptr breaks inference · Issue #11066 · rust-lang/rust-clippy