rust-lang/rust-clippy

FP ref_as_ptr: things that cannot const drop

matthiaskrgr opened this issue · 0 comments

Summary

.

Lint Name

ref_as_ptr

Reproducer

I tried this code:

// check-pass

// This is a regression test for a `span_delayed_bug` during interning when a constant
// evaluates to a (non-dangling) raw pointer.

#[warn(clippy::ref_as_ptr)]

const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;

fn main() {}

I saw this happen:

warning: constant `CONST_RAW` is never used
 --> src/main.rs:8:7
  |
8 | const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;
  |       ^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: reference as raw pointer
 --> src/main.rs:8:36
  |
8 | const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;
  |                                    ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(&Vec::new())`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr
note: the lint level is defined here

I expected to see this happen:
The applied sugg does not compile

error[E0493]: destructor of `std::vec::Vec<i32>` cannot be evaluated at compile-time
 --> src/main.rs:8:56
  |
8 | const CONST_RAW: *const Vec<i32> = std::ptr::from_ref(&Vec::new());
  |                                                        ^^^^^^^^^^- value is dropped here
  |                                                        |
  |                                                        the destructor for this type cannot be evaluated in constants

error: aborting due to 1 previous error

Version

rustc 1.78.0-nightly (d44e3b95c 2024-02-09)
binary: rustc
commit-hash: d44e3b95cb9d410d89cb8ab3233906a33f43756a
commit-date: 2024-02-09
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 17.0.6u

Additional Labels

No response