Skip to content

Commit 4b634ed

Browse files
committed
Don't lint niche optimized variants in enums
1 parent 100cd22 commit 4b634ed

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
@@ -1096,14 +1096,6 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
10961096
[var_one, var_two] => match (&var_one.fields.raw[..], &var_two.fields.raw[..]) {
10971097
([], [field]) | ([field], []) => field.ty(tcx, args),
10981098
([field1], [field2]) => {
1099-
// TODO: We pass all the checks here although individual enum variants has
1100-
// checks for FFI safety even when niche optimized which needs to be
1101-
// suppressed. for types like `Result<PhantomData<()>, E>`, PhantomData has
1102-
// it's own lint for FFI which needs to be suppressed: `composed only of
1103-
// `PhantomData``. This is true for other custom types as well `struct
1104-
// Example;` which emits `this struct has unspecified layout` and suggests to
1105-
// add `#[repr(C)]` and when that is done, linter emits `this struct has no
1106-
// fields`, all under the `improper_ctypes_definitions` lint group
11071099
let ty1 = field1.ty(tcx, args);
11081100
let ty2 = field2.ty(tcx, args);
11091101

@@ -1200,7 +1192,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12001192
args: GenericArgsRef<'tcx>,
12011193
) -> FfiResult<'tcx> {
12021194
use FfiResult::*;
1203-
12041195
let transparent_with_all_zst_fields = if def.repr().transparent() {
12051196
if let Some(field) = transparent_newtype_field(self.cx.tcx, variant) {
12061197
// Transparent newtypes have at most one non-ZST field which needs to be checked..
@@ -1327,27 +1318,29 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13271318
return FfiSafe;
13281319
}
13291320

1321+
if def.is_variant_list_non_exhaustive() && !def.did().is_local() {
1322+
return FfiUnsafe {
1323+
ty,
1324+
reason: fluent::lint_improper_ctypes_non_exhaustive,
1325+
help: None,
1326+
};
1327+
}
1328+
13301329
// Check for a repr() attribute to specify the size of the
13311330
// discriminant.
13321331
if !def.repr().c() && !def.repr().transparent() && def.repr().int.is_none()
13331332
{
13341333
// Special-case types like `Option<extern fn()>` and `Result<extern fn(), ()>`
1335-
if repr_nullable_ptr(self.cx.tcx, self.cx.param_env, ty, self.mode)
1336-
.is_none()
1334+
if let Some(ty) =
1335+
repr_nullable_ptr(self.cx.tcx, self.cx.param_env, ty, self.mode)
13371336
{
1338-
return FfiUnsafe {
1339-
ty,
1340-
reason: fluent::lint_improper_ctypes_enum_repr_reason,
1341-
help: Some(fluent::lint_improper_ctypes_enum_repr_help),
1342-
};
1337+
return self.check_type_for_ffi(cache, ty);
13431338
}
1344-
}
13451339

1346-
if def.is_variant_list_non_exhaustive() && !def.did().is_local() {
13471340
return FfiUnsafe {
13481341
ty,
1349-
reason: fluent::lint_improper_ctypes_non_exhaustive,
1350-
help: None,
1342+
reason: fluent::lint_improper_ctypes_enum_repr_reason,
1343+
help: Some(fluent::lint_improper_ctypes_enum_repr_help),
13511344
};
13521345
}
13531346

0 commit comments

Comments
 (0)