@@ -12,9 +12,7 @@ use rustc_hir::def::{CtorKind, DefKind};
1212use rustc_hir:: { LangItem , Node , attrs, find_attr, intravisit} ;
1313use rustc_infer:: infer:: { RegionVariableOrigin , TyCtxtInferExt } ;
1414use 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 ;
1816use rustc_middle:: hir:: nested_filter;
1917use rustc_middle:: middle:: resolve_bound_vars:: ResolvedArg ;
2018use 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 }
0 commit comments