@@ -11,7 +11,7 @@ use rustc_hir::def::{CtorKind, DefKind};
1111use rustc_hir:: { LangItem , Node , find_attr, intravisit} ;
1212use rustc_infer:: infer:: { RegionVariableOrigin , TyCtxtInferExt } ;
1313use rustc_infer:: traits:: { Obligation , ObligationCauseCode , WellFormedLoc } ;
14- use rustc_lint_defs:: builtin:: { REPR_TRANSPARENT_NON_ZST_FIELDS , UNSUPPORTED_CALLING_CONVENTIONS } ;
14+ use rustc_lint_defs:: builtin:: UNSUPPORTED_CALLING_CONVENTIONS ;
1515use rustc_macros:: Diagnostic ;
1616use rustc_middle:: hir:: nested_filter;
1717use rustc_middle:: middle:: resolve_bound_vars:: ResolvedArg ;
@@ -1689,39 +1689,6 @@ pub(super) fn check_packed_inner(
16891689}
16901690
16911691pub ( super ) fn check_transparent < ' tcx > ( tcx : TyCtxt < ' tcx > , adt : ty:: AdtDef < ' tcx > ) {
1692- struct ZeroSizedFieldReprTransparentIncompatibility < ' tcx > {
1693- unsuited : UnsuitedInfo < ' tcx > ,
1694- }
1695-
1696- impl < ' a , ' tcx > Diagnostic < ' a , ( ) > for ZeroSizedFieldReprTransparentIncompatibility < ' tcx > {
1697- fn into_diag ( self , dcx : DiagCtxtHandle < ' a > , level : Level ) -> Diag < ' a , ( ) > {
1698- let Self { unsuited } = self ;
1699- let ( title, note) = match unsuited. reason {
1700- UnsuitedReason :: NonExhaustive => (
1701- "external non-exhaustive types" ,
1702- "is marked with `#[non_exhaustive]`, so it could become non-zero-sized in the future." ,
1703- ) ,
1704- UnsuitedReason :: PrivateField => (
1705- "external types with private fields" ,
1706- "contains private fields, so it could become non-zero-sized in the future." ,
1707- ) ,
1708- UnsuitedReason :: ReprC => (
1709- "`repr(C)` types" ,
1710- "is a `#[repr(C)]` type, so it is not guaranteed to be zero-sized on all targets." ,
1711- ) ,
1712- } ;
1713- Diag :: new (
1714- dcx,
1715- level,
1716- format ! ( "zero-sized fields in `repr(transparent)` cannot contain {title}" ) ,
1717- )
1718- . with_note ( format ! (
1719- "this field contains `{field_ty}`, which {note}" ,
1720- field_ty = unsuited. ty,
1721- ) )
1722- }
1723- }
1724-
17251692 if !adt. repr ( ) . transparent ( ) {
17261693 return ;
17271694 }
@@ -1741,108 +1708,137 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
17411708 // Don't bother checking the fields.
17421709 return ;
17431710 }
1711+ let variant = adt. variant ( VariantIdx :: ZERO ) ;
17441712
1745- let typing_env = ty:: TypingEnv :: non_body_analysis ( tcx, adt. did ( ) ) ;
1746- // For each field, figure out if it has "trivial" layout (i.e., is a 1-ZST).
1747- struct FieldInfo < ' tcx > {
1748- span : Span ,
1749- trivial : bool ,
1750- ty : Ty < ' tcx > ,
1751- }
1752-
1753- let field_infos = adt. all_fields ( ) . map ( |field| {
1754- let ty = field. ty ( tcx, GenericArgs :: identity_for_item ( tcx, field. did ) ) ;
1755- let layout = tcx. layout_of ( typing_env. as_query_input ( ty) ) ;
1756- // We are currently checking the type this field came from, so it must be local
1757- let span = tcx. hir_span_if_local ( field. did ) . unwrap ( ) ;
1758- let trivial = layout. is_ok_and ( |layout| layout. is_1zst ( ) ) ;
1759- FieldInfo { span, trivial, ty }
1760- } ) ;
1761-
1762- let non_trivial_fields = field_infos
1763- . clone ( )
1764- . filter_map ( |field| if !field. trivial { Some ( field. span ) } else { None } ) ;
1765- let non_trivial_count = non_trivial_fields. clone ( ) . count ( ) ;
1766- if non_trivial_count >= 2 {
1767- bad_non_zero_sized_fields (
1768- tcx,
1769- adt,
1770- non_trivial_count,
1771- non_trivial_fields,
1772- tcx. def_span ( adt. did ( ) ) ,
1773- ) ;
1713+ if variant. fields . len ( ) <= 1 {
1714+ // No need to check when there's at most one field.
17741715 return ;
17751716 }
17761717
1777- // Even some 1-ZST fields are not allowed though, if they have `non_exhaustive` or private
1778- // fields or `repr(C)`. We call those fields "unsuited".
1779- struct UnsuitedInfo < ' tcx > {
1780- /// The source of the problem, a type that is found somewhere within the field type.
1781- ty : Ty < ' tcx > ,
1782- reason : UnsuitedReason ,
1783- }
1784- enum UnsuitedReason {
1785- NonExhaustive ,
1786- PrivateField ,
1787- ReprC ,
1718+ let typing_env = ty:: TypingEnv :: non_body_analysis ( tcx, adt. did ( ) ) ;
1719+
1720+ /// We call a field "trivial" for `repr(transparent)` purposes if it can be ignored.
1721+ /// IOW, `repr(transparent)` is allowed if there is at most one non-trivial field.
1722+ /// This enum captuers all the reasons why a field might not be "trivial".
1723+ enum NonTrivialReason < ' tcx > {
1724+ UnknownLayout ,
1725+ NonZeroSized ,
1726+ NonTrivialAlignment ,
1727+ PrivateField { inside : Ty < ' tcx > } ,
1728+ NonExhaustive { ty : Ty < ' tcx > } ,
1729+ ReprC { ty : Ty < ' tcx > } ,
1730+ }
1731+ struct NonTrivialFieldInfo < ' tcx > {
1732+ span : Span ,
1733+ reason : NonTrivialReason < ' tcx > ,
17881734 }
17891735
1790- fn check_unsuited < ' tcx > (
1736+ /// Check if this type is "trivial" for `repr(transparent)`. If not, return the reason why
1737+ /// and the problematic type.
1738+ fn is_trivial < ' tcx > (
17911739 tcx : TyCtxt < ' tcx > ,
17921740 typing_env : ty:: TypingEnv < ' tcx > ,
17931741 ty : Ty < ' tcx > ,
1794- ) -> ControlFlow < UnsuitedInfo < ' tcx > > {
1742+ ) -> ControlFlow < NonTrivialReason < ' tcx > > {
17951743 // We can encounter projections during traversal, so ensure the type is normalized.
17961744 let ty =
17971745 tcx. try_normalize_erasing_regions ( typing_env, Unnormalized :: new_wip ( ty) ) . unwrap_or ( ty) ;
17981746 match ty. kind ( ) {
1799- ty:: Tuple ( list) => list. iter ( ) . try_for_each ( |t| check_unsuited ( tcx, typing_env, t) ) ,
1800- ty:: Array ( ty, _) => check_unsuited ( tcx, typing_env, * ty) ,
1747+ ty:: Tuple ( list) => list. iter ( ) . try_for_each ( |t| is_trivial ( tcx, typing_env, t) ) ,
1748+ ty:: Array ( ty, _) => is_trivial ( tcx, typing_env, * ty) ,
18011749 ty:: Adt ( def, args) => {
18021750 if !def. did ( ) . is_local ( ) && !find_attr ! ( tcx, def. did( ) , RustcPubTransparent ( _) ) {
18031751 let non_exhaustive = def. is_variant_list_non_exhaustive ( )
18041752 || def. variants ( ) . iter ( ) . any ( ty:: VariantDef :: is_field_list_non_exhaustive) ;
1753+ if non_exhaustive {
1754+ return ControlFlow :: Break ( NonTrivialReason :: NonExhaustive { ty } ) ;
1755+ }
18051756 let has_priv = def. all_fields ( ) . any ( |f| !f. vis . is_public ( ) ) ;
1806- if non_exhaustive || has_priv {
1807- return ControlFlow :: Break ( UnsuitedInfo {
1808- ty,
1809- reason : if non_exhaustive {
1810- UnsuitedReason :: NonExhaustive
1811- } else {
1812- UnsuitedReason :: PrivateField
1813- } ,
1814- } ) ;
1757+ if has_priv {
1758+ return ControlFlow :: Break ( NonTrivialReason :: PrivateField { inside : ty } ) ;
18151759 }
18161760 }
18171761 if def. repr ( ) . c ( ) {
1818- return ControlFlow :: Break ( UnsuitedInfo { ty, reason : UnsuitedReason :: ReprC } ) ;
1762+ return ControlFlow :: Break ( NonTrivialReason :: ReprC { ty } ) ;
18191763 }
18201764 def. all_fields ( )
18211765 . map ( |field| field. ty ( tcx, args) )
1822- . try_for_each ( |t| check_unsuited ( tcx, typing_env, t) )
1766+ . try_for_each ( |t| is_trivial ( tcx, typing_env, t) )
18231767 }
18241768 _ => ControlFlow :: Continue ( ( ) ) ,
18251769 }
18261770 }
18271771
1828- let mut prev_unsuited_1zst = false ;
1829- for field in field_infos {
1830- if field. trivial
1831- && let Some ( unsuited) = check_unsuited ( tcx, typing_env, field. ty ) . break_value ( )
1832- {
1833- // If there are any non-trivial fields, then there can be no non-exhaustive 1-zsts.
1834- // Otherwise, it's only an issue if there's >1 non-exhaustive 1-zst.
1835- if non_trivial_count > 0 || prev_unsuited_1zst {
1836- tcx. emit_node_span_lint (
1837- REPR_TRANSPARENT_NON_ZST_FIELDS ,
1838- tcx. local_def_id_to_hir_id ( adt. did ( ) . expect_local ( ) ) ,
1839- field. span ,
1840- ZeroSizedFieldReprTransparentIncompatibility { unsuited } ,
1841- ) ;
1842- } else {
1843- prev_unsuited_1zst = true ;
1772+ let non_trivial_fields = variant
1773+ . fields
1774+ . iter ( )
1775+ . filter_map ( |field| {
1776+ let ty = field. ty ( tcx, GenericArgs :: identity_for_item ( tcx, field. did ) ) ;
1777+ let layout = tcx. layout_of ( typing_env. as_query_input ( ty) ) ;
1778+ // We are currently checking the type this field came from, so it must be local
1779+ let span = tcx. hir_span_if_local ( field. did ) . unwrap ( ) ;
1780+ // Rule out non-1ZST
1781+ if !layout. is_ok_and ( |layout| layout. is_1zst ( ) ) {
1782+ let reason = match layout {
1783+ Err ( _) => NonTrivialReason :: UnknownLayout ,
1784+ Ok ( layout) => {
1785+ if !( layout. is_sized ( ) && layout. size . bytes ( ) == 0 ) {
1786+ NonTrivialReason :: NonZeroSized
1787+ } else {
1788+ NonTrivialReason :: NonTrivialAlignment
1789+ }
1790+ }
1791+ } ;
1792+ return Some ( NonTrivialFieldInfo { span, reason } ) ;
1793+ }
1794+ // Recursively check for other things that have to be ruled out.
1795+ if let Some ( reason) = is_trivial ( tcx, typing_env, ty) . break_value ( ) {
1796+ return Some ( NonTrivialFieldInfo { span, reason } ) ;
18441797 }
1798+ // Otherwise,
1799+ None
1800+ } )
1801+ . collect :: < Vec < _ > > ( ) ;
1802+
1803+ if non_trivial_fields. len ( ) >= 2 {
1804+ let count = non_trivial_fields. len ( ) ;
1805+ let desc = if adt. is_enum ( ) {
1806+ format_args ! ( "the variant of a transparent {}" , adt. descr( ) )
1807+ } else {
1808+ format_args ! ( "transparent {}" , adt. descr( ) )
1809+ } ;
1810+ let ty_span = tcx. def_span ( adt. did ( ) ) ;
1811+ let mut diag = tcx. dcx ( ) . struct_span_err (
1812+ ty_span,
1813+ format ! ( "{desc} needs at most one non-trivial field, but has {count}" ) ,
1814+ ) ;
1815+ diag. code ( E0690 ) ;
1816+
1817+ // Label for the type.
1818+ diag. span_label ( ty_span, format ! ( "needs at most one non-trivial field, but has {count}" ) ) ;
1819+ // Label for each non-trivial field.
1820+ for field in non_trivial_fields {
1821+ let msg = match field. reason {
1822+ NonTrivialReason :: UnknownLayout => {
1823+ format ! ( "this field is generic and hence may have non-zero size" )
1824+ }
1825+ NonTrivialReason :: NonZeroSized => format ! ( "this field has non-zero size" ) ,
1826+ NonTrivialReason :: NonTrivialAlignment => format ! ( "this field requires alignment" ) ,
1827+ NonTrivialReason :: PrivateField { inside } => format ! (
1828+ "this field contains `{inside}`, which has private fields, so it could become non-zero-sized in the future"
1829+ ) ,
1830+ NonTrivialReason :: NonExhaustive { ty } => format ! (
1831+ "this field contains `{ty}`, which is marked with `#[non_exhaustive]`, so it could become non-zero-sized in the future"
1832+ ) ,
1833+ NonTrivialReason :: ReprC { ty } => format ! (
1834+ "this field contains `{ty}`, which is a `#[repr(C)]` type, so it is not guaranteed to be zero-sized on all targets"
1835+ ) ,
1836+ } ;
1837+ diag. span_label ( field. span , msg) ;
18451838 }
1839+
1840+ diag. emit ( ) ;
1841+ return ;
18461842 }
18471843}
18481844
0 commit comments