Skip to content

Commit e9cf280

Browse files
warn less about non-exhaustive in ffi
Bindgen allows generating `#[non_exhaustive] #[repr(u32)]` enums. This results in nonintuitive nonlocal `improper_ctypes` warnings, even when the types are otherwise perfectly valid in C. Adjust for actual tooling expectations by avoiding warning on simple enums with only unit variants.
1 parent 749f80a commit e9cf280

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

compiler/rustc_lint/src/types.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::ControlFlow;
33

44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_errors::DiagMessage;
6+
use rustc_hir::def::CtorKind;
67
use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
78
use rustc_middle::bug;
89
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
@@ -1386,15 +1387,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13861387
// Empty enums are okay... although sort of useless.
13871388
return FfiSafe;
13881389
}
1389-
1390-
if def.is_variant_list_non_exhaustive() && !def.did().is_local() {
1391-
return FfiUnsafe {
1392-
ty,
1393-
reason: fluent::lint_improper_ctypes_non_exhaustive,
1394-
help: None,
1395-
};
1396-
}
1397-
13981390
// Check for a repr() attribute to specify the size of the
13991391
// discriminant.
14001392
if !def.repr().c() && !def.repr().transparent() && def.repr().int.is_none()
@@ -1413,8 +1405,25 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
14131405
};
14141406
}
14151407

1408+
// non_exhaustive suggests it is possible that someone might break ABI
1409+
// see: https://github.com/rust-lang/rust/issues/44109#issuecomment-537583344
1410+
// so warn on complex enums being used outside their crate
1411+
let nonexhaustive_nonlocal_ffi =
1412+
def.is_variant_list_non_exhaustive() && !def.did().is_local();
1413+
14161414
// Check the contained variants.
14171415
for variant in def.variants() {
1416+
// but only warn about really_tagged_union reprs,
1417+
// exempt enums with unit ctors like C's (like rust-bindgen)
1418+
if nonexhaustive_nonlocal_ffi
1419+
&& !matches!(variant.ctor_kind(), Some(CtorKind::Const))
1420+
{
1421+
return FfiUnsafe {
1422+
ty,
1423+
reason: fluent::lint_improper_ctypes_non_exhaustive,
1424+
help: None,
1425+
};
1426+
};
14181427
let is_non_exhaustive = variant.is_field_list_non_exhaustive();
14191428
if is_non_exhaustive && !variant.def_id.is_local() {
14201429
return FfiUnsafe {

tests/ui/rfcs/rfc-2008-non-exhaustive/improper_ctypes/auxiliary/types.rs

+11
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ pub enum NonExhaustiveVariants {
2727
#[non_exhaustive] Tuple(u32),
2828
#[non_exhaustive] Struct { field: u32 }
2929
}
30+
31+
// Note the absence of repr(C): it's not necessary, and recent C code can now use repr hints too.
32+
#[repr(u32)]
33+
#[non_exhaustive]
34+
pub enum NonExhaustiveCLikeEnum {
35+
One = 1,
36+
Two = 2,
37+
Three = 3,
38+
Four = 4,
39+
Five = 5,
40+
}

tests/ui/rfcs/rfc-2008-non-exhaustive/improper_ctypes/extern_crate_improper.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ extern crate types;
66
// This test checks that non-exhaustive types with `#[repr(C)]` from an extern crate are considered
77
// improper.
88

9-
use types::{NonExhaustiveEnum, NonExhaustiveVariants, NormalStruct, TupleStruct, UnitStruct};
9+
use types::{
10+
NonExhaustiveCLikeEnum, NonExhaustiveEnum, NonExhaustiveVariants,
11+
NormalStruct, TupleStruct, UnitStruct,
12+
};
1013

1114
extern "C" {
1215
pub fn non_exhaustive_enum(_: NonExhaustiveEnum);
@@ -21,4 +24,9 @@ extern "C" {
2124
//~^ ERROR `extern` block uses type `NonExhaustiveVariants`, which is not FFI-safe
2225
}
2326

27+
// These should pass without remark, as they're C-compatible, despite being "non-exhaustive".
28+
extern "C" {
29+
pub fn non_exhaustive_c_compat_enum(_: NonExhaustiveCLikeEnum);
30+
}
31+
2432
fn main() {}

tests/ui/rfcs/rfc-2008-non-exhaustive/improper_ctypes/extern_crate_improper.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `extern` block uses type `NonExhaustiveEnum`, which is not FFI-safe
2-
--> $DIR/extern_crate_improper.rs:12:35
2+
--> $DIR/extern_crate_improper.rs:15:35
33
|
44
LL | pub fn non_exhaustive_enum(_: NonExhaustiveEnum);
55
| ^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -12,31 +12,31 @@ LL | #![deny(improper_ctypes)]
1212
| ^^^^^^^^^^^^^^^
1313

1414
error: `extern` block uses type `NormalStruct`, which is not FFI-safe
15-
--> $DIR/extern_crate_improper.rs:14:44
15+
--> $DIR/extern_crate_improper.rs:17:44
1616
|
1717
LL | pub fn non_exhaustive_normal_struct(_: NormalStruct);
1818
| ^^^^^^^^^^^^ not FFI-safe
1919
|
2020
= note: this struct is non-exhaustive
2121

2222
error: `extern` block uses type `UnitStruct`, which is not FFI-safe
23-
--> $DIR/extern_crate_improper.rs:16:42
23+
--> $DIR/extern_crate_improper.rs:19:42
2424
|
2525
LL | pub fn non_exhaustive_unit_struct(_: UnitStruct);
2626
| ^^^^^^^^^^ not FFI-safe
2727
|
2828
= note: this struct is non-exhaustive
2929

3030
error: `extern` block uses type `TupleStruct`, which is not FFI-safe
31-
--> $DIR/extern_crate_improper.rs:18:43
31+
--> $DIR/extern_crate_improper.rs:21:43
3232
|
3333
LL | pub fn non_exhaustive_tuple_struct(_: TupleStruct);
3434
| ^^^^^^^^^^^ not FFI-safe
3535
|
3636
= note: this struct is non-exhaustive
3737

3838
error: `extern` block uses type `NonExhaustiveVariants`, which is not FFI-safe
39-
--> $DIR/extern_crate_improper.rs:20:38
39+
--> $DIR/extern_crate_improper.rs:23:38
4040
|
4141
LL | pub fn non_exhaustive_variant(_: NonExhaustiveVariants);
4242
| ^^^^^^^^^^^^^^^^^^^^^ not FFI-safe

0 commit comments

Comments
 (0)