@@ -402,8 +402,24 @@ impl<'tcx> AdtDef {
402
402
tcx : TyCtxt < ' tcx > ,
403
403
) -> impl Iterator < Item = ( VariantIdx , Discr < ' tcx > ) > + Captures < ' tcx > {
404
404
assert ! ( self . is_enum( ) ) ;
405
+ let no_explicit_discriminants = self
406
+ . variants
407
+ . iter_enumerated ( )
408
+ . all ( |( i, v) | v. discr == ty:: VariantDiscr :: Relative ( i. as_u32 ( ) ) ) ;
409
+
410
+ let mut any_dataful_variants = false ;
411
+ for fields in self . variants . iter ( ) {
412
+ if fields. fields . len ( ) > 0 {
413
+ any_dataful_variants = true ;
414
+ break ;
415
+ }
416
+ }
417
+ let initial_discr = no_explicit_discriminants
418
+ && !self . repr . inhibit_enum_layout_opt ( )
419
+ && !self . repr . inhibit_struct_field_reordering_opt ( )
420
+ && !any_dataful_variants;
405
421
let repr_type = self . repr . discr_type ( ) ;
406
- let initial = repr_type. initial_discriminant ( tcx) ;
422
+ let initial = Discr { val : initial_discr as u128 , ty : repr_type. to_ty ( tcx) } ;
407
423
let mut prev_discr = None :: < Discr < ' tcx > > ;
408
424
self . variants . iter_enumerated ( ) . map ( move |( i, v) | {
409
425
let mut discr = prev_discr. map_or ( initial, |d| d. wrap_incr ( tcx) ) ;
@@ -436,9 +452,26 @@ impl<'tcx> AdtDef {
436
452
) -> Discr < ' tcx > {
437
453
assert ! ( self . is_enum( ) ) ;
438
454
let ( val, offset) = self . discriminant_def_for_variant ( variant_index) ;
455
+ let no_explicit_discriminants = self
456
+ . variants
457
+ . iter_enumerated ( )
458
+ . all ( |( i, v) | v. discr == ty:: VariantDiscr :: Relative ( i. as_u32 ( ) ) ) ;
459
+
460
+ let mut any_dataful_variants = false ;
461
+ for fields in self . variants . iter ( ) {
462
+ if fields. fields . len ( ) > 0 {
463
+ any_dataful_variants = true ;
464
+ break ;
465
+ }
466
+ }
467
+ let initial_discr = no_explicit_discriminants
468
+ && !self . repr . inhibit_enum_layout_opt ( )
469
+ && !self . repr . inhibit_struct_field_reordering_opt ( )
470
+ && !any_dataful_variants;
471
+ let initial = Discr { val : initial_discr as u128 , ty : self . repr . discr_type ( ) . to_ty ( tcx) } ;
439
472
let explicit_value = val
440
473
. and_then ( |expr_did| self . eval_explicit_discr ( tcx, expr_did) )
441
- . unwrap_or_else ( || self . repr . discr_type ( ) . initial_discriminant ( tcx ) ) ;
474
+ . unwrap_or_else ( || initial ) ;
442
475
explicit_value. checked_add ( tcx, offset as u128 ) . 0
443
476
}
444
477
0 commit comments