@@ -23,7 +23,7 @@ use rustc_middle::ty::subst::SubstsRef;
23
23
use rustc_middle:: ty:: { self , AdtDef , Ty , UpvarSubsts } ;
24
24
use rustc_middle:: ty:: { CanonicalUserType , CanonicalUserTypeAnnotation } ;
25
25
use rustc_span:: def_id:: LocalDefId ;
26
- use rustc_span:: { Span , Symbol , DUMMY_SP } ;
26
+ use rustc_span:: { sym , Span , Symbol , DUMMY_SP } ;
27
27
use rustc_target:: abi:: VariantIdx ;
28
28
use rustc_target:: asm:: InlineAsmRegOrRegClass ;
29
29
use std:: fmt;
@@ -695,17 +695,32 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
695
695
Ok ( ( ) )
696
696
}
697
697
PatKind :: Variant { ref subpatterns, .. } | PatKind :: Leaf { ref subpatterns } => {
698
- let variant = match self . kind {
699
- PatKind :: Variant { adt_def, variant_index, .. } => {
700
- Some ( adt_def. variant ( variant_index) )
701
- }
702
- _ => self . ty . ty_adt_def ( ) . and_then ( |adt| {
703
- if !adt. is_enum ( ) { Some ( adt. non_enum_variant ( ) ) } else { None }
698
+ let variant_and_name = match self . kind {
699
+ PatKind :: Variant { adt_def, variant_index, .. } => ty:: tls:: with ( |tcx| {
700
+ let variant = adt_def. variant ( variant_index) ;
701
+ let adt_did = adt_def. did ( ) ;
702
+ let name = if tcx. get_diagnostic_item ( sym:: Option ) == Some ( adt_did)
703
+ || tcx. get_diagnostic_item ( sym:: Result ) == Some ( adt_did)
704
+ {
705
+ variant. name . to_string ( )
706
+ } else {
707
+ format ! ( "{}::{}" , tcx. def_path_str( adt_def. did( ) ) , variant. name)
708
+ } ;
709
+ Some ( ( variant, name) )
710
+ } ) ,
711
+ _ => self . ty . ty_adt_def ( ) . and_then ( |adt_def| {
712
+ if !adt_def. is_enum ( ) {
713
+ ty:: tls:: with ( |tcx| {
714
+ Some ( ( adt_def. non_enum_variant ( ) , tcx. def_path_str ( adt_def. did ( ) ) ) )
715
+ } )
716
+ } else {
717
+ None
718
+ }
704
719
} ) ,
705
720
} ;
706
721
707
- if let Some ( variant) = variant {
708
- write ! ( f, "{}" , variant . name) ?;
722
+ if let Some ( ( variant, name ) ) = & variant_and_name {
723
+ write ! ( f, "{}" , name) ?;
709
724
710
725
// Only for Adt we can have `S {...}`,
711
726
// which we handle separately here.
@@ -730,8 +745,9 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
730
745
}
731
746
}
732
747
733
- let num_fields = variant. map_or ( subpatterns. len ( ) , |v| v. fields . len ( ) ) ;
734
- if num_fields != 0 || variant. is_none ( ) {
748
+ let num_fields =
749
+ variant_and_name. as_ref ( ) . map_or ( subpatterns. len ( ) , |( v, _) | v. fields . len ( ) ) ;
750
+ if num_fields != 0 || variant_and_name. is_none ( ) {
735
751
write ! ( f, "(" ) ?;
736
752
for i in 0 ..num_fields {
737
753
write ! ( f, "{}" , start_or_comma( ) ) ?;
0 commit comments