@@ -17,6 +17,7 @@ use tracing::instrument;
17
17
18
18
use super :: errors:: {
19
19
InvalidAbi , InvalidAbiSuggestion , MisplacedRelaxTraitBound , TupleStructWithDefault ,
20
+ UnionWithDefault ,
20
21
} ;
21
22
use super :: stability:: { enabled_names, gate_unstable_abi} ;
22
23
use super :: {
@@ -316,7 +317,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
316
317
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
317
318
|this| {
318
319
this. arena . alloc_from_iter (
319
- enum_definition. variants . iter ( ) . map ( |x| this. lower_variant ( x) ) ,
320
+ enum_definition. variants . iter ( ) . map ( |x| this. lower_variant ( i , x) ) ,
320
321
)
321
322
} ,
322
323
) ;
@@ -328,7 +329,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
328
329
generics,
329
330
id,
330
331
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
331
- |this| this. lower_variant_data ( hir_id, struct_def) ,
332
+ |this| this. lower_variant_data ( hir_id, i , struct_def) ,
332
333
) ;
333
334
hir:: ItemKind :: Struct ( ident, generics, struct_def)
334
335
}
@@ -338,7 +339,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
338
339
generics,
339
340
id,
340
341
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
341
- |this| this. lower_variant_data ( hir_id, vdata) ,
342
+ |this| this. lower_variant_data ( hir_id, i , vdata) ,
342
343
) ;
343
344
hir:: ItemKind :: Union ( ident, generics, vdata)
344
345
}
@@ -714,13 +715,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
714
715
}
715
716
}
716
717
717
- fn lower_variant ( & mut self , v : & Variant ) -> hir:: Variant < ' hir > {
718
+ fn lower_variant ( & mut self , item_kind : & ItemKind , v : & Variant ) -> hir:: Variant < ' hir > {
718
719
let hir_id = self . lower_node_id ( v. id ) ;
719
720
self . lower_attrs ( hir_id, & v. attrs , v. span ) ;
720
721
hir:: Variant {
721
722
hir_id,
722
723
def_id : self . local_def_id ( v. id ) ,
723
- data : self . lower_variant_data ( hir_id, & v. data ) ,
724
+ data : self . lower_variant_data ( hir_id, item_kind , & v. data ) ,
724
725
disr_expr : v. disr_expr . as_ref ( ) . map ( |e| self . lower_anon_const_to_anon_const ( e) ) ,
725
726
ident : self . lower_ident ( v. ident ) ,
726
727
span : self . lower_span ( v. span ) ,
@@ -730,15 +731,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
730
731
fn lower_variant_data (
731
732
& mut self ,
732
733
parent_id : hir:: HirId ,
734
+ item_kind : & ItemKind ,
733
735
vdata : & VariantData ,
734
736
) -> hir:: VariantData < ' hir > {
735
737
match vdata {
736
- VariantData :: Struct { fields, recovered } => hir :: VariantData :: Struct {
737
- fields : self
738
+ VariantData :: Struct { fields, recovered } => {
739
+ let fields = self
738
740
. arena
739
- . alloc_from_iter ( fields. iter ( ) . enumerate ( ) . map ( |f| self . lower_field_def ( f) ) ) ,
740
- recovered : * recovered,
741
- } ,
741
+ . alloc_from_iter ( fields. iter ( ) . enumerate ( ) . map ( |f| self . lower_field_def ( f) ) ) ;
742
+
743
+ if let ItemKind :: Union ( ..) = item_kind {
744
+ for field in & fields[ ..] {
745
+ if let Some ( default) = field. default {
746
+ // Unions cannot derive `Default`, and it's not clear how to use default
747
+ // field values of unions if that was supported. Therefore, blanket reject
748
+ // trying to use field values with unions.
749
+ if self . tcx . features ( ) . default_field_values ( ) {
750
+ self . dcx ( ) . emit_err ( UnionWithDefault { span : default. span } ) ;
751
+ } else {
752
+ let _ = self . dcx ( ) . span_delayed_bug (
753
+ default. span ,
754
+ "expected union default field values feature gate error but none \
755
+ was produced",
756
+ ) ;
757
+ }
758
+ }
759
+ }
760
+ }
761
+
762
+ hir:: VariantData :: Struct { fields, recovered : * recovered }
763
+ }
742
764
VariantData :: Tuple ( fields, id) => {
743
765
let ctor_id = self . lower_node_id ( * id) ;
744
766
self . alias_attrs ( ctor_id, parent_id) ;
0 commit comments