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
```
This commit is contained in:
bors 2021-10-03 00:41:49 +00:00
commit c70b35efd8
5 changed files with 69 additions and 45 deletions

View File

@ -1056,6 +1056,15 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
FfiSafe
}
ty::RawPtr(ty::TypeAndMut { ty, .. })
if match ty.kind() {
ty::Tuple(tuple) => tuple.is_empty(),
_ => false,
} =>
{
FfiSafe
}
ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => {
self.check_type_for_ffi(cache, ty)
}

View File

@ -66,6 +66,10 @@ pub extern "C" fn ptr_type1(size: *const Foo) { }
pub extern "C" fn ptr_type2(size: *const Foo) { }
pub extern "C" fn ptr_unit(p: *const ()) { }
pub extern "C" fn ptr_tuple(p: *const ((),)) { }
pub extern "C" fn slice_type(p: &[u32]) { }
//~^ ERROR: uses type `[u32]`

View File

@ -1,5 +1,5 @@
error: `extern` fn uses type `[u32]`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:69:33
--> $DIR/lint-ctypes-fn.rs:73:33
|
LL | pub extern "C" fn slice_type(p: &[u32]) { }
| ^^^^^^ not FFI-safe
@ -13,7 +13,7 @@ LL | #![deny(improper_ctypes_definitions)]
= note: slices have no C equivalent
error: `extern` fn uses type `str`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:72:31
--> $DIR/lint-ctypes-fn.rs:76:31
|
LL | pub extern "C" fn str_type(p: &str) { }
| ^^^^ not FFI-safe
@ -22,7 +22,7 @@ LL | pub extern "C" fn str_type(p: &str) { }
= note: string slices have no C equivalent
error: `extern` fn uses type `Box<[u8]>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:79:34
--> $DIR/lint-ctypes-fn.rs:83:34
|
LL | pub extern "C" fn boxed_slice(p: Box<[u8]>) { }
| ^^^^^^^^^ not FFI-safe
@ -30,7 +30,7 @@ LL | pub extern "C" fn boxed_slice(p: Box<[u8]>) { }
= note: box cannot be represented as a single pointer
error: `extern` fn uses type `Box<str>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:82:35
--> $DIR/lint-ctypes-fn.rs:86:35
|
LL | pub extern "C" fn boxed_string(p: Box<str>) { }
| ^^^^^^^^ not FFI-safe
@ -38,7 +38,7 @@ LL | pub extern "C" fn boxed_string(p: Box<str>) { }
= note: box cannot be represented as a single pointer
error: `extern` fn uses type `Box<dyn Trait>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:85:34
--> $DIR/lint-ctypes-fn.rs:89:34
|
LL | pub extern "C" fn boxed_trait(p: Box<dyn Trait>) { }
| ^^^^^^^^^^^^^^ not FFI-safe
@ -46,7 +46,7 @@ LL | pub extern "C" fn boxed_trait(p: Box<dyn Trait>) { }
= note: box cannot be represented as a single pointer
error: `extern` fn uses type `char`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:88:32
--> $DIR/lint-ctypes-fn.rs:92:32
|
LL | pub extern "C" fn char_type(p: char) { }
| ^^^^ not FFI-safe
@ -55,7 +55,7 @@ LL | pub extern "C" fn char_type(p: char) { }
= note: the `char` type has no C equivalent
error: `extern` fn uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:91:32
--> $DIR/lint-ctypes-fn.rs:95:32
|
LL | pub extern "C" fn i128_type(p: i128) { }
| ^^^^ not FFI-safe
@ -63,7 +63,7 @@ LL | pub extern "C" fn i128_type(p: i128) { }
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` fn uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:94:32
--> $DIR/lint-ctypes-fn.rs:98:32
|
LL | pub extern "C" fn u128_type(p: u128) { }
| ^^^^ not FFI-safe
@ -71,7 +71,7 @@ LL | pub extern "C" fn u128_type(p: u128) { }
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` fn uses type `(i32, i32)`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:97:33
--> $DIR/lint-ctypes-fn.rs:101:33
|
LL | pub extern "C" fn tuple_type(p: (i32, i32)) { }
| ^^^^^^^^^^ not FFI-safe
@ -80,7 +80,7 @@ LL | pub extern "C" fn tuple_type(p: (i32, i32)) { }
= note: tuples have unspecified layout
error: `extern` fn uses type `(i32, i32)`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:100:34
--> $DIR/lint-ctypes-fn.rs:104:34
|
LL | pub extern "C" fn tuple_type2(p: I32Pair) { }
| ^^^^^^^ not FFI-safe
@ -89,7 +89,7 @@ LL | pub extern "C" fn tuple_type2(p: I32Pair) { }
= note: tuples have unspecified layout
error: `extern` fn uses type `ZeroSize`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:103:32
--> $DIR/lint-ctypes-fn.rs:107:32
|
LL | pub extern "C" fn zero_size(p: ZeroSize) { }
| ^^^^^^^^ not FFI-safe
@ -103,7 +103,7 @@ LL | pub struct ZeroSize;
| ^^^^^^^^^^^^^^^^^^^^
error: `extern` fn uses type `ZeroSizeWithPhantomData`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:106:40
--> $DIR/lint-ctypes-fn.rs:110:40
|
LL | pub extern "C" fn zero_size_phantom(p: ZeroSizeWithPhantomData) { }
| ^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -116,7 +116,7 @@ LL | pub struct ZeroSizeWithPhantomData(PhantomData<i32>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `extern` fn uses type `PhantomData<bool>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:109:51
--> $DIR/lint-ctypes-fn.rs:113:51
|
LL | pub extern "C" fn zero_size_phantom_toplevel() -> PhantomData<bool> {
| ^^^^^^^^^^^^^^^^^ not FFI-safe
@ -124,7 +124,7 @@ LL | pub extern "C" fn zero_size_phantom_toplevel() -> PhantomData<bool> {
= note: composed only of `PhantomData`
error: `extern` fn uses type `fn()`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:114:30
--> $DIR/lint-ctypes-fn.rs:118:30
|
LL | pub extern "C" fn fn_type(p: RustFn) { }
| ^^^^^^ not FFI-safe
@ -133,7 +133,7 @@ LL | pub extern "C" fn fn_type(p: RustFn) { }
= note: this function pointer has Rust-specific calling convention
error: `extern` fn uses type `fn()`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:117:31
--> $DIR/lint-ctypes-fn.rs:121:31
|
LL | pub extern "C" fn fn_type2(p: fn()) { }
| ^^^^ not FFI-safe
@ -142,7 +142,7 @@ LL | pub extern "C" fn fn_type2(p: fn()) { }
= note: this function pointer has Rust-specific calling convention
error: `extern` fn uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:122:39
--> $DIR/lint-ctypes-fn.rs:126:39
|
LL | pub extern "C" fn transparent_i128(p: TransparentI128) { }
| ^^^^^^^^^^^^^^^ not FFI-safe
@ -150,7 +150,7 @@ LL | pub extern "C" fn transparent_i128(p: TransparentI128) { }
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` fn uses type `str`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:125:38
--> $DIR/lint-ctypes-fn.rs:129:38
|
LL | pub extern "C" fn transparent_str(p: TransparentStr) { }
| ^^^^^^^^^^^^^^ not FFI-safe
@ -159,7 +159,7 @@ LL | pub extern "C" fn transparent_str(p: TransparentStr) { }
= note: string slices have no C equivalent
error: `extern` fn uses type `PhantomData<bool>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:171:43
--> $DIR/lint-ctypes-fn.rs:175:43
|
LL | pub extern "C" fn unused_generic2<T>() -> PhantomData<bool> {
| ^^^^^^^^^^^^^^^^^ not FFI-safe
@ -167,7 +167,7 @@ LL | pub extern "C" fn unused_generic2<T>() -> PhantomData<bool> {
= note: composed only of `PhantomData`
error: `extern` fn uses type `Vec<T>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:184:39
--> $DIR/lint-ctypes-fn.rs:188:39
|
LL | pub extern "C" fn used_generic4<T>(x: Vec<T>) { }
| ^^^^^^ not FFI-safe
@ -176,7 +176,7 @@ LL | pub extern "C" fn used_generic4<T>(x: Vec<T>) { }
= note: this struct has unspecified layout
error: `extern` fn uses type `Vec<T>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:187:41
--> $DIR/lint-ctypes-fn.rs:191:41
|
LL | pub extern "C" fn used_generic5<T>() -> Vec<T> {
| ^^^^^^ not FFI-safe

View File

@ -47,6 +47,8 @@ pub struct ZeroSizeWithPhantomData(::std::marker::PhantomData<i32>);
extern "C" {
pub fn ptr_type1(size: *const Foo); //~ ERROR: uses type `Foo`
pub fn ptr_type2(size: *const Foo); //~ ERROR: uses type `Foo`
pub fn ptr_unit(p: *const ());
pub fn ptr_tuple(p: *const ((),)); //~ ERROR: uses type `((),)`
pub fn slice_type(p: &[u32]); //~ ERROR: uses type `[u32]`
pub fn str_type(p: &str); //~ ERROR: uses type `str`
pub fn box_type(p: Box<u32>); //~ ERROR uses type `Box<u32>`

View File

@ -31,8 +31,17 @@ note: the type is defined here
LL | pub struct Foo;
| ^^^^^^^^^^^^^^^
error: `extern` block uses type `((),)`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:51:25
|
LL | pub fn ptr_tuple(p: *const ((),));
| ^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a struct instead
= note: tuples have unspecified layout
error: `extern` block uses type `[u32]`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:50:26
--> $DIR/lint-ctypes.rs:52:26
|
LL | pub fn slice_type(p: &[u32]);
| ^^^^^^ not FFI-safe
@ -41,7 +50,7 @@ LL | pub fn slice_type(p: &[u32]);
= note: slices have no C equivalent
error: `extern` block uses type `str`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:51:24
--> $DIR/lint-ctypes.rs:53:24
|
LL | pub fn str_type(p: &str);
| ^^^^ not FFI-safe
@ -50,7 +59,7 @@ LL | pub fn str_type(p: &str);
= note: string slices have no C equivalent
error: `extern` block uses type `Box<u32>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:52:24
--> $DIR/lint-ctypes.rs:54:24
|
LL | pub fn box_type(p: Box<u32>);
| ^^^^^^^^ not FFI-safe
@ -59,7 +68,7 @@ LL | pub fn box_type(p: Box<u32>);
= note: this struct has unspecified layout
error: `extern` block uses type `Option<Box<u32>>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:53:28
--> $DIR/lint-ctypes.rs:55:28
|
LL | pub fn opt_box_type(p: Option<Box<u32>>);
| ^^^^^^^^^^^^^^^^ not FFI-safe
@ -68,7 +77,7 @@ LL | pub fn opt_box_type(p: Option<Box<u32>>);
= note: enum has no representation hint
error: `extern` block uses type `char`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:55:25
--> $DIR/lint-ctypes.rs:57:25
|
LL | pub fn char_type(p: char);
| ^^^^ not FFI-safe
@ -77,7 +86,7 @@ LL | pub fn char_type(p: char);
= note: the `char` type has no C equivalent
error: `extern` block uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:56:25
--> $DIR/lint-ctypes.rs:58:25
|
LL | pub fn i128_type(p: i128);
| ^^^^ not FFI-safe
@ -85,7 +94,7 @@ LL | pub fn i128_type(p: i128);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:57:25
--> $DIR/lint-ctypes.rs:59:25
|
LL | pub fn u128_type(p: u128);
| ^^^^ not FFI-safe
@ -93,7 +102,7 @@ LL | pub fn u128_type(p: u128);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `dyn Bar`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:58:26
--> $DIR/lint-ctypes.rs:60:26
|
LL | pub fn trait_type(p: &dyn Bar);
| ^^^^^^^^ not FFI-safe
@ -101,7 +110,7 @@ LL | pub fn trait_type(p: &dyn Bar);
= note: trait objects have no C equivalent
error: `extern` block uses type `(i32, i32)`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:59:26
--> $DIR/lint-ctypes.rs:61:26
|
LL | pub fn tuple_type(p: (i32, i32));
| ^^^^^^^^^^ not FFI-safe
@ -110,7 +119,7 @@ LL | pub fn tuple_type(p: (i32, i32));
= note: tuples have unspecified layout
error: `extern` block uses type `(i32, i32)`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:60:27
--> $DIR/lint-ctypes.rs:62:27
|
LL | pub fn tuple_type2(p: I32Pair);
| ^^^^^^^ not FFI-safe
@ -119,7 +128,7 @@ LL | pub fn tuple_type2(p: I32Pair);
= note: tuples have unspecified layout
error: `extern` block uses type `ZeroSize`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:61:25
--> $DIR/lint-ctypes.rs:63:25
|
LL | pub fn zero_size(p: ZeroSize);
| ^^^^^^^^ not FFI-safe
@ -133,7 +142,7 @@ LL | pub struct ZeroSize;
| ^^^^^^^^^^^^^^^^^^^^
error: `extern` block uses type `ZeroSizeWithPhantomData`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:62:33
--> $DIR/lint-ctypes.rs:64:33
|
LL | pub fn zero_size_phantom(p: ZeroSizeWithPhantomData);
| ^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -146,7 +155,7 @@ LL | pub struct ZeroSizeWithPhantomData(::std::marker::PhantomData<i32>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `extern` block uses type `PhantomData<bool>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:65:12
--> $DIR/lint-ctypes.rs:67:12
|
LL | -> ::std::marker::PhantomData<bool>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -154,7 +163,7 @@ LL | -> ::std::marker::PhantomData<bool>;
= note: composed only of `PhantomData`
error: `extern` block uses type `fn()`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:66:23
--> $DIR/lint-ctypes.rs:68:23
|
LL | pub fn fn_type(p: RustFn);
| ^^^^^^ not FFI-safe
@ -163,7 +172,7 @@ LL | pub fn fn_type(p: RustFn);
= note: this function pointer has Rust-specific calling convention
error: `extern` block uses type `fn()`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:67:24
--> $DIR/lint-ctypes.rs:69:24
|
LL | pub fn fn_type2(p: fn());
| ^^^^ not FFI-safe
@ -172,7 +181,7 @@ LL | pub fn fn_type2(p: fn());
= note: this function pointer has Rust-specific calling convention
error: `extern` block uses type `Box<u32>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:68:28
--> $DIR/lint-ctypes.rs:70:28
|
LL | pub fn fn_contained(p: RustBadRet);
| ^^^^^^^^^^ not FFI-safe
@ -181,7 +190,7 @@ LL | pub fn fn_contained(p: RustBadRet);
= note: this struct has unspecified layout
error: `extern` block uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:69:32
--> $DIR/lint-ctypes.rs:71:32
|
LL | pub fn transparent_i128(p: TransparentI128);
| ^^^^^^^^^^^^^^^ not FFI-safe
@ -189,7 +198,7 @@ LL | pub fn transparent_i128(p: TransparentI128);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `str`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:70:31
--> $DIR/lint-ctypes.rs:72:31
|
LL | pub fn transparent_str(p: TransparentStr);
| ^^^^^^^^^^^^^^ not FFI-safe
@ -198,7 +207,7 @@ LL | pub fn transparent_str(p: TransparentStr);
= note: string slices have no C equivalent
error: `extern` block uses type `Box<u32>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:71:30
--> $DIR/lint-ctypes.rs:73:30
|
LL | pub fn transparent_fn(p: TransparentBadFn);
| ^^^^^^^^^^^^^^^^ not FFI-safe
@ -207,7 +216,7 @@ LL | pub fn transparent_fn(p: TransparentBadFn);
= note: this struct has unspecified layout
error: `extern` block uses type `[u8; 8]`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:72:27
--> $DIR/lint-ctypes.rs:74:27
|
LL | pub fn raw_array(arr: [u8; 8]);
| ^^^^^^^ not FFI-safe
@ -216,7 +225,7 @@ LL | pub fn raw_array(arr: [u8; 8]);
= note: passing raw arrays by value is not FFI-safe
error: `extern` block uses type `Option<UnsafeCell<extern "C" fn()>>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:74:26
--> $DIR/lint-ctypes.rs:76:26
|
LL | pub fn no_niche_a(a: Option<UnsafeCell<extern fn()>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -225,7 +234,7 @@ LL | pub fn no_niche_a(a: Option<UnsafeCell<extern fn()>>);
= note: enum has no representation hint
error: `extern` block uses type `Option<UnsafeCell<&i32>>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:76:26
--> $DIR/lint-ctypes.rs:78:26
|
LL | pub fn no_niche_b(b: Option<UnsafeCell<&i32>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -234,7 +243,7 @@ LL | pub fn no_niche_b(b: Option<UnsafeCell<&i32>>);
= note: enum has no representation hint
error: `extern` block uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:79:34
--> $DIR/lint-ctypes.rs:81:34
|
LL | pub static static_u128_type: u128;
| ^^^^ not FFI-safe
@ -242,12 +251,12 @@ LL | pub static static_u128_type: u128;
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:80:40
--> $DIR/lint-ctypes.rs:82:40
|
LL | pub static static_u128_array_type: [u128; 16];
| ^^^^^^^^^^ not FFI-safe
|
= note: 128-bit integers don't currently have a known stable ABI
error: aborting due to 26 previous errors
error: aborting due to 27 previous errors