@@ -1358,17 +1358,49 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13581358 TyKind :: Err => {
13591359 hir:: TyKind :: Err ( self . dcx ( ) . span_delayed_bug ( t. span , "TyKind::Err lowered" ) )
13601360 }
1361- // FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1362- #[ allow( rustc:: untranslatable_diagnostic) ]
1363- #[ allow( rustc:: diagnostic_outside_of_impl) ]
1364- TyKind :: AnonStruct ( ref _fields) => {
1365- hir:: TyKind :: Err ( self . dcx ( ) . span_err ( t. span , "anonymous structs are unimplemented" ) )
1366- }
1367- // FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
1368- #[ allow( rustc:: untranslatable_diagnostic) ]
1369- #[ allow( rustc:: diagnostic_outside_of_impl) ]
1370- TyKind :: AnonUnion ( ref _fields) => {
1371- hir:: TyKind :: Err ( self . dcx ( ) . span_err ( t. span , "anonymous unions are unimplemented" ) )
1361+ // Lower the anonymous structs or unions in a nested lowering context.
1362+ //
1363+ // ```
1364+ // struct Foo {
1365+ // _: union {
1366+ // // ^__________________ <-- within the nested lowering context,
1367+ // /* fields */ // | we lower all fields defined into an
1368+ // } // | owner node of struct or union item
1369+ // // ^_____________________|
1370+ // }
1371+ //
1372+ TyKind :: AnonStruct ( def_node_id, fields) | TyKind :: AnonUnion ( def_node_id, fields) => {
1373+ let ( def_kind, item_kind) : ( DefKind , fn ( _, _) -> _ ) = match t. kind {
1374+ TyKind :: AnonStruct ( ..) => ( DefKind :: Struct , hir:: ItemKind :: Struct ) ,
1375+ TyKind :: AnonUnion ( ..) => ( DefKind :: Union , hir:: ItemKind :: Union ) ,
1376+ _ => unreachable ! ( ) ,
1377+ } ;
1378+ let def_id = self . create_def (
1379+ self . current_hir_id_owner . def_id ,
1380+ * def_node_id,
1381+ kw:: Empty ,
1382+ def_kind,
1383+ t. span ,
1384+ ) ;
1385+ debug ! ( ?def_id) ;
1386+ let owner_id = hir:: OwnerId { def_id } ;
1387+ self . with_hir_id_owner ( * def_node_id, |this| {
1388+ let fields = this. arena . alloc_from_iter (
1389+ fields. iter ( ) . enumerate ( ) . map ( |f| this. lower_field_def ( f) ) ,
1390+ ) ;
1391+ let span = t. span ;
1392+ let variant_data = hir:: VariantData :: Struct ( fields, false ) ;
1393+ // FIXME: capture the generics from the outer adt.
1394+ let generics = hir:: Generics :: empty ( ) ;
1395+ hir:: OwnerNode :: Item ( this. arena . alloc ( hir:: Item {
1396+ ident : Ident :: new ( kw:: Empty , span) ,
1397+ owner_id,
1398+ kind : item_kind ( variant_data, generics) ,
1399+ span : this. lower_span ( span) ,
1400+ vis_span : this. lower_span ( span. shrink_to_lo ( ) ) ,
1401+ } ) )
1402+ } ) ;
1403+ hir:: TyKind :: AnonAdt ( hir:: ItemId { owner_id } )
13721404 }
13731405 TyKind :: Slice ( ty) => hir:: TyKind :: Slice ( self . lower_ty ( ty, itctx) ) ,
13741406 TyKind :: Ptr ( mt) => hir:: TyKind :: Ptr ( self . lower_mt ( mt, itctx) ) ,
0 commit comments