@@ -84,6 +84,7 @@ use syntax::ast::{self, Name};
8484use syntax_pos:: Span ;
8585
8686use std:: fmt;
87+ use std:: hash:: { Hash , Hasher } ;
8788use rustc_data_structures:: sync:: Lrc ;
8889use std:: rc:: Rc ;
8990use util:: nodemap:: ItemLocalSet ;
@@ -132,9 +133,22 @@ pub enum InteriorKind {
132133 InteriorElement ( InteriorOffsetKind ) ,
133134}
134135
135- // FIXME: Use actual index instead of `ast::Name` with questionable hygiene
136- #[ derive( Clone , Copy , PartialEq , Eq , Hash ) ]
137- pub struct FieldIndex ( pub ast:: Name ) ;
136+ // Contains index of a field that is actually used for loan path comparisons and
137+ // string representation of the field that should be used only for diagnostics.
138+ #[ derive( Clone , Copy , Eq ) ]
139+ pub struct FieldIndex ( pub usize , pub Name ) ;
140+
141+ impl PartialEq for FieldIndex {
142+ fn eq ( & self , rhs : & Self ) -> bool {
143+ self . 0 == rhs. 0
144+ }
145+ }
146+
147+ impl Hash for FieldIndex {
148+ fn hash < H : Hasher > ( & self , h : & mut H ) {
149+ self . 0 . hash ( h)
150+ }
151+ }
138152
139153#[ derive( Clone , Copy , PartialEq , Eq , Hash , Debug ) ]
140154pub enum InteriorOffsetKind {
@@ -195,7 +209,7 @@ pub enum ImmutabilityBlame<'tcx> {
195209}
196210
197211impl < ' tcx > cmt_ < ' tcx > {
198- fn resolve_field ( & self , field_name : Name ) -> Option < ( & ' tcx ty:: AdtDef , & ' tcx ty:: FieldDef ) >
212+ fn resolve_field ( & self , field_index : usize ) -> Option < ( & ' tcx ty:: AdtDef , & ' tcx ty:: FieldDef ) >
199213 {
200214 let adt_def = match self . ty . sty {
201215 ty:: TyAdt ( def, _) => def,
@@ -212,7 +226,7 @@ impl<'tcx> cmt_<'tcx> {
212226 & adt_def. variants [ 0 ]
213227 }
214228 } ;
215- Some ( ( adt_def, variant_def. field_named ( field_name ) ) )
229+ Some ( ( adt_def, & variant_def. fields [ field_index ] ) )
216230 }
217231
218232 pub fn immutability_blame ( & self ) -> Option < ImmutabilityBlame < ' tcx > > {
@@ -639,7 +653,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
639653 expr. id,
640654 expr,
641655 base_cmt) ;
642- Ok ( self . cat_field ( expr, base_cmt, f_name. node , expr_ty) )
656+ let f_index = self . tcx . field_index ( expr. id , self . tables ) ;
657+ Ok ( self . cat_field ( expr, base_cmt, f_index, f_name. node , expr_ty) )
643658 }
644659
645660 hir:: ExprIndex ( ref base, _) => {
@@ -967,14 +982,15 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
967982 pub fn cat_field < N : ast_node > ( & self ,
968983 node : & N ,
969984 base_cmt : cmt < ' tcx > ,
985+ f_index : usize ,
970986 f_name : Name ,
971987 f_ty : Ty < ' tcx > )
972988 -> cmt < ' tcx > {
973989 let ret = Rc :: new ( cmt_ {
974990 id : node. id ( ) ,
975991 span : node. span ( ) ,
976992 mutbl : base_cmt. mutbl . inherit ( ) ,
977- cat : Categorization :: Interior ( base_cmt, InteriorField ( FieldIndex ( f_name) ) ) ,
993+ cat : Categorization :: Interior ( base_cmt, InteriorField ( FieldIndex ( f_index , f_name) ) ) ,
978994 ty : f_ty,
979995 note : NoteNone
980996 } ) ;
@@ -1262,7 +1278,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
12621278
12631279 for ( i, subpat) in subpats. iter ( ) . enumerate_and_adjust ( expected_len, ddpos) {
12641280 let subpat_ty = self . pat_ty ( & subpat) ?; // see (*2)
1265- let interior = InteriorField ( FieldIndex ( Name :: intern ( & i. to_string ( ) ) ) ) ;
1281+ let interior = InteriorField ( FieldIndex ( i , Name :: intern ( & i. to_string ( ) ) ) ) ;
12661282 let subcmt = self . cat_imm_interior ( pat, cmt. clone ( ) , subpat_ty, interior) ;
12671283 self . cat_pattern_ ( subcmt, & subpat, op) ?;
12681284 }
@@ -1285,7 +1301,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
12851301
12861302 for fp in field_pats {
12871303 let field_ty = self . pat_ty ( & fp. node . pat ) ?; // see (*2)
1288- let cmt_field = self . cat_field ( pat, cmt. clone ( ) , fp. node . name , field_ty) ;
1304+ let f_index = self . tcx . field_index ( fp. node . id , self . tables ) ;
1305+ let cmt_field = self . cat_field ( pat, cmt. clone ( ) , f_index, fp. node . name , field_ty) ;
12891306 self . cat_pattern_ ( cmt_field, & fp. node . pat , op) ?;
12901307 }
12911308 }
@@ -1302,7 +1319,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13021319 } ;
13031320 for ( i, subpat) in subpats. iter ( ) . enumerate_and_adjust ( expected_len, ddpos) {
13041321 let subpat_ty = self . pat_ty ( & subpat) ?; // see (*2)
1305- let interior = InteriorField ( FieldIndex ( Name :: intern ( & i. to_string ( ) ) ) ) ;
1322+ let interior = InteriorField ( FieldIndex ( i , Name :: intern ( & i. to_string ( ) ) ) ) ;
13061323 let subcmt = self . cat_imm_interior ( pat, cmt. clone ( ) , subpat_ty, interior) ;
13071324 self . cat_pattern_ ( subcmt, & subpat, op) ?;
13081325 }
@@ -1521,7 +1538,7 @@ pub fn ptr_sigil(ptr: PointerKind) -> &'static str {
15211538impl fmt:: Debug for InteriorKind {
15221539 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
15231540 match * self {
1524- InteriorField ( FieldIndex ( name ) ) => write ! ( f, "{}" , name ) ,
1541+ InteriorField ( FieldIndex ( _ , info ) ) => write ! ( f, "{}" , info ) ,
15251542 InteriorElement ( ..) => write ! ( f, "[]" ) ,
15261543 }
15271544 }
0 commit comments