Skip to content

Commit c1dcd7e

Browse files
committed
use non-semver-aware uninhabited check
1 parent eee5199 commit c1dcd7e

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,12 +1516,13 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15161516
// We are currently checking the type this field came from, so it must be local
15171517
let span = tcx.hir_span_if_local(field.did).unwrap();
15181518
let trivial = layout.is_ok_and(|layout| layout.is_1zst());
1519-
(span, trivial, ty)
1519+
let uninhabited = layout.is_ok_and(|layout| layout.is_uninhabited());
1520+
(span, trivial, uninhabited, ty)
15201521
});
15211522

1522-
let non_trivial_fields = field_infos
1523-
.clone()
1524-
.filter_map(|(span, trivial, _non_exhaustive)| if !trivial { Some(span) } else { None });
1523+
let non_trivial_fields = field_infos.clone().filter_map(
1524+
|(span, trivial, _uninhabited, _non_exhaustive)| if !trivial { Some(span) } else { None },
1525+
);
15251526
let non_trivial_count = non_trivial_fields.clone().count();
15261527
if non_trivial_count >= 2 {
15271528
bad_non_zero_sized_fields(
@@ -1553,9 +1554,6 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15531554
adt: DefId,
15541555
ty: Ty<'tcx>,
15551556
) -> ControlFlow<UnsuitedInfo<'tcx>> {
1556-
if !ty.is_inhabited_from(tcx, adt, ty::TypingEnv::non_body_analysis(tcx, adt)) {
1557-
return ControlFlow::Break(UnsuitedInfo { ty, reason: UnsuitedReason::Uninhabited });
1558-
}
15591557
match ty.kind() {
15601558
ty::Tuple(list) => list.iter().try_for_each(|t| check_unsuited_1zst(tcx, adt, t)),
15611559
ty::Array(ty, _) => check_unsuited_1zst(tcx, adt, *ty),
@@ -1589,11 +1587,15 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15891587
}
15901588

15911589
let mut prev_unsuited_1zst = false;
1592-
for (span, trivial, ty) in field_infos {
1590+
for (span, trivial, uninhabited, ty) in field_infos {
15931591
if !trivial {
15941592
continue;
15951593
}
1596-
if let Some(unsuited) = check_unsuited_1zst(tcx, adt.did(), ty).break_value() {
1594+
if let Some(unsuited) =
1595+
check_unsuited_1zst(tcx, adt.did(), ty).break_value().or_else(|| {
1596+
uninhabited.then_some(UnsuitedInfo { ty, reason: UnsuitedReason::Uninhabited })
1597+
})
1598+
{
15971599
// If there are any non-trivial fields, then there can be no non-exhaustive 1-zsts.
15981600
// Otherwise, it's only an issue if there's >1 non-exhaustive 1-zst.
15991601
if non_trivial_count > 0 || prev_unsuited_1zst {
@@ -1606,7 +1608,7 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
16061608
UnsuitedReason::NonExhaustive => "is marked with `#[non_exhaustive]`",
16071609
UnsuitedReason::PrivateField => "contains private fields",
16081610
UnsuitedReason::ReprC => "is marked with `#[repr(C)]`",
1609-
UnsuitedReason::Uninhabited => "is not (publicly) inhabited",
1611+
UnsuitedReason::Uninhabited => "is not inhabited",
16101612
};
16111613
diag.note(format!(
16121614
"this field contains `{field_ty}`, which {note}, \

tests/ui/repr/repr-transparent-uninhabited.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error: zero-sized fields in `repr(transparent)` cannot contain external non-exha
44
LL | pub struct T5(Sized, Void);
55
| ^^^^
66
|
7-
= note: this field contains `Void`, which is not (publicly) inhabited, and makes it not a breaking change to become non-zero-sized in the future.
7+
= note: this field contains `Void`, which is not inhabited, and makes it not a breaking change to become non-zero-sized in the future.
88

99
error: zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
1010
--> $DIR/repr-transparent-uninhabited.rs:20:15
1111
|
1212
LL | pub struct T6(!, Sized);
1313
| ^
1414
|
15-
= note: this field contains `!`, which is not (publicly) inhabited, and makes it not a breaking change to become non-zero-sized in the future.
15+
= note: this field contains `!`, which is not inhabited, and makes it not a breaking change to become non-zero-sized in the future.
1616

1717
error: aborting due to 2 previous errors
1818

0 commit comments

Comments
 (0)