Skip to content

Commit 3f96790

Browse files
Auto merge of #147185 - RalfJung:repr-c-not-zst, r=<try>
repr(transparent): do not consider repr(C) types to be 1-ZST
2 parents bd34871 + 44cb9b1 commit 3f96790

File tree

8 files changed

+206
-132
lines changed

8 files changed

+206
-132
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use rustc_hir::def::{CtorKind, DefKind};
1212
use rustc_hir::{LangItem, Node, attrs, find_attr, intravisit};
1313
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1414
use rustc_infer::traits::{Obligation, ObligationCauseCode, WellFormedLoc};
15-
use rustc_lint_defs::builtin::{
16-
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, UNSUPPORTED_CALLING_CONVENTIONS,
17-
};
15+
use rustc_lint_defs::builtin::UNSUPPORTED_CALLING_CONVENTIONS;
1816
use rustc_middle::hir::nested_filter;
1917
use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
2018
use rustc_middle::middle::stability::EvalResult;
@@ -1510,8 +1508,26 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15101508
return;
15111509
}
15121510

1513-
// For each field, figure out if it's known to have "trivial" layout (i.e., is a 1-ZST), with
1514-
// "known" respecting #[non_exhaustive] attributes.
1511+
// For each field, figure out if it has "trivial" layout (i.e., is a 1-ZST).
1512+
// Even some 1-ZST fields are not allowed though, if they have `non_exhaustive` or private
1513+
// fields or `repr(C)`. We call those fields "unsuited".
1514+
struct FieldInfo<'tcx> {
1515+
span: Span,
1516+
trivial: bool,
1517+
unsuited: Option<UnsuitedInfo<'tcx>>,
1518+
}
1519+
struct UnsuitedInfo<'tcx> {
1520+
/// The source of the problem, a type that is found somewhere within the field type.
1521+
ty: Ty<'tcx>,
1522+
reason: UnsuitedReason,
1523+
}
1524+
enum UnsuitedReason {
1525+
NonExhaustive,
1526+
PrivateField,
1527+
ReprC,
1528+
Uninhabited,
1529+
}
1530+
15151531
let field_infos = adt.all_fields().map(|field| {
15161532
let ty = field.ty(tcx, GenericArgs::identity_for_item(tcx, field.did));
15171533
let typing_env = ty::TypingEnv::non_body_analysis(tcx, field.did);
@@ -1520,17 +1536,18 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15201536
let span = tcx.hir_span_if_local(field.did).unwrap();
15211537
let trivial = layout.is_ok_and(|layout| layout.is_1zst());
15221538
if !trivial {
1523-
return (span, trivial, None);
1539+
// No need to even compute `unsuited`.
1540+
return FieldInfo { span, trivial, unsuited: None };
15241541
}
1525-
// Even some 1-ZST fields are not allowed though, if they have `non_exhaustive`.
15261542

1527-
fn check_non_exhaustive<'tcx>(
1543+
fn check_unsuited<'tcx>(
15281544
tcx: TyCtxt<'tcx>,
1529-
t: Ty<'tcx>,
1530-
) -> ControlFlow<(&'static str, DefId, GenericArgsRef<'tcx>, bool)> {
1531-
match t.kind() {
1532-
ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
1533-
ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
1545+
adt: DefId,
1546+
ty: Ty<'tcx>,
1547+
) -> ControlFlow<UnsuitedInfo<'tcx>> {
1548+
match ty.kind() {
1549+
ty::Tuple(list) => list.iter().try_for_each(|t| check_unsuited(tcx, adt, t)),
1550+
ty::Array(ty, _) => check_unsuited(tcx, adt, *ty),
15341551
ty::Adt(def, args) => {
15351552
if !def.did().is_local()
15361553
&& !find_attr!(
@@ -1545,28 +1562,44 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15451562
.any(ty::VariantDef::is_field_list_non_exhaustive);
15461563
let has_priv = def.all_fields().any(|f| !f.vis.is_public());
15471564
if non_exhaustive || has_priv {
1548-
return ControlFlow::Break((
1549-
def.descr(),
1550-
def.did(),
1551-
args,
1552-
non_exhaustive,
1553-
));
1565+
return ControlFlow::Break(UnsuitedInfo {
1566+
ty,
1567+
reason: if non_exhaustive {
1568+
UnsuitedReason::NonExhaustive
1569+
} else {
1570+
UnsuitedReason::PrivateField
1571+
},
1572+
});
15541573
}
15551574
}
1575+
if def.repr().c() {
1576+
return ControlFlow::Break(UnsuitedInfo {
1577+
ty,
1578+
reason: UnsuitedReason::ReprC,
1579+
});
1580+
}
15561581
def.all_fields()
15571582
.map(|field| field.ty(tcx, args))
1558-
.try_for_each(|t| check_non_exhaustive(tcx, t))
1583+
.try_for_each(|t| check_unsuited(tcx, adt, t))
15591584
}
15601585
_ => ControlFlow::Continue(()),
15611586
}
15621587
}
15631588

1564-
(span, trivial, check_non_exhaustive(tcx, ty).break_value())
1589+
FieldInfo {
1590+
span,
1591+
trivial,
1592+
unsuited: check_unsuited(tcx, adt.did(), ty).break_value().or_else(|| {
1593+
// We don't need to check this recursively, a single top-level check suffices.
1594+
let uninhabited = layout.is_ok_and(|layout| layout.is_uninhabited());
1595+
uninhabited.then_some(UnsuitedInfo { ty, reason: UnsuitedReason::Uninhabited })
1596+
}),
1597+
}
15651598
});
15661599

