Skip to content

Commit 644c694

Browse files
committed
Add ffi tests for pattern types
1 parent c182ce9 commit 644c694

File tree

4 files changed

+185
-22
lines changed

4 files changed

+185
-22
lines changed

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

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ check-pass
22
//@ aux-build:external_extern_fn.rs
33
#![crate_type = "lib"]
4-
4+
#![feature(pattern_type_macro, pattern_types)]
55
mod redeclared_different_signature {
66
mod a {
77
extern "C" {
@@ -490,3 +490,42 @@ mod hidden_niche {
490490
}
491491
}
492492
}
493+
494+
mod pattern_types {
495+
mod a {
496+
use std::pat::pattern_type;
497+
#[repr(transparent)]
498+
struct NonZeroUsize(pattern_type!(usize is 1..));
499+
extern "C" {
500+
fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
501+
//~^ WARN not FFI-safe
502+
fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
503+
//~^ WARN not FFI-safe
504+
fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
505+
//~^ WARN not FFI-safe
506+
fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
507+
//~^ WARN not FFI-safe
508+
fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
509+
//~^ WARN not FFI-safe
510+
fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
511+
//~^ WARN not FFI-safe
512+
}
513+
}
514+
mod b {
515+
extern "C" {
516+
// If there's a clash in either of these cases you're either gaining an incorrect
517+
// invariant that the value is non-zero, or you're missing out on that invariant. Both
518+
// cases are warning for, from both a caller-convenience and optimisation perspective.
519+
fn pt_non_zero_usize() -> usize;
520+
//~^ WARN `pt_non_zero_usize` redeclared with a different signature
521+
fn pt_non_zero_usize_opt() -> usize;
522+
//~^ WARN `pt_non_zero_usize_opt` redeclared with a different signature
523+
fn pt_non_null_ptr() -> *const ();
524+
//~^ WARN `pt_non_null_ptr` redeclared with a different signature
525+
fn pt_non_zero_usize_wrapper() -> usize;
526+
//~^ WARN `pt_non_zero_usize_wrapper` redeclared with a different signature
527+
fn pt_non_zero_usize_wrapper_opt() -> usize;
528+
//~^ WARN `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
529+
}
530+
}
531+
}

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

+115-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,60 @@ 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+
29+
warning: `extern` block uses type `Option<(usize) is 1..=>`, which is not FFI-safe
30+
--> $DIR/clashing-extern-fn.rs:502:43
31+
|
32+
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
34+
|
35+
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
36+
= note: enum has no representation hint
37+
38+
warning: `extern` block uses type `Option<(usize) is 0..=>`, which is not FFI-safe
39+
--> $DIR/clashing-extern-fn.rs:504:54
40+
|
41+
LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
43+
|
44+
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
45+
= note: enum has no representation hint
46+
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+
65+
warning: `extern` block uses type `Option<NonZeroUsize>`, which is not FFI-safe
66+
--> $DIR/clashing-extern-fn.rs:510:51
67+
|
68+
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
69+
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
70+
|
71+
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
72+
= note: enum has no representation hint
73+
2074
warning: `clash` redeclared with a different signature
2175
--> $DIR/clashing-extern-fn.rs:13:13
2276
|
@@ -258,5 +312,65 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
258312
= note: expected `unsafe extern "C" fn() -> usize`
259313
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
260314

261-
warning: 22 warnings emitted
315+
warning: `pt_non_zero_usize` redeclared with a different signature
316+
--> $DIR/clashing-extern-fn.rs:519:13
317+
|
318+
LL | fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
319+
| ------------------------------------------------------ `pt_non_zero_usize` previously declared here
320+
...
321+
LL | fn pt_non_zero_usize() -> usize;
322+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
323+
|
324+
= note: expected `unsafe extern "C" fn() -> (usize) is 1..=`
325+
found `unsafe extern "C" fn() -> usize`
326+
327+
warning: `pt_non_zero_usize_opt` redeclared with a different signature
328+
--> $DIR/clashing-extern-fn.rs:521:13
329+
|
330+
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
331+
| ------------------------------------------------------------------ `pt_non_zero_usize_opt` previously declared here
332+
...
333+
LL | fn pt_non_zero_usize_opt() -> usize;
334+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
335+
|
336+
= note: expected `unsafe extern "C" fn() -> Option<(usize) is 1..=>`
337+
found `unsafe extern "C" fn() -> usize`
338+
339+
warning: `pt_non_null_ptr` redeclared with a different signature
340+
--> $DIR/clashing-extern-fn.rs:523:13
341+
|
342+
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
343+
| ---------------------------------------------------- `pt_non_null_ptr` previously declared here
344+
...
345+
LL | fn pt_non_null_ptr() -> *const ();
346+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
347+
|
348+
= note: expected `unsafe extern "C" fn() -> (usize) is 1..=`
349+
found `unsafe extern "C" fn() -> *const ()`
350+
351+
warning: `pt_non_zero_usize_wrapper` redeclared with a different signature
352+
--> $DIR/clashing-extern-fn.rs:525:13
353+
|
354+
LL | fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
355+
| ----------------------------------------------- `pt_non_zero_usize_wrapper` previously declared here
356+
...
357+
LL | fn pt_non_zero_usize_wrapper() -> usize;
358+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
359+
|
360+
= note: expected `unsafe extern "C" fn() -> NonZeroUsize`
361+
found `unsafe extern "C" fn() -> usize`
362+
363+
warning: `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
364+
--> $DIR/clashing-extern-fn.rs:527:13
365+
|
366+
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
367+
| ----------------------------------------------------------- `pt_non_zero_usize_wrapper_opt` previously declared here
368+
...
369+
LL | fn pt_non_zero_usize_wrapper_opt() -> usize;
370+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
371+
|
372+
= note: expected `unsafe extern "C" fn() -> Option<NonZeroUsize>`
373+
found `unsafe extern "C" fn() -> usize`
374+
375+
warning: 33 warnings emitted
262376

tests/ui/lint/lint-ctypes-enum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ extern "C" {
9494
fn option_transparent_union(x: Option<TransparentUnion<num::NonZero<u8>>>);
9595
//~^ ERROR `extern` block uses type
9696
fn option_repr_rust(x: Option<Rust<num::NonZero<u8>>>); //~ ERROR `extern` block uses type
97+
fn option_u8(x: Option<u8>); //~ ERROR `extern` block uses type
9798

9899
fn result_ref_t(x: Result<&'static u8, ()>);
99100
fn result_fn_t(x: Result<extern "C" fn(), ()>);

tests/ui/lint/lint-ctypes-enum.stderr

+29-20
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,33 @@ LL | fn option_repr_rust(x: Option<Rust<num::NonZero<u8>>>);
7979
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
8080
= note: enum has no representation hint
8181

82+
error: `extern` block uses type `Option<u8>`, which is not FFI-safe
83+
--> $DIR/lint-ctypes-enum.rs:97:21
84+
|
85+
LL | fn option_u8(x: Option<u8>);
86+
| ^^^^^^^^^^ not FFI-safe
87+
|
88+
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
89+
= note: enum has no representation hint
90+
8291
error: `extern` block uses type `u128`, which is not FFI-safe
83-
--> $DIR/lint-ctypes-enum.rs:106:33
92+
--> $DIR/lint-ctypes-enum.rs:107:33
8493
|
8594
LL | fn result_nonzero_u128_t(x: Result<num::NonZero<u128>, ()>);
8695
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
8796
|
8897
= note: 128-bit integers don't currently have a known stable ABI
8998

9099
error: `extern` block uses type `i128`, which is not FFI-safe
91-
--> $DIR/lint-ctypes-enum.rs:113:33
100+
--> $DIR/lint-ctypes-enum.rs:114:33
92101
|
93102
LL | fn result_nonzero_i128_t(x: Result<num::NonZero<i128>, ()>);
94103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
95104
|
96105
= note: 128-bit integers don't currently have a known stable ABI
97106

98107
error: `extern` block uses type `Result<TransparentUnion<NonZero<u8>>, ()>`, which is not FFI-safe
99-
--> $DIR/lint-ctypes-enum.rs:118:38
108+
--> $DIR/lint-ctypes-enum.rs:119:38
100109
|
101110
LL | fn result_transparent_union_t(x: Result<TransparentUnion<num::NonZero<u8>>, ()>);
102111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -105,7 +114,7 @@ LL | fn result_transparent_union_t(x: Result<TransparentUnion<num::NonZero<u
105114
= note: enum has no representation hint
106115

107116
error: `extern` block uses type `Result<Rust<NonZero<u8>>, ()>`, which is not FFI-safe
108-
--> $DIR/lint-ctypes-enum.rs:120:30
117+
--> $DIR/lint-ctypes-enum.rs:121:30
109118
|
110119
LL | fn result_repr_rust_t(x: Result<Rust<num::NonZero<u8>>, ()>);
111120
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -114,7 +123,7 @@ LL | fn result_repr_rust_t(x: Result<Rust<num::NonZero<u8>>, ()>);
114123
= note: enum has no representation hint
115124

116125
error: `extern` block uses type `Result<NonZero<u8>, U>`, which is not FFI-safe
117-
--> $DIR/lint-ctypes-enum.rs:124:51
126+
--> $DIR/lint-ctypes-enum.rs:125:51
118127
|
119128
LL | fn result_1zst_exhaustive_single_variant_t(x: Result<num::NonZero<u8>, U>);
120129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -123,7 +132,7 @@ LL | fn result_1zst_exhaustive_single_variant_t(x: Result<num::NonZero<u8>,
123132
= note: enum has no representation hint
124133

125134
error: `extern` block uses type `Result<NonZero<u8>, B>`, which is not FFI-safe
126-
--> $DIR/lint-ctypes-enum.rs:126:53
135+
--> $DIR/lint-ctypes-enum.rs:127:53
127136
|
128137
LL | fn result_1zst_exhaustive_multiple_variant_t(x: Result<num::NonZero<u8>, B>);
129138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -132,7 +141,7 @@ LL | fn result_1zst_exhaustive_multiple_variant_t(x: Result<num::NonZero<u8>
132141
= note: enum has no representation hint
133142

134143
error: `extern` block uses type `Result<NonZero<u8>, NonExhaustive>`, which is not FFI-safe
135-
--> $DIR/lint-ctypes-enum.rs:128:51
144+
--> $DIR/lint-ctypes-enum.rs:129:51
136145
|
137146
LL | fn result_1zst_non_exhaustive_no_variant_t(x: Result<num::NonZero<u8>, NonExhaustive>);
138147
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -141,7 +150,7 @@ LL | fn result_1zst_non_exhaustive_no_variant_t(x: Result<num::NonZero<u8>,
141150
= note: enum has no representation hint
142151

143152
error: `extern` block uses type `Result<NonZero<u8>, Field>`, which is not FFI-safe
144-
--> $DIR/lint-ctypes-enum.rs:131:49
153+
--> $DIR/lint-ctypes-enum.rs:132:49
145154
|
146155
LL | fn result_1zst_exhaustive_single_field_t(x: Result<num::NonZero<u8>, Field>);
147156
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -150,7 +159,7 @@ LL | fn result_1zst_exhaustive_single_field_t(x: Result<num::NonZero<u8>, Fi
150159
= note: enum has no representation hint
151160

152161
error: `extern` block uses type `Result<Result<(), NonZero<u8>>, ()>`, which is not FFI-safe
153-
--> $DIR/lint-ctypes-enum.rs:133:30
162+
--> $DIR/lint-ctypes-enum.rs:134:30
154163
|
155164
LL | fn result_cascading_t(x: Result<Result<(), num::NonZero<u8>>, ()>);
156165
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -159,23 +168,23 @@ LL | fn result_cascading_t(x: Result<Result<(), num::NonZero<u8>>, ()>);
159168
= note: enum has no representation hint
160169

161170
error: `extern` block uses type `u128`, which is not FFI-safe
162-
--> $DIR/lint-ctypes-enum.rs:144:33
171+
--> $DIR/lint-ctypes-enum.rs:145:33
163172
|
164173
LL | fn result_nonzero_u128_e(x: Result<(), num::NonZero<u128>>);
165174
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
166175
|
167176
= note: 128-bit integers don't currently have a known stable ABI
168177

169178
error: `extern` block uses type `i128`, which is not FFI-safe
170-
--> $DIR/lint-ctypes-enum.rs:151:33
179+
--> $DIR/lint-ctypes-enum.rs:152:33
171180
|
172181
LL | fn result_nonzero_i128_e(x: Result<(), num::NonZero<i128>>);
173182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
174183
|
175184
= note: 128-bit integers don't currently have a known stable ABI
176185

177186
error: `extern` block uses type `Result<(), TransparentUnion<NonZero<u8>>>`, which is not FFI-safe
178-
--> $DIR/lint-ctypes-enum.rs:156:38
187+
--> $DIR/lint-ctypes-enum.rs:157:38
179188
|
180189
LL | fn result_transparent_union_e(x: Result<(), TransparentUnion<num::NonZero<u8>>>);
181190
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -184,7 +193,7 @@ LL | fn result_transparent_union_e(x: Result<(), TransparentUnion<num::NonZe
184193
= note: enum has no representation hint
185194

186195
error: `extern` block uses type `Result<(), Rust<NonZero<u8>>>`, which is not FFI-safe
187-
--> $DIR/lint-ctypes-enum.rs:158:30
196+
--> $DIR/lint-ctypes-enum.rs:159:30
188197
|
189198
LL | fn result_repr_rust_e(x: Result<(), Rust<num::NonZero<u8>>>);
190199
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -193,7 +202,7 @@ LL | fn result_repr_rust_e(x: Result<(), Rust<num::NonZero<u8>>>);
193202
= note: enum has no representation hint
194203

195204
error: `extern` block uses type `Result<U, NonZero<u8>>`, which is not FFI-safe
196-
--> $DIR/lint-ctypes-enum.rs:162:51
205+
--> $DIR/lint-ctypes-enum.rs:163:51
197206
|
198207
LL | fn result_1zst_exhaustive_single_variant_e(x: Result<U, num::NonZero<u8>>);
199208
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -202,7 +211,7 @@ LL | fn result_1zst_exhaustive_single_variant_e(x: Result<U, num::NonZero<u8
202211
= note: enum has no representation hint
203212

204213
error: `extern` block uses type `Result<B, NonZero<u8>>`, which is not FFI-safe
205-
--> $DIR/lint-ctypes-enum.rs:164:53
214+
--> $DIR/lint-ctypes-enum.rs:165:53
206215
|
207216
LL | fn result_1zst_exhaustive_multiple_variant_e(x: Result<B, num::NonZero<u8>>);
208217
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -211,7 +220,7 @@ LL | fn result_1zst_exhaustive_multiple_variant_e(x: Result<B, num::NonZero<
211220
= note: enum has no representation hint
212221

213222
error: `extern` block uses type `Result<NonExhaustive, NonZero<u8>>`, which is not FFI-safe
214-
--> $DIR/lint-ctypes-enum.rs:166:51
223+
--> $DIR/lint-ctypes-enum.rs:167:51
215224
|
216225
LL | fn result_1zst_non_exhaustive_no_variant_e(x: Result<NonExhaustive, num::NonZero<u8>>);
217226
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -220,7 +229,7 @@ LL | fn result_1zst_non_exhaustive_no_variant_e(x: Result<NonExhaustive, num
220229
= note: enum has no representation hint
221230

222231
error: `extern` block uses type `Result<Field, NonZero<u8>>`, which is not FFI-safe
223-
--> $DIR/lint-ctypes-enum.rs:169:49
232+
--> $DIR/lint-ctypes-enum.rs:170:49
224233
|
225234
LL | fn result_1zst_exhaustive_single_field_e(x: Result<Field, num::NonZero<u8>>);
226235
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -229,7 +238,7 @@ LL | fn result_1zst_exhaustive_single_field_e(x: Result<Field, num::NonZero<
229238
= note: enum has no representation hint
230239

231240
error: `extern` block uses type `Result<(), Result<(), NonZero<u8>>>`, which is not FFI-safe
232-
--> $DIR/lint-ctypes-enum.rs:171:30
241+
--> $DIR/lint-ctypes-enum.rs:172:30
233242
|
234243
LL | fn result_cascading_e(x: Result<(), Result<(), num::NonZero<u8>>>);
235244
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -238,13 +247,13 @@ LL | fn result_cascading_e(x: Result<(), Result<(), num::NonZero<u8>>>);
238247
= note: enum has no representation hint
239248

240249
error: `extern` block uses type `Result<(), ()>`, which is not FFI-safe
241-
--> $DIR/lint-ctypes-enum.rs:173:27
250+
--> $DIR/lint-ctypes-enum.rs:174:27
242251
|
243252
LL | fn result_unit_t_e(x: Result<(), ()>);
244253
| ^^^^^^^^^^^^^^ not FFI-safe
245254
|
246255
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
247256
= note: enum has no representation hint
248257

249-
error: aborting due to 26 previous errors
258+
error: aborting due to 27 previous errors
250259

0 commit comments

Comments
 (0)