1
Fork 0

Correctly handle pattern types in FFI safety

This commit is contained in:
Oli Scherer 2025-01-24 15:57:13 +00:00
parent 644c6948d0
commit 473352da31
4 changed files with 12 additions and 47 deletions

View file

@ -390,9 +390,6 @@ lint_improper_ctypes_only_phantomdata = composed only of `PhantomData`
lint_improper_ctypes_opaque = opaque types have no C equivalent
lint_improper_ctypes_pat_help = consider using the base type instead
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
lint_improper_ctypes_slice_help = consider using a raw pointer instead
lint_improper_ctypes_slice_reason = slices have no C equivalent

View file

@ -1256,11 +1256,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
help: Some(fluent::lint_improper_ctypes_char_help),
},
ty::Pat(..) => FfiUnsafe {
ty,
reason: fluent::lint_improper_ctypes_pat_reason,
help: Some(fluent::lint_improper_ctypes_pat_help),
},
// It's just extra invariants on the type that you need to uphold,
// but only the base type is relevant for being representable in FFI.
ty::Pat(base, ..) => self.check_type_for_ffi(acc, base),
ty::Int(ty::IntTy::I128) | ty::Uint(ty::UintTy::U128) => {
FfiUnsafe { ty, reason: fluent::lint_improper_ctypes_128bit, help: None }

View file

@ -498,15 +498,12 @@ mod pattern_types {
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
}

View file

@ -17,17 +17,8 @@ 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
--> $DIR/clashing-extern-fn.rs:501:43
|
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -36,7 +27,7 @@ LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..
= 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
--> $DIR/clashing-extern-fn.rs:503:54
|
LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -44,26 +35,8 @@ LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(u
= 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
--> $DIR/clashing-extern-fn.rs:507:51
|
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@ -313,7 +286,7 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
warning: `pt_non_zero_usize` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:519:13
--> $DIR/clashing-extern-fn.rs:516:13
|
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
| ------------------------------------------------------ `pt_non_zero_usize` previously declared here
@ -325,7 +298,7 @@ LL | fn pt_non_zero_usize() -> usize;
found `unsafe extern "C" fn() -> usize`
warning: `pt_non_zero_usize_opt` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:521:13
--> $DIR/clashing-extern-fn.rs:518:13
|
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
| ------------------------------------------------------------------ `pt_non_zero_usize_opt` previously declared here
@ -337,7 +310,7 @@ LL | fn pt_non_zero_usize_opt() -> usize;
found `unsafe extern "C" fn() -> usize`
warning: `pt_non_null_ptr` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:523:13
--> $DIR/clashing-extern-fn.rs:520:13
|
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
| ---------------------------------------------------- `pt_non_null_ptr` previously declared here
@ -349,7 +322,7 @@ LL | fn pt_non_null_ptr() -> *const ();
found `unsafe extern "C" fn() -> *const ()`
warning: `pt_non_zero_usize_wrapper` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:525:13
--> $DIR/clashing-extern-fn.rs:522:13
|
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
| ----------------------------------------------- `pt_non_zero_usize_wrapper` previously declared here
@ -361,7 +334,7 @@ LL | fn pt_non_zero_usize_wrapper() -> usize;
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
--> $DIR/clashing-extern-fn.rs:524:13
|
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
| ----------------------------------------------------------- `pt_non_zero_usize_wrapper_opt` previously declared here
@ -372,5 +345,5 @@ LL | fn pt_non_zero_usize_wrapper_opt() -> usize;
= note: expected `unsafe extern "C" fn() -> Option<NonZeroUsize>`
found `unsafe extern "C" fn() -> usize`
warning: 33 warnings emitted
warning: 30 warnings emitted