]> git.lizzy.rs Git - rust.git/commit
Auto merge of #84267 - dtolnay:ptrunit, r=nagisa
authorbors <bors@rust-lang.org>
Sun, 3 Oct 2021 00:41:49 +0000 (00:41 +0000)
committerbors <bors@rust-lang.org>
Sun, 3 Oct 2021 00:41:49 +0000 (00:41 +0000)
commitc70b35efd8ed4d2e85a468c6519300c16609cdab
tree00bb25b0a64bd85ee9e66c0b9b492b311d45905d
parent2801a770ce3a21a53769180d9815240ac555cbef
parentabfad74ec6326b806509a676b6527e8d645d4fdf
Auto merge of #84267 - dtolnay:ptrunit, r=nagisa

Make *const (), *mut () okay for FFI

Pointer-to-() is used occasionally in the standard library to mean "pointer to none-of-your-business". Examples:

- `RawWakerVTable::new` https://doc.rust-lang.org/1.51.0/std/task/struct.RawWakerVTable.html#method.new
- `<*const T>::to_raw_parts` https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.to_raw_parts

I believe it's useful for the same purpose in FFI signatures, even while `()` itself is not FFI safe. The following should be allowed:

```rust
extern "C" {
    fn demo(pc: *const (), pm: *mut ());
}
```

Prior to this PR, those pointers were not considered okay for an extern signature.

```console
warning: `extern` block uses type `()`, which is not FFI-safe
 --> src/main.rs:2:17
  |
2 |     fn demo(pc: *const (), pm: *mut ());
  |                 ^^^^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes)]` on by default
  = help: consider using a struct instead
  = note: tuples have unspecified layout

warning: `extern` block uses type `()`, which is not FFI-safe
 --> src/main.rs:2:32
  |
2 |     fn demo(pc: *const (), pm: *mut ());
  |                                ^^^^^^^ not FFI-safe
  |
  = help: consider using a struct instead
  = note: tuples have unspecified layout
```
compiler/rustc_lint/src/types.rs