Skip to content

Commit 784bbfa

Browse files
committed
Handle pattern types wrapped in Option in FFI checks
1 parent dd5927d commit 784bbfa

File tree

3 files changed

+27
-48
lines changed

3 files changed

+27
-48
lines changed

Diff for: compiler/rustc_lint/src/types.rs

+25
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,31 @@ fn ty_is_known_nonnull<'tcx>(
861861
.filter_map(|variant| transparent_newtype_field(tcx, variant))
862862
.any(|field| ty_is_known_nonnull(tcx, typing_env, field.ty(tcx, args), mode))
863863
}
864+
ty::Pat(base, pat) => {
865+
ty_is_known_nonnull(tcx, typing_env, *base, mode)
866+
|| match **pat {
867+
ty::PatternKind::Range { start, end, include_end } => match (start, end) {
868+
(Some(start), None) => {
869+
start.try_to_bits(tcx, typing_env).is_some_and(|i| i > 0)
870+
}
871+
(Some(start), Some(end)) => {
872+
if let Some(start) = start.try_to_bits(tcx, typing_env) {
873+
if let Some(end) = end.try_to_bits(tcx, typing_env) {
874+
return if include_end {
875+
// This also works for negative numbers, as we just need
876+
// to ensure we aren't wrapping over zero.
877+
start > 0 && end >= start
878+
} else {
879+
start > 0 && end > start
880+
};
881+
}
882+
}
883+
false
884+
}
885+
_ => false,
886+
},
887+
}
888+
}
864889
_ => false,
865890
}
866891
}

Diff for: tests/ui/lint/clashing-extern-fn.rs

-4
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,9 @@ mod pattern_types {
499499
extern "C" {
500500
fn pt_non_zero_usize() -> pattern_type!(usize is 1..);
501501
fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
502-
//~^ WARN not FFI-safe
503502
fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
504503
fn pt_non_zero_usize_wrapper() -> NonZeroUsize;
505504
fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
506-
//~^ WARN not FFI-safe
507505
}
508506
}
509507
mod b {
@@ -513,12 +511,10 @@ mod pattern_types {
513511
// cases are warning for, from both a caller-convenience and optimisation perspective.
514512
fn pt_non_zero_usize() -> usize;
515513
fn pt_non_zero_usize_opt() -> usize;
516-
//~^ WARN `pt_non_zero_usize_opt` redeclared with a different signature
517514
fn pt_non_null_ptr() -> *const ();
518515
//~^ WARN `pt_non_null_ptr` redeclared with a different signature
519516
fn pt_non_zero_usize_wrapper() -> usize;
520517
fn pt_non_zero_usize_wrapper_opt() -> usize;
521-
//~^ WARN `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
522518
}
523519
}
524520
}

Diff for: tests/ui/lint/clashing-extern-fn.stderr

+2-44
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,6 @@ 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 `Option<(usize) is 1..=>`, which is not FFI-safe
21-
--> $DIR/clashing-extern-fn.rs:501:43
22-
|
23-
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
25-
|
26-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
27-
= note: enum has no representation hint
28-
29-
warning: `extern` block uses type `Option<NonZeroUsize>`, which is not FFI-safe
30-
--> $DIR/clashing-extern-fn.rs:505:51
31-
|
32-
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
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-
3820
warning: `clash` redeclared with a different signature
3921
--> $DIR/clashing-extern-fn.rs:13:13
4022
|
@@ -276,20 +258,8 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
276258
= note: expected `unsafe extern "C" fn() -> usize`
277259
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
278260

279-
warning: `pt_non_zero_usize_opt` redeclared with a different signature
280-
--> $DIR/clashing-extern-fn.rs:515:13
281-
|
282-
LL | fn pt_non_zero_usize_opt() -> Option<pattern_type!(usize is 1..)>;
283-
| ------------------------------------------------------------------ `pt_non_zero_usize_opt` previously declared here
284-
...
285-
LL | fn pt_non_zero_usize_opt() -> usize;
286-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
287-
|
288-
= note: expected `unsafe extern "C" fn() -> Option<(usize) is 1..=>`
289-
found `unsafe extern "C" fn() -> usize`
290-
291261
warning: `pt_non_null_ptr` redeclared with a different signature
292-
--> $DIR/clashing-extern-fn.rs:517:13
262+
--> $DIR/clashing-extern-fn.rs:514:13
293263
|
294264
LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
295265
| ---------------------------------------------------- `pt_non_null_ptr` previously declared here
@@ -300,17 +270,5 @@ LL | fn pt_non_null_ptr() -> *const ();
300270
= note: expected `unsafe extern "C" fn() -> (usize) is 1..=`
301271
found `unsafe extern "C" fn() -> *const ()`
302272

303-
warning: `pt_non_zero_usize_wrapper_opt` redeclared with a different signature
304-
--> $DIR/clashing-extern-fn.rs:520:13
305-
|
306-
LL | fn pt_non_zero_usize_wrapper_opt() -> Option<NonZeroUsize>;
307-
| ----------------------------------------------------------- `pt_non_zero_usize_wrapper_opt` previously declared here
308-
...
309-
LL | fn pt_non_zero_usize_wrapper_opt() -> usize;
310-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
311-
|
312-
= note: expected `unsafe extern "C" fn() -> Option<NonZeroUsize>`
313-
found `unsafe extern "C" fn() -> usize`
314-
315-
warning: 27 warnings emitted
273+
warning: 23 warnings emitted
316274

0 commit comments

Comments
 (0)