Skip to content

Commit 08b85a1

Browse files
committed
Don't lint niche optimized variants in enums
1 parent b3e6d52 commit 08b85a1

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

compiler/rustc_lint/src/types.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,14 +1141,6 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
11411141
[var_one, var_two] => match (&var_one.fields.raw[..], &var_two.fields.raw[..]) {
11421142
([], [field]) | ([field], []) => field.ty(tcx, args),
11431143
([field1], [field2]) => {
1144-
// TODO: We pass all the checks here although individual enum variants has
1145-
// checks for FFI safety even when niche optimized which needs to be
1146-
// suppressed. for types like `Result<PhantomData<()>, E>`, PhantomData has
1147-
// it's own lint for FFI which needs to be suppressed: `composed only of
1148-
// `PhantomData``. This is true for other custom types as well `struct
1149-
// Example;` which emits `this struct has unspecified layout` and suggests to
1150-
// add `#[repr(C)]` and when that is done, linter emits `this struct has no
1151-
// fields`, all under the `improper_ctypes_definitions` lint group
11521144
let ty1 = field1.ty(tcx, args);
11531145
let ty2 = field2.ty(tcx, args);
11541146

@@ -1245,7 +1237,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12451237
args: GenericArgsRef<'tcx>,
12461238
) -> FfiResult<'tcx> {
12471239
use FfiResult::*;
1248-
12491240
let transparent_with_all_zst_fields = if def.repr().transparent() {
12501241
if let Some(field) = transparent_newtype_field(self.cx.tcx, variant) {
12511242
// Transparent newtypes have at most one non-ZST field which needs to be checked..
@@ -1372,27 +1363,29 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13721363
return FfiSafe;
13731364
}
13741365

1366+
if def.is_variant_list_non_exhaustive() && !def.did().is_local() {
1367+
return FfiUnsafe {
1368+
ty,
1369+
reason: fluent::lint_improper_ctypes_non_exhaustive,
1370+
help: None,
1371+
};
1372+
}
1373+
13751374
// Check for a repr() attribute to specify the size of the
13761375
// discriminant.
13771376
if !def.repr().c() && !def.repr().transparent() && def.repr().int.is_none()
13781377
{
13791378
// Special-case types like `Option<extern fn()>` and `Result<extern fn(), ()>`
1380-
if repr_nullable_ptr(self.cx.tcx, self.cx.param_env, ty, self.mode)
1381-
.is_none()
1379+
if let Some(ty) =
1380+
repr_nullable_ptr(self.cx.tcx, self.cx.param_env, ty, self.mode)
13821381
{
1383-
return FfiUnsafe {
1384-
ty,
1385-
reason: fluent::lint_improper_ctypes_enum_repr_reason,
1386-
help: Some(fluent::lint_improper_ctypes_enum_repr_help),
1387-
};
1382+
return self.check_type_for_ffi(cache, ty);
13881383
}
1389-
}
13901384

1391-
if def.is_variant_list_non_exhaustive() && !def.did().is_local() {
13921385
return FfiUnsafe {
13931386
ty,
1394-
reason: fluent::lint_improper_ctypes_non_exhaustive,
1395-
help: None,
1387+
reason: fluent::lint_improper_ctypes_enum_repr_reason,
1388+
help: Some(fluent::lint_improper_ctypes_enum_repr_help),
13961389
};
13971390
}
13981391

0 commit comments

Comments
 (0)