Repository: rust
Issue: False positive for path statement with no effect; removing path would hide bug
Labels: A-diagnostics,T-compiler

### Code

```rust
pub trait SameSize {
    const CHECK_SIZE: ();
}

impl<Src: Sized, Dst: Sized> SameSize for (Src, Dst) {
    const CHECK_SIZE: () = assert!(size_of::<Src>() == size_of::<Dst>());
}

pub fn cast_ptr<Src, Dst>(ptr: *const Src) -> *const Dst
where
    (Src, Dst): SameSize,
{
    // Force the size check to be evaluated at compile time
    <(Src, Dst) as SameSize>::CHECK_SIZE;

    ptr.cast()
}

fn main() {
    let ptr: *const u32 = &1;

    let _works = cast_ptr::<_, core::num::NonZero<u32>>(ptr);

    let _fails = cast_ptr::<_, u64>(ptr); // Compile-time error
}
```

### Current output

```
Compiling playground v0.0.1 (/playground)
warning: path statement with no effect
  --> src/main.rs:14:5
   |
14 |     <(Src, Dst) as SameSize>::CHECK_SIZE;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(path_statements)]` on by default

error[E0080]: evaluation of `<(u32, u64) as SameSize>::CHECK_SIZE` failed
 --> src/main.rs:6:28
  |
6 |     const CHECK_SIZE: () = assert!(size_of::<Src>() == size_of::<Dst>());
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: size_of::<Src>() == size_of::<Dst>()', src/main.rs:6:28
  |
  = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
  --> src/main.rs:14:5
   |
14 |     <(Src, Dst) as SameSize>::CHECK_SIZE;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: the above error was encountered while instantiating `fn cast_ptr::<u32, u64>`
  --> src/main.rs:24:18
   |
24 |     let _fails = cast_ptr::<_, u64>(ptr); // Compile-time error
   |                  ^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0080`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted
```

### Desired output

```
Compiling playground v0.0.1 (/playground)
error[E0080]: evaluation of `<(u32, u64) as SameSize>::CHECK_SIZE` failed
 --> src/main.rs:6:28
  |
6 |     const CHECK_SIZE: () = assert!(size_of::<Src>() == size_of::<Dst>());
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: size_of::<Src>() == size_of::<Dst>()', src/main.rs:6:28
  |
  = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
  --> src/main.rs:14:5
   |
14 |     <(Src, Dst) as SameSize>::CHECK_SIZE;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: the above error was encountered while instantiating `fn cast_ptr::<u32, u64>`
  --> src/main.rs:24:18
   |
24 |     let _fails = cast_ptr::<_, u64>(ptr); // Compile-time error
   |                  ^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (bin "playground") due to 1 previous error
```

### Rationale and extra context

The code here checks that the source pointee and destination pointee have the same size. Thus the `_works` line should be fine as `u32` and `NonZero<u32>` have the same size, but the `_fails` line should cause a compile-time error as `u32` and `u64` have different sizes.

The path statement `<(Src, Dst) as SameSize>::CHECK_SIZE;` is required for the constant `CHECK_SIZE` to be checked, otherwise there never is a check at compile time, and the `_fails` line would fail to catch a mismatch. Removing the path statement would cause the compilation to erroneously not fail.

### Other cases

_No response_

### Rust Version

```
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7
```

### Anything else?

_No response_