about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <github333195615777966@oli-obk.de>2025-01-24 15:57:13 +0000
committerOli Scherer <github333195615777966@oli-obk.de>2025-02-11 08:30:35 +0000
commit473352da311f79bfaadb90c02c6458d02b1fb119 (patch)
treeee7b1b79fe29629313719f1511da5e39d3ebf2d4
parent644c6948d01c66b744d8d7b8d06ea131a75e8311 (diff)
downloadrust-473352da311f79bfaadb90c02c6458d02b1fb119.tar.gz
rust-473352da311f79bfaadb90c02c6458d02b1fb119.zip
Correctly handle pattern types in FFI safety
-rw-r--r--compiler/rustc_lint/messages.ftl3
-rw-r--r--compiler/rustc_lint/src/types.rs8
-rw-r--r--tests/ui/lint/clashing-extern-fn.rs3
-rw-r--r--tests/ui/lint/clashing-extern-fn.stderr45
4 files changed, 12 insertions, 47 deletions
diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl
index 55c6a122d35..480d97e377a 100644
--- a/compiler/rustc_lint/messages.ftl
+++ b/compiler/rustc_lint/messages.ftl
@@ -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
diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs
index 601d2fbfb67..0dccb8d336d 100644
--- a/compiler/rustc_lint/src/types.rs
+++ b/compiler/rustc_lint/src/types.rs
@@ -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 }
diff --git a/tests/ui/lint/clashing-extern-fn.rs b/tests/ui/lint/clashing-extern-fn.rs
index 0464299348b..012eda761f4 100644
--- a/tests/ui/lint/clashing-extern-fn.rs
+++ b/tests/ui/lint/clashing-extern-fn.rs
@@ -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
         }
diff --git a/tests/ui/lint/clashing-extern-fn.stderr b/tests/ui/lint/clashing-extern-fn.stderr
index acf31a1f5dd..16d251c1d7a 100644
--- a/tests/ui/lint/clashing-extern-fn.stderr
+++ b/tests/ui/lint/clashing-extern-fn.stderr
@@ -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