15671600
let non_trivial_fields = field_infos
15681601
.clone()
1569-
.filter_map(|(span, trivial, _non_exhaustive)| if !trivial { Some(span) } else { None });
1602+
.filter_map(|field| if !field.trivial { Some(field.span) } else { None });
15701603
let non_trivial_count = non_trivial_fields.clone().count();
15711604
if non_trivial_count >= 2 {
15721605
bad_non_zero_sized_fields(
@@ -1578,36 +1611,34 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15781611
);
15791612
return;
15801613
}
1581-
let mut prev_non_exhaustive_1zst = false;
1582-
for (span, _trivial, non_exhaustive_1zst) in field_infos {
1583-
if let Some((descr, def_id, args, non_exhaustive)) = non_exhaustive_1zst {
1614+
1615+
let mut prev_unsuited_1zst = false;
1616+
for field in field_infos {
1617+
if let Some(unsuited) = field.unsuited {
1618+
assert!(field.trivial);
15841619
// If there are any non-trivial fields, then there can be no non-exhaustive 1-zsts.
15851620
// Otherwise, it's only an issue if there's >1 non-exhaustive 1-zst.
1586-
if non_trivial_count > 0 || prev_non_exhaustive_1zst {
1587-
tcx.node_span_lint(
1588-
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
1589-
tcx.local_def_id_to_hir_id(adt.did().expect_local()),
1590-
span,
1591-
|lint| {
1592-
lint.primary_message(
1593-
"zero-sized fields in `repr(transparent)` cannot \
1621+
if non_trivial_count > 0 || prev_unsuited_1zst {
1622+
let mut diag = tcx.dcx().struct_span_err(
1623+
field.span,
1624+
"zero-sized fields in `repr(transparent)` cannot \
15941625
contain external non-exhaustive types",
1595-
);
1596-
let note = if non_exhaustive {
1597-
"is marked with `#[non_exhaustive]`"
1598-
} else {
1599-
"contains private fields"
1600-
};
1601-
let field_ty = tcx.def_path_str_with_args(def_id, args);
1602-
lint.note(format!(
1603-
"this {descr} contains `{field_ty}`, which {note}, \
1626+
);
1627+
let note = match unsuited.reason {
1628+
UnsuitedReason::NonExhaustive => "is marked with `#[non_exhaustive]`",
1629+
UnsuitedReason::PrivateField => "contains private fields",
1630+
UnsuitedReason::ReprC => "is marked with `#[repr(C)]`",
1631+
UnsuitedReason::Uninhabited => "is not inhabited",
1632+
};
1633+
diag.note(format!(
1634+
"this field contains `{field_ty}`, which {note}, \
16041635
and makes it not a breaking change to become \
1605-
non-zero-sized in the future."
1606-
));
1607-
},
1608-
)
1636+
non-zero-sized in the future.",
1637+
field_ty = unsuited.ty,
1638+
));
1639+
diag.emit();
16091640
} else {
1610-
prev_non_exhaustive_1zst = true;
1641+
prev_unsuited_1zst = true;
16111642
}
16121643
}
16131644
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pub enum FutureIncompatibilityReason {
403403
///
404404
/// After a lint has been in this state for a while and you feel like it is ready to graduate
405405
/// to warning everyone, consider setting [`FutureIncompatibleInfo::report_in_deps`] to true.
406-
/// (see it's documentation for more guidance)
406+
/// (see its documentation for more guidance)
407407
///
408408
/// After some period of time, lints with this variant can be turned into
409409
/// hard errors (and the lint removed). Preferably when there is some

tests/ui/repr/repr-transparent-non-exhaustive.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,82 +36,66 @@ pub struct T4(Sized, ExternalIndirection<(InternalPrivate, InternalNonExhaustive
3636
#[repr(transparent)]
3737
pub struct T5(Sized, Private);
3838
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
39-
//~| WARN this was previously accepted by the compiler
4039

4140
#[repr(transparent)]
4241
pub struct T6(Sized, NonExhaustive);
4342
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
44-
//~| WARN this was previously accepted by the compiler
4543

4644
#[repr(transparent)]
4745
pub struct T7(Sized, NonExhaustiveEnum);
4846
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
49-
//~| WARN this was previously accepted by the compiler
5047

5148
#[repr(transparent)]
5249
pub struct T8(Sized, NonExhaustiveVariant);
5350
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
54-
//~| WARN this was previously accepted by the compiler
5551

5652
#[repr(transparent)]
5753
pub struct T9(Sized, InternalIndirection<Private>);
5854
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
59-
//~| WARN this was previously accepted by the compiler
6055

6156
#[repr(transparent)]
6257
pub struct T10(Sized, InternalIndirection<NonExhaustive>);
6358
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
64-
//~| WARN this was previously accepted by the compiler
6559

6660
#[repr(transparent)]
6761
pub struct T11(Sized, InternalIndirection<NonExhaustiveEnum>);
6862
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
69-
//~| WARN this was previously accepted by the compiler
7063

7164
#[repr(transparent)]
7265
pub struct T12(Sized, InternalIndirection<NonExhaustiveVariant>);
7366
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
74-
//~| WARN this was previously accepted by the compiler
7567

7668
#[repr(transparent)]
7769
pub struct T13(Sized, ExternalIndirection<Private>);
7870
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
79-
//~| WARN this was previously accepted by the compiler
8071

8172
#[repr(transparent)]
8273
pub struct T14(Sized, ExternalIndirection<NonExhaustive>);
8374
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
84-
//~| WARN this was previously accepted by the compiler
8575

8676
#[repr(transparent)]
8777
pub struct T15(Sized, ExternalIndirection<NonExhaustiveEnum>);
8878
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
89-
//~| WARN this was previously accepted by the compiler
9079

9180
#[repr(transparent)]
9281
pub struct T16(Sized, ExternalIndirection<NonExhaustiveVariant>);
9382
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
94-
//~| WARN this was previously accepted by the compiler
9583

9684
#[repr(transparent)]
9785
pub struct T17(NonExhaustive, Sized);
9886
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
99-
//~| WARN this was previously accepted by the compiler
10087

10188
#[repr(transparent)]
10289
pub struct T18(NonExhaustive, NonExhaustive);
10390
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
104-
//~| WARN this was previously accepted by the compiler
10591

10692
#[repr(transparent)]
10793
pub struct T19(NonExhaustive, Private);
10894
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
109-
//~| WARN this was previously accepted by the compiler
11095

11196
#[repr(transparent)]
11297
pub struct T19Flipped(Private, NonExhaustive);
11398
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
114-
//~| WARN this was previously accepted by the compiler
11599

116100
#[repr(transparent)]
117101
pub struct T20(NonExhaustive);

0 commit comments

Comments
 (0)