@@ -44,7 +44,7 @@ use hir;
44
44
use hir:: map:: Definitions ;
45
45
use hir:: map:: definitions:: DefPathData ;
46
46
use hir:: def_id:: { DefIndex , DefId } ;
47
- use hir:: def:: Def ;
47
+ use hir:: def:: { Def , PathResolution } ;
48
48
49
49
use std:: collections:: BTreeMap ;
50
50
use std:: iter;
@@ -53,7 +53,7 @@ use syntax::attr::{ThinAttributes, ThinAttributesExt};
53
53
use syntax:: ext:: mtwt;
54
54
use syntax:: ptr:: P ;
55
55
use syntax:: codemap:: { respan, Spanned , Span } ;
56
- use syntax:: parse:: token;
56
+ use syntax:: parse:: token:: { self , keywords } ;
57
57
use syntax:: std_inject;
58
58
use syntax:: visit:: { self , Visitor } ;
59
59
@@ -72,6 +72,9 @@ pub trait Resolver {
72
72
// Resolve a global hir path generated by the lowerer when expanding `for`, `if let`, etc.
73
73
fn resolve_generated_global_path ( & mut self , path : & hir:: Path , is_value : bool ) -> Def ;
74
74
75
+ // Obtain the resolution for a node id
76
+ fn get_resolution ( & mut self , id : NodeId ) -> Option < PathResolution > ;
77
+
75
78
// Record the resolution of a path or binding generated by the lowerer when expanding.
76
79
fn record_resolution ( & mut self , id : NodeId , def : Def ) ;
77
80
@@ -85,6 +88,9 @@ impl Resolver for DummyResolver {
85
88
fn resolve_generated_global_path ( & mut self , _path : & hir:: Path , _is_value : bool ) -> Def {
86
89
Def :: Err
87
90
}
91
+ fn get_resolution ( & mut self , _id : NodeId ) -> Option < PathResolution > {
92
+ None
93
+ }
88
94
fn record_resolution ( & mut self , _id : NodeId , _def : Def ) { }
89
95
fn definitions ( & mut self ) -> Option < & mut Definitions > {
90
96
None
@@ -170,7 +176,11 @@ impl<'a> LoweringContext<'a> {
170
176
}
171
177
172
178
fn lower_ident ( & mut self , ident : Ident ) -> Name {
173
- mtwt:: resolve ( ident)
179
+ if ident. name != keywords:: Invalid . name ( ) {
180
+ mtwt:: resolve ( ident)
181
+ } else {
182
+ ident. name
183
+ }
174
184
}
175
185
176
186
fn lower_attrs ( & mut self , attrs : & Vec < Attribute > ) -> hir:: HirVec < Attribute > {
@@ -315,18 +325,14 @@ impl<'a> LoweringContext<'a> {
315
325
}
316
326
}
317
327
318
- // Path segments are usually unhygienic, hygienic path segments can occur only in
319
- // identifier-like paths originating from `ExprPath`.
320
- // Make life simpler for rustc_resolve by renaming only such segments.
321
- fn lower_path_full ( & mut self , p : & Path , maybe_hygienic : bool ) -> hir:: Path {
322
- let maybe_hygienic = maybe_hygienic && !p. global && p. segments . len ( ) == 1 ;
328
+ fn lower_path_full ( & mut self , p : & Path , rename : bool ) -> hir:: Path {
323
329
hir:: Path {
324
330
global : p. global ,
325
331
segments : p. segments
326
332
. iter ( )
327
333
. map ( |& PathSegment { identifier, ref parameters } | {
328
334
hir:: PathSegment {
329
- name : if maybe_hygienic {
335
+ name : if rename {
330
336
self . lower_ident ( identifier)
331
337
} else {
332
338
identifier. name
@@ -846,9 +852,14 @@ impl<'a> LoweringContext<'a> {
846
852
PatKind :: Wild => hir:: PatKind :: Wild ,
847
853
PatKind :: Ident ( ref binding_mode, pth1, ref sub) => {
848
854
self . with_parent_def ( p. id , |this| {
855
+ let name = match this. resolver . get_resolution ( p. id ) . map ( |d| d. full_def ( ) ) {
856
+ // Only pattern bindings are renamed
857
+ None | Some ( Def :: Local ( ..) ) => this. lower_ident ( pth1. node ) ,
858
+ _ => pth1. node . name ,
859
+ } ;
849
860
hir:: PatKind :: Ident ( this. lower_binding_mode ( binding_mode) ,
850
- respan ( pth1. span , this . lower_ident ( pth1 . node ) ) ,
851
- sub. as_ref ( ) . map ( |x| this. lower_pat ( x) ) )
861
+ respan ( pth1. span , name ) ,
862
+ sub. as_ref ( ) . map ( |x| this. lower_pat ( x) ) )
852
863
} )
853
864
}
854
865
PatKind :: Lit ( ref e) => hir:: PatKind :: Lit ( self . lower_expr ( e) ) ,
@@ -1212,7 +1223,16 @@ impl<'a> LoweringContext<'a> {
1212
1223
position : position,
1213
1224
}
1214
1225
} ) ;
1215
- hir:: ExprPath ( hir_qself, self . lower_path_full ( path, qself. is_none ( ) ) )
1226
+ let rename = if path. segments . len ( ) == 1 {
1227
+ // Only local variables are renamed
1228
+ match self . resolver . get_resolution ( e. id ) . map ( |d| d. full_def ( ) ) {
1229
+ Some ( Def :: Local ( ..) ) | Some ( Def :: Upvar ( ..) ) => true ,
1230
+ _ => false ,
1231
+ }
1232
+ } else {
1233
+ false
1234
+ } ;
1235
+ hir:: ExprPath ( hir_qself, self . lower_path_full ( path, rename) )
1216
1236
}
1217
1237
ExprKind :: Break ( opt_ident) => hir:: ExprBreak ( opt_ident. map ( |sp_ident| {
1218
1238
respan ( sp_ident. span , self . lower_ident ( sp_ident. node ) )
0 commit comments