Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP ref_as_ptr: things that cannot const drop #12255

Closed
matthiaskrgr opened this issue Feb 10, 2024 · 0 comments · Fixed by #12260
Closed

FP ref_as_ptr: things that cannot const drop #12255

matthiaskrgr opened this issue Feb 10, 2024 · 0 comments · Fixed by #12260
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 10, 2024
bors added a commit that referenced this issue Feb 24, 2024
Take lifetime extension into account in `ref_as_ptr`

fixes #12255

This should be merged upstream as well. Changing `let x = &temp as *const _` into `let x = from_ref(&temp)` is UB.

changelog: `ref_as_ptr`: Take lifetime extension into account
bors added a commit that referenced this issue Feb 24, 2024
Take lifetime extension into account in `ref_as_ptr`

fixes #12255

This should be merged upstream as well. Changing `let x = &temp as *const _` into `let x = from_ref(&temp)` is UB.

changelog: `ref_as_ptr`: Take lifetime extension into account
@bors bors closed this as completed in 0597838 Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant