12
12
use std:: fmt;
13
13
14
14
use rustc_abi:: { FieldIdx , VariantIdx } ;
15
- use rustc_middle:: bug;
16
15
use rustc_middle:: ty:: { self , AdtDef , Ty , TyCtxt } ;
16
+ use rustc_middle:: { bug, thir} ;
17
17
use rustc_span:: sym;
18
18
19
19
#[ derive( Clone , Debug ) ]
@@ -44,12 +44,33 @@ pub(crate) enum EnumInfo<'tcx> {
44
44
NotEnum ,
45
45
}
46
46
47
+ fn erase_path_if_local < ' tcx > (
48
+ tcx : TyCtxt < ' _ > ,
49
+ adt_def : AdtDef < ' tcx > ,
50
+ scrut : & thir:: Expr < ' tcx > ,
51
+ ) -> bool {
52
+ let enum_parent_def_id = tcx. parent ( adt_def. did ( ) ) ;
53
+ let scrut_parent_def_id = if let thir:: ExprKind :: Scope { region_scope : _, lint_level, value : _ } =
54
+ scrut. kind
55
+ && let thir:: LintLevel :: Explicit ( hir_id) = lint_level
56
+ {
57
+ Some ( hir_id. owner . to_def_id ( ) )
58
+ } else {
59
+ None
60
+ } ;
61
+ if scrut_parent_def_id == Some ( enum_parent_def_id) {
62
+ return true ;
63
+ }
64
+ false
65
+ }
66
+
47
67
pub ( crate ) fn write_struct_like < ' tcx > (
48
68
f : & mut impl fmt:: Write ,
49
69
tcx : TyCtxt < ' _ > ,
50
70
ty : Ty < ' tcx > ,
51
71
enum_info : & EnumInfo < ' tcx > ,
52
72
subpatterns : & [ FieldPat ] ,
73
+ scrut : Option < & thir:: Expr < ' tcx > > ,
53
74
) -> fmt:: Result {
54
75
let variant_and_name = match * enum_info {
55
76
EnumInfo :: Enum { adt_def, variant_index } => {
@@ -60,7 +81,18 @@ pub(crate) fn write_struct_like<'tcx>(
60
81
{
61
82
variant. name . to_string ( )
62
83
} else {
63
- format ! ( "{}::{}" , tcx. def_path_str( adt_def. did( ) ) , variant. name)
84
+ let enum_and_variant = if let Some ( scrut) = scrut
85
+ && erase_path_if_local ( tcx, adt_def, scrut)
86
+ {
87
+ ty:: print:: with_forced_trimmed_paths!( format!(
88
+ "{}::{}" ,
89
+ tcx. def_path_str( adt_def. did( ) ) ,
90
+ variant. name
91
+ ) )
92
+ } else {
93
+ format ! ( "{}::{}" , tcx. def_path_str( adt_def. did( ) ) , variant. name)
94
+ } ;
95
+ enum_and_variant
64
96
} ;
65
97
Some ( ( variant, name) )
66
98
}
0 commit comments