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

Usage of pattern on lhs of assignment results in unsafety check for raw pointer deferences being skipped #107288

Closed
Aiden2207 opened this issue Jan 25, 2023 · 5 comments
Labels
C-bug Category: This is a bug.

Comments

@Aiden2207
Copy link

Aiden2207 commented Jan 25, 2023

I tried this code:

fn main() {
    let _ = *(0 as *const ());
}

playground

I expected to see this happen: A compiler error, dereferencing pointers is unsafe, and no unsafe block is provided.
Instead, this happened:

   Compiling playground v0.0.1 (/playground)
warning: dereferencing a null pointer
 --> src/main.rs:2:13
  |
2 |     let _ = *(0 as *const ());
  |             ^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
  |
  = note: `#[warn(deref_nullptr)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.97s
     Running `target/debug/playground`

As far as I can tell, this code erroneously compiles for any Copy type, so long as the pattern doesn't bind anything to an identifier. Calls to unsafe functions cause a compiler error as normal.

Meta

Works on the playground when I tested with stable-1.66.1, beta-1.67.0-beta.10, and nightly-1.69.0 (2023-01-23)

@Aiden2207 Aiden2207 added the C-bug Category: This is a bug. label Jan 25, 2023
@steffahn
Copy link
Member

steffahn commented Jan 25, 2023

Looks like a duplicate of #79735

Edit: Maybe it isn't an exact duplicate, but the behavior is one that's sufficiently tracked in that other issue already.

Edit2: Actually, it isn't sufficiently tracked in the other issue. The code here gives a warning claiming "undefined behavior", so in case the code is considered safe, we would still have a diagnostic issue.

@steffahn
Copy link
Member

Ah, there's also #80059. But that discussion does not encounter the "this code causes undefined behavior" warning either.

@CAD97
Copy link
Contributor

CAD97 commented Jan 25, 2023

That warning feels like it's coming from const evaluation (rvalue promotion). I don't know if it actually is, but I would not be surprised if promotion/promotability is involved here somehow.

@lukas-code
Copy link
Member

This will be fixed by #102256.

@WaffleLapkin
Copy link
Member

This is in fact fixed by #102256 on the latest stable:

error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
 --> src/main.rs:2:13
  |
2 |     let _ = *(0 as *const ());
  |             ^^^^^^^^^^^^^^^^^ dereference of raw pointer
  |
  = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants