1
Fork 0

Add ffi tests for pattern types

This commit is contained in:
Oli Scherer 2025-01-24 15:57:13 +00:00
parent c182ce9cbc
commit 644c6948d0
4 changed files with 185 additions and 22 deletions

View file

@ -1,7 +1,7 @@
//@ check-pass
//@ aux-build:external_extern_fn.rs
#![crate_type = "lib"]
#![feature(pattern_type_macro, pattern_types)]
mod redeclared_different_signature {
mod a {
extern "C" {
@ -490,3 +490,42 @@ mod hidden_niche {
}
}
}
mod pattern_types {
mod a {
use std::pat::pattern_type;
#[repr(transparent)]
struct NonZeroUsize(pattern_type!(usize is 1..));
extern "C" {
fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
//~^ WARN not FFI-safe
fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
//~^ WARN not FFI-safe
fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
//~^ WARN not FFI-safe
fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
//~^ WARN not FFI-safe
fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
//~^ WARN not FFI-safe
fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
//~^ WARN not FFI-safe
}
}
mod b {
extern "C" {
// If there's a clash in either of these cases you're either gaining an incorrect
// invariant that the value is non-zero, or you're missing out on that invariant. Both
// cases are warning for, from both a caller-convenience and optimisation perspective.
fn pt_non_zero_usize() -> usize;
//~^ WARN `pt_non_zero_usize` redeclared with a different signature
fn pt_non_zero_usize_opt() -> usize;
//~^ WARN `pt_non_zero_usize_opt` redeclared with a different signature
fn pt_non_null_ptr() -> *const ();
//~^ WARN `pt_non_null_ptr` redeclared with a different signature
fn pt_non_zero_usize_wrapper() -> usize;
//~^ WARN `pt_non_zero_usize_wrapper` redeclared with a different signature
fn pt_non_zero_usize_wrapper_opt() -> usize;
//~^ WARN `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
}
}
}

View file

@ -17,6 +17,60 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:500:39
|
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using the base type instead
= note: pattern types have no C equivalent
warning: `extern` block uses type `Option<(usize) is 1..=>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:502:43
|
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
warning: `extern` block uses type `Option<(usize) is 0..=>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:504:54
|
LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:506:37
|
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using the base type instead
= note: pattern types have no C equivalent
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:508:47
|
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
| ^^^^^^^^^^^^ not FFI-safe
|
= help: consider using the base type instead
= note: pattern types have no C equivalent
warning: `extern` block uses type `Option<NonZeroUsize>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:510:51
|
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
warning: `clash` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:13:13
|
@ -258,5 +312,65 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
= note: expected `unsafe extern "C" fn() -> usize`
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
warning: 22 warnings emitted
warning: `pt_non_zero_usize` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:519:13
|
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
| ------------------------------------------------------ `pt_non_zero_usize` previously declared here
...
LL | fn pt_non_zero_usize() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> (usize) is 1..=`
found `unsafe extern "C" fn() -> usize`
warning: `pt_non_zero_usize_opt` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:521:13
|
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
| ------------------------------------------------------------------ `pt_non_zero_usize_opt` previously declared here
...
LL | fn pt_non_zero_usize_opt() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> Option<(usize) is 1..=>`
found `unsafe extern "C" fn() -> usize`
warning: `pt_non_null_ptr` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:523:13
|
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
| ---------------------------------------------------- `pt_non_null_ptr` previously declared here
...
LL | fn pt_non_null_ptr() -> *const ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> (usize) is 1..=`
found `unsafe extern "C" fn() -> *const ()`
warning: `pt_non_zero_usize_wrapper` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:525:13
|
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
| ----------------------------------------------- `pt_non_zero_usize_wrapper` previously declared here
...
LL | fn pt_non_zero_usize_wrapper() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> NonZeroUsize`
found `unsafe extern "C" fn() -> usize`
warning: `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:527:13
|
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
| ----------------------------------------------------------- `pt_non_zero_usize_wrapper_opt` previously declared here
...
LL | fn pt_non_zero_usize_wrapper_opt() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> Option<NonZeroUsize>`
found `unsafe extern "C" fn() -> usize`
warning: 33 warnings emitted

View file

@ -94,6 +94,7 @@ extern "C" {
fn option_transparent_union(x: Option<TransparentUnion<num::NonZero<u8>>>);
//~^ ERROR `extern` block uses type
fn option_repr_rust(x: Option<Rust<num::NonZero<u8>>>); //~ ERROR `extern` block uses type
fn option_u8(x: Option<u8>); //~ ERROR `extern` block uses type
fn result_ref_t(x: Result<&'static u8, ()>);
fn result_fn_t(x: Result<extern "C" fn(), ()>);

View file

@ -79,8 +79,17 @@ LL | fn option_repr_rust(x: Option<Rust<num::NonZero<u8>>>);
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
error: `extern` block uses type `Option<u8>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:97:21
|
LL | fn option_u8(x: Option<u8>);
| ^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
error: `extern` block uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:106:33
--> $DIR/lint-ctypes-enum.rs:107:33
|
LL | fn result_nonzero_u128_t(x: Result<num::NonZero<u128>, ()>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -88,7 +97,7 @@ LL | fn result_nonzero_u128_t(x: Result<num::NonZero<u128>, ()>);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:113:33
--> $DIR/lint-ctypes-enum.rs:114:33
|
LL | fn result_nonzero_i128_t(x: Result<num::NonZero<i128>, ()>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -96,7 +105,7 @@ LL | fn result_nonzero_i128_t(x: Result<num::NonZero<i128>, ()>);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `Result<TransparentUnion<NonZero<u8>>, ()>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:118:38
--> $DIR/lint-ctypes-enum.rs:119:38
|
LL | fn result_transparent_union_t(x: Result<TransparentUnion<num::NonZero<u8>>, ()>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -105,7 +114,7 @@ LL | fn result_transparent_union_t(x: Result<TransparentUnion<num::NonZero<u
= note: enum has no representation hint
error: `extern` block uses type `Result<Rust<NonZero<u8>>, ()>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:120:30
--> $DIR/lint-ctypes-enum.rs:121:30
|
LL | fn result_repr_rust_t(x: Result<Rust<num::NonZero<u8>>, ()>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -114,7 +123,7 @@ LL | fn result_repr_rust_t(x: Result<Rust<num::NonZero<u8>>, ()>);
= note: enum has no representation hint
error: `extern` block uses type `Result<NonZero<u8>, U>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:124:51
--> $DIR/lint-ctypes-enum.rs:125:51
|
LL | fn result_1zst_exhaustive_single_variant_t(x: Result<num::NonZero<u8>, U>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -123,7 +132,7 @@ LL | fn result_1zst_exhaustive_single_variant_t(x: Result<num::NonZero<u8>,
= note: enum has no representation hint
error: `extern` block uses type `Result<NonZero<u8>, B>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:126:53
--> $DIR/lint-ctypes-enum.rs:127:53
|
LL | fn result_1zst_exhaustive_multiple_variant_t(x: Result<num::NonZero<u8>, B>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -132,7 +141,7 @@ LL | fn result_1zst_exhaustive_multiple_variant_t(x: Result<num::NonZero<u8>
= note: enum has no representation hint
error: `extern` block uses type `Result<NonZero<u8>, NonExhaustive>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:128:51
--> $DIR/lint-ctypes-enum.rs:129:51
|
LL | fn result_1zst_non_exhaustive_no_variant_t(x: Result<num::NonZero<u8>, NonExhaustive>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -141,7 +150,7 @@ LL | fn result_1zst_non_exhaustive_no_variant_t(x: Result<num::NonZero<u8>,
= note: enum has no representation hint
error: `extern` block uses type `Result<NonZero<u8>, Field>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:131:49
--> $DIR/lint-ctypes-enum.rs:132:49
|
LL | fn result_1zst_exhaustive_single_field_t(x: Result<num::NonZero<u8>, Field>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -150,7 +159,7 @@ LL | fn result_1zst_exhaustive_single_field_t(x: Result<num::NonZero<u8>, Fi
= note: enum has no representation hint
error: `extern` block uses type `Result<Result<(), NonZero<u8>>, ()>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:133:30
--> $DIR/lint-ctypes-enum.rs:134:30
|
LL | fn result_cascading_t(x: Result<Result<(), num::NonZero<u8>>, ()>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -159,7 +168,7 @@ LL | fn result_cascading_t(x: Result<Result<(), num::NonZero<u8>>, ()>);
= note: enum has no representation hint
error: `extern` block uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:144:33
--> $DIR/lint-ctypes-enum.rs:145:33
|
LL | fn result_nonzero_u128_e(x: Result<(), num::NonZero<u128>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -167,7 +176,7 @@ LL | fn result_nonzero_u128_e(x: Result<(), num::NonZero<u128>>);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:151:33
--> $DIR/lint-ctypes-enum.rs:152:33
|
LL | fn result_nonzero_i128_e(x: Result<(), num::NonZero<i128>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -175,7 +184,7 @@ LL | fn result_nonzero_i128_e(x: Result<(), num::NonZero<i128>>);
= note: 128-bit integers don't currently have a known stable ABI
error: `extern` block uses type `Result<(), TransparentUnion<NonZero<u8>>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:156:38
--> $DIR/lint-ctypes-enum.rs:157:38
|
LL | fn result_transparent_union_e(x: Result<(), TransparentUnion<num::NonZero<u8>>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -184,7 +193,7 @@ LL | fn result_transparent_union_e(x: Result<(), TransparentUnion<num::NonZe
= note: enum has no representation hint
error: `extern` block uses type `Result<(), Rust<NonZero<u8>>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:158:30
--> $DIR/lint-ctypes-enum.rs:159:30
|
LL | fn result_repr_rust_e(x: Result<(), Rust<num::NonZero<u8>>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -193,7 +202,7 @@ LL | fn result_repr_rust_e(x: Result<(), Rust<num::NonZero<u8>>>);
= note: enum has no representation hint
error: `extern` block uses type `Result<U, NonZero<u8>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:162:51
--> $DIR/lint-ctypes-enum.rs:163:51
|
LL | fn result_1zst_exhaustive_single_variant_e(x: Result<U, num::NonZero<u8>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -202,7 +211,7 @@ LL | fn result_1zst_exhaustive_single_variant_e(x: Result<U, num::NonZero<u8
= note: enum has no representation hint
error: `extern` block uses type `Result<B, NonZero<u8>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:164:53
--> $DIR/lint-ctypes-enum.rs:165:53
|
LL | fn result_1zst_exhaustive_multiple_variant_e(x: Result<B, num::NonZero<u8>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -211,7 +220,7 @@ LL | fn result_1zst_exhaustive_multiple_variant_e(x: Result<B, num::NonZero<
= note: enum has no representation hint
error: `extern` block uses type `Result<NonExhaustive, NonZero<u8>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:166:51
--> $DIR/lint-ctypes-enum.rs:167:51
|
LL | fn result_1zst_non_exhaustive_no_variant_e(x: Result<NonExhaustive, num::NonZero<u8>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -220,7 +229,7 @@ LL | fn result_1zst_non_exhaustive_no_variant_e(x: Result<NonExhaustive, num
= note: enum has no representation hint
error: `extern` block uses type `Result<Field, NonZero<u8>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:169:49
--> $DIR/lint-ctypes-enum.rs:170:49
|
LL | fn result_1zst_exhaustive_single_field_e(x: Result<Field, num::NonZero<u8>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -229,7 +238,7 @@ LL | fn result_1zst_exhaustive_single_field_e(x: Result<Field, num::NonZero<
= note: enum has no representation hint
error: `extern` block uses type `Result<(), Result<(), NonZero<u8>>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:171:30
--> $DIR/lint-ctypes-enum.rs:172:30
|
LL | fn result_cascading_e(x: Result<(), Result<(), num::NonZero<u8>>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -238,7 +247,7 @@ LL | fn result_cascading_e(x: Result<(), Result<(), num::NonZero<u8>>>);
= note: enum has no representation hint
error: `extern` block uses type `Result<(), ()>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:173:27
--> $DIR/lint-ctypes-enum.rs:174:27
|
LL | fn result_unit_t_e(x: Result<(), ()>);
| ^^^^^^^^^^^^^^ not FFI-safe
@ -246,5 +255,5 @@ LL | fn result_unit_t_e(x: Result<(), ()>);
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
error: aborting due to 26 previous errors
error: aborting due to 27 previous errors