Skip to content

Commit 473352d

Browse files
committed
Correctly handle pattern types in FFI safety
1 parent 644c694 commit 473352d

File tree

4 files changed

+12
-47
lines changed

4 files changed

+12
-47
lines changed

compiler/rustc_lint/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,6 @@ lint_improper_ctypes_only_phantomdata = composed only of `PhantomData`
390390
391391
lint_improper_ctypes_opaque = opaque types have no C equivalent
392392
393-
lint_improper_ctypes_pat_help = consider using the base type instead
394-
395-
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
396393
lint_improper_ctypes_slice_help = consider using a raw pointer instead
397394
398395
lint_improper_ctypes_slice_reason = slices have no C equivalent

compiler/rustc_lint/src/types.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1256,11 +1256,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12561256
help: Some(fluent::lint_improper_ctypes_char_help),
12571257
},
12581258

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

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

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

-3
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,12 @@ 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_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
505504
//~^ WARN not FFI-safe
506505
fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
507-
//~^ WARN not FFI-safe
508506
fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
509-
//~^ WARN not FFI-safe
510507
fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
511508
//~^ WARN not FFI-safe
512509
}

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

+9-36
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,8 @@ 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
@@ -36,34 +27,16 @@ LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..
3627
= note: enum has no representation hint
3728

3829
warning: `extern` block uses type `Option<(usize) is 0..=>`, which is not FFI-safe
39-
--> $DIR/clashing-extern-fn.rs:504:54
30+
--> $DIR/clashing-extern-fn.rs:503:54
4031
|
4132
LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
4233
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
4334
|
4435
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
4536
= note: enum has no representation hint
4637

47-
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
48-
--> $DIR/clashing-extern-fn.rs:506:37
49-
|
50-
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
51-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
52-
|
53-
= help: consider using the base type instead
54-
= note: pattern types have no C equivalent
55-
56-
warning: `extern` block uses type `(usize) is 1..=`, which is not FFI-safe
57-
--> $DIR/clashing-extern-fn.rs:508:47
58-
|
59-
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
60-
| ^^^^^^^^^^^^ not FFI-safe
61-
|
62-
= help: consider using the base type instead
63-
= note: pattern types have no C equivalent
64-
6538
warning: `extern` block uses type `Option<NonZeroUsize>`, which is not FFI-safe
66-
--> $DIR/clashing-extern-fn.rs:510:51
39+
--> $DIR/clashing-extern-fn.rs:507:51
6740
|
6841
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
6942
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -313,7 +286,7 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
313286
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
314287

315288
warning: `pt_non_zero_usize` redeclared with a different signature
316-
--> $DIR/clashing-extern-fn.rs:519:13
289+
--> $DIR/clashing-extern-fn.rs:516:13
317290
|
318291
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
319292
| ------------------------------------------------------ `pt_non_zero_usize` previously declared here
@@ -325,7 +298,7 @@ LL | fn pt_non_zero_usize() -> usize;
325298
found `unsafe extern "C" fn() -> usize`
326299

327300
warning: `pt_non_zero_usize_opt` redeclared with a different signature
328-
--> $DIR/clashing-extern-fn.rs:521:13
301+
--> $DIR/clashing-extern-fn.rs:518:13
329302
|
330303
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
331304
| ------------------------------------------------------------------ `pt_non_zero_usize_opt` previously declared here
@@ -337,7 +310,7 @@ LL | fn pt_non_zero_usize_opt() -> usize;
337310
found `unsafe extern "C" fn() -> usize`
338311

339312
warning: `pt_non_null_ptr` redeclared with a different signature
340-
--> $DIR/clashing-extern-fn.rs:523:13
313+
--> $DIR/clashing-extern-fn.rs:520:13
341314
|
342315
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
343316
| ---------------------------------------------------- `pt_non_null_ptr` previously declared here
@@ -349,7 +322,7 @@ LL | fn pt_non_null_ptr() -> *const ();
349322
found `unsafe extern "C" fn() -> *const ()`
350323

351324
warning: `pt_non_zero_usize_wrapper` redeclared with a different signature
352-
--> $DIR/clashing-extern-fn.rs:525:13
325+
--> $DIR/clashing-extern-fn.rs:522:13
353326
|
354327
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
355328
| ----------------------------------------------- `pt_non_zero_usize_wrapper` previously declared here
@@ -361,7 +334,7 @@ LL | fn pt_non_zero_usize_wrapper() -> usize;
361334
found `unsafe extern "C" fn() -> usize`
362335

363336
warning: `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
364-
--> $DIR/clashing-extern-fn.rs:527:13
337+
--> $DIR/clashing-extern-fn.rs:524:13
365338
|
366339
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
367340
| ----------------------------------------------------------- `pt_non_zero_usize_wrapper_opt` previously declared here
@@ -372,5 +345,5 @@ LL | fn pt_non_zero_usize_wrapper_opt() -> usize;
372345
= note: expected `unsafe extern "C" fn() -> Option<NonZeroUsize>`
373346
found `unsafe extern "C" fn() -> usize`
374347

375-
warning: 33 warnings emitted
348+
warning: 30 warnings emitted
376349

0 commit comments

Comments
 (0)