Skip to content

Commit 768363c

Browse files
committed
Correctly handle pattern types in FFI safety
1 parent 9488e7d commit 768363c

File tree

4 files changed

+11
-46
lines changed

4 files changed

+11
-46
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,6 @@ lint_improper_ctypes_only_phantomdata = composed only of `PhantomData`
385385
386386
lint_improper_ctypes_opaque = opaque types have no C equivalent
387387
388-
lint_improper_ctypes_pat_help = consider using the base type instead
389-
390-
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
391388
lint_improper_ctypes_slice_help = consider using a raw pointer instead
392389
393390
lint_improper_ctypes_slice_reason = slices have no C equivalent

compiler/rustc_lint/src/types.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,11 +1240,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12401240
help: Some(fluent::lint_improper_ctypes_char_help),
12411241
},
12421242

1243-
ty::Pat(..) => FfiUnsafe {
1244-
ty,
1245-
reason: fluent::lint_improper_ctypes_pat_reason,
1246-
help: Some(fluent::lint_improper_ctypes_pat_help),
1247-
},
1243+
// It's just extra invariants on the type that you need to uphold,
1244+
// but only the base type is relevant for being representable in FFI.
1245+
ty::Pat(base, ..) => self.check_type_for_ffi(acc, base),
12481246

12491247
ty::Int(ty::IntTy::I128) | ty::Uint(ty::UintTy::U128) => {
12501248
FfiUnsafe { ty, reason: fluent::lint_improper_ctypes_128bit, help: None }

tests/ui/lint/clashing-extern-fn.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,10 @@ mod pattern_types {
498498
struct NonZeroUsize(pattern_type!(usize is 1..));
499499
extern "C" {
500500
fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
501-
//~^ WARN not FFI-safe
502501
fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
503502
//~^ WARN not FFI-safe
504503
fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
505-
//~^ WARN not FFI-safe
506504
fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
507-
//~^ WARN not FFI-safe
508505
fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
509506
//~^ WARN not FFI-safe
510507
}

tests/ui/lint/clashing-extern-fn.stderr

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,17 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
1717
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
1818
= note: enum has no representation hint
1919

20-
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
21-
--> $DIR/clashing-extern-fn.rs:500:39
22-
|
23-
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
25-
|
26-
= help: consider using the base type instead
27-
= note: pattern types have no C equivalent
28-
2920
warning: `extern` block uses type `Option<(usize) is 1..=>`, which is not FFI-safe
30-
--> $DIR/clashing-extern-fn.rs:502:43
21+
--> $DIR/clashing-extern-fn.rs:501:43
3122
|
3223
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
3324
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
3425
|
3526
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
3627
= note: enum has no representation hint
3728

38-
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
39-
--> $DIR/clashing-extern-fn.rs:504:37
40-
|
41-
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
43-
|
44-
= help: consider using the base type instead
45-
= note: pattern types have no C equivalent
46-
47-
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
48-
--> $DIR/clashing-extern-fn.rs:506:47
49-
|
50-
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
51-
| ^^^^^^^^^^^^ not FFI-safe
52-
|
53-
= help: consider using the base type instead
54-
= note: pattern types have no C equivalent
55-
5629
warning: `extern` block uses type `Option<NonZeroUsize>`, which is not FFI-safe
57-
--> $DIR/clashing-extern-fn.rs:508:51
30+
--> $DIR/clashing-extern-fn.rs:505:51
5831
|
5932
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
6033
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -304,7 +277,7 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
304277
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
305278

306279
warning: `pt_non_zero_usize` redeclared with a different signature
307-
--> $DIR/clashing-extern-fn.rs:517:13
280+
--> $DIR/clashing-extern-fn.rs:514:13
308281
|
309282
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
310283
| ------------------------------------------------------ `pt_non_zero_usize` previously declared here
@@ -316,7 +289,7 @@ LL | fn pt_non_zero_usize() -> usize;
316289
found `unsafe extern "C" fn() -> usize`
317290

318291
warning: `pt_non_zero_usize_opt` redeclared with a different signature
319-
--> $DIR/clashing-extern-fn.rs:519:13
292+
--> $DIR/clashing-extern-fn.rs:516:13
320293
|
321294
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
322295
| ------------------------------------------------------------------ `pt_non_zero_usize_opt` previously declared here
@@ -328,7 +301,7 @@ LL | fn pt_non_zero_usize_opt() -> usize;
328301
found `unsafe extern "C" fn() -> usize`
329302

330303
warning: `pt_non_null_ptr` redeclared with a different signature
331-
--> $DIR/clashing-extern-fn.rs:521:13
304+
--> $DIR/clashing-extern-fn.rs:518:13
332305
|
333306
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
334307
| ---------------------------------------------------- `pt_non_null_ptr` previously declared here
@@ -340,7 +313,7 @@ LL | fn pt_non_null_ptr() -> *const ();
340313
found `unsafe extern "C" fn() -> *const ()`
341314

342315
warning: `pt_non_zero_usize_wrapper` redeclared with a different signature
343-
--> $DIR/clashing-extern-fn.rs:523:13
316+
--> $DIR/clashing-extern-fn.rs:520:13
344317
|
345318
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
346319
| ----------------------------------------------- `pt_non_zero_usize_wrapper` previously declared here
@@ -352,7 +325,7 @@ LL | fn pt_non_zero_usize_wrapper() -> usize;
352325
found `unsafe extern "C" fn() -> usize`
353326

354327
warning: `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
355-
--> $DIR/clashing-extern-fn.rs:525:13
328+
--> $DIR/clashing-extern-fn.rs:522:13
356329
|
357330
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
358331
| ----------------------------------------------------------- `pt_non_zero_usize_wrapper_opt` previously declared here
@@ -363,5 +336,5 @@ LL | fn pt_non_zero_usize_wrapper_opt() -> usize;
363336
= note: expected `unsafe extern "C" fn() -> Option<NonZeroUsize>`
364337
found `unsafe extern "C" fn() -> usize`
365338

366-
warning: 32 warnings emitted
339+
warning: 29 warnings emitted
367340

0 commit comments

Comments
 (0)