Skip to content

Commit 9488e7d

Browse files
committed
Add ffi tests for pattern types
1 parent 633a3fe commit 9488e7d

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

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

Lines changed: 38 additions & 1 deletion
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,40 @@ 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_null_ptr() -> pattern_type!(usize is 1..);
505+
//~^ WARN not FFI-safe
506+
fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
507+
//~^ WARN not FFI-safe
508+
fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
509+
//~^ WARN not FFI-safe
510+
}
511+
}
512+
mod b {
513+
extern "C" {
514+
// If there's a clash in either of these cases you're either gaining an incorrect
515+
// invariant that the value is non-zero, or you're missing out on that invariant. Both
516+
// cases are warning for, from both a caller-convenience and optimisation perspective.
517+
fn pt_non_zero_usize() -> usize;
518+
//~^ WARN `pt_non_zero_usize` redeclared with a different signature
519+
fn pt_non_zero_usize_opt() -> usize;
520+
//~^ WARN `pt_non_zero_usize_opt` redeclared with a different signature
521+
fn pt_non_null_ptr() -> *const ();
522+
//~^ WARN `pt_non_null_ptr` redeclared with a different signature
523+
fn pt_non_zero_usize_wrapper() -> usize;
524+
//~^ WARN `pt_non_zero_usize_wrapper` redeclared with a different signature
525+
fn pt_non_zero_usize_wrapper_opt() -> usize;
526+
//~^ WARN `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
527+
}
528+
}
529+
}

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

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,51 @@ 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 `(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+
56+
warning: `extern` block uses type `Option<NonZeroUsize>`, which is not FFI-safe
57+
--> $DIR/clashing-extern-fn.rs:508:51
58+
|
59+
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
60+
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
61+
|
62+
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
63+
= note: enum has no representation hint
64+
2065
warning: `clash` redeclared with a different signature
2166
--> $DIR/clashing-extern-fn.rs:13:13
2267
|
@@ -258,5 +303,65 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
258303
= note: expected `unsafe extern "C" fn() -> usize`
259304
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
260305

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

0 commit comments

Comments
 (0)