Auto merge of #108611 - davidtwco:issue-94223-external-abi-fn-ptr-in-internal-abi-fn, r=jackh726

lint/ctypes: ext. abi fn-ptr in internal abi fn

Fixes #94223.

- In the improper ctypes lint, instead of skipping functions with internal ABIs, check that the signature doesn't contain any fn-ptr types with external ABIs that aren't FFI-safe.
- When computing the ABI for fn-ptr types, remove an `unwrap` that assumed FFI-safe types in foreign fn-ptr types.
  - I'm not certain that this is the correct approach.
This commit is contained in:
bors 2023-07-03 20:30:28 +00:00
commit 0ab38e95bb
7 changed files with 324 additions and 19 deletions

View file

@ -1,5 +1,5 @@
// run-pass
#![allow(improper_ctypes)]
#![allow(improper_ctypes, improper_ctypes_definitions)]
// ignore-wasm32-bare no libc to test ffi with

View file

@ -0,0 +1,8 @@
// check-pass
#![allow(improper_ctypes_definitions)]
#![crate_type = "lib"]
// Check that computing the fn abi for `bad`, with a external ABI fn ptr that is not FFI-safe, does
// not ICE.
pub fn bad(f: extern "C" fn([u8])) {}

View file

@ -1,5 +1,6 @@
// run-pass
#![allow(improper_ctypes_definitions)]
#![allow(non_camel_case_types)]
#![allow(dead_code)]
#![allow(unused_mut)]

View file

@ -0,0 +1,42 @@
#![crate_type = "lib"]
#![deny(improper_ctypes_definitions)]
pub fn bad(f: extern "C" fn([u8])) {}
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
pub fn bad_twice(f: Result<extern "C" fn([u8]), extern "C" fn([u8])>) {}
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
//~^^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
struct BadStruct(extern "C" fn([u8]));
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
enum BadEnum {
A(extern "C" fn([u8])),
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
}
enum BadUnion {
A(extern "C" fn([u8])),
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
}
type Foo = extern "C" fn([u8]);
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
pub struct FfiUnsafe;
#[allow(improper_ctypes_definitions)]
extern "C" fn f(_: FfiUnsafe) {
unimplemented!()
}
pub static BAD: extern "C" fn(FfiUnsafe) = f;
//~^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
//~^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
//~^^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
pub const BAD_CONST: extern "C" fn(FfiUnsafe) = f;
//~^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe

View file

@ -0,0 +1,126 @@
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:4:15
|
LL | pub fn bad(f: extern "C" fn([u8])) {}
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
note: the lint level is defined here
--> $DIR/lint-ctypes-94223.rs:2:9
|
LL | #![deny(improper_ctypes_definitions)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:7:28
|
LL | pub fn bad_twice(f: Result<extern "C" fn([u8]), extern "C" fn([u8])>) {}
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:7:49
|
LL | pub fn bad_twice(f: Result<extern "C" fn([u8]), extern "C" fn([u8])>) {}
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:11:18
|
LL | struct BadStruct(extern "C" fn([u8]));
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:15:7
|
LL | A(extern "C" fn([u8])),
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:20:7
|
LL | A(extern "C" fn([u8])),
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:24:12
|
LL | type Foo = extern "C" fn([u8]);
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:34:17
|
LL | pub static BAD: extern "C" fn(FfiUnsafe) = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^
error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:37:30
|
LL | pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^
error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:37:56
|
LL | pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^
error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:41:22
|
LL | pub const BAD_CONST: extern "C" fn(FfiUnsafe) = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^
error: aborting due to 11 previous errors