@@ -158,7 +158,7 @@ enum ResolutionError<'a> {
158158 /// error E0435: attempt to use a non-constant value in a constant
159159 AttemptToUseNonConstantValueInConstant ,
160160 /// error E0530: X bindings cannot shadow Ys
161- BindingShadowsSomethingUnacceptable ( & ' a str , & ' a str , Name ) ,
161+ BindingShadowsSomethingUnacceptable ( & ' a str , Name , & ' a NameBinding < ' a > ) ,
162162 /// error E0531: unresolved pattern path kind `name`
163163 PatPathUnresolved ( & ' a str , & ' a Path ) ,
164164 /// error E0532: expected pattern path kind, found another pattern path kind
@@ -428,17 +428,16 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
428428 E0435 ,
429429 "attempt to use a non-constant value in a constant" )
430430 }
431- ResolutionError :: BindingShadowsSomethingUnacceptable ( what_binding, shadows_what, name) => {
431+ ResolutionError :: BindingShadowsSomethingUnacceptable ( what_binding, name, binding) => {
432+ let shadows_what = PathResolution :: new ( binding. def ( ) . unwrap ( ) ) . kind_name ( ) ;
432433 let mut err = struct_span_err ! ( resolver. session,
433434 span,
434435 E0530 ,
435436 "{}s cannot shadow {}s" , what_binding, shadows_what) ;
436437 err. span_label ( span, & format ! ( "cannot be named the same as a {}" , shadows_what) ) ;
437- if let Success ( binding) = resolver. current_module . resolve_name ( name, ValueNS , true ) {
438- let participle = if binding. is_import ( ) { "imported" } else { "defined" } ;
439- err. span_label ( binding. span , & format ! ( "a {} `{}` is {} here" ,
440- shadows_what, name, participle) ) ;
441- }
438+ let participle = if binding. is_import ( ) { "imported" } else { "defined" } ;
439+ let msg = & format ! ( "a {} `{}` is {} here" , shadows_what, name, participle) ;
440+ err. span_label ( binding. span , msg) ;
442441 err
443442 }
444443 ResolutionError :: PatPathUnresolved ( expected_what, path) => {
@@ -718,12 +717,16 @@ impl<'a> LexicalScopeBinding<'a> {
718717 }
719718 }
720719
721- fn module ( self ) -> Option < Module < ' a > > {
720+ fn item ( self ) -> Option < & ' a NameBinding < ' a > > {
722721 match self {
723- LexicalScopeBinding :: Item ( binding) => binding . module ( ) ,
722+ LexicalScopeBinding :: Item ( binding) => Some ( binding ) ,
724723 _ => None ,
725724 }
726725 }
726+
727+ fn module ( self ) -> Option < Module < ' a > > {
728+ self . item ( ) . and_then ( NameBinding :: module)
729+ }
727730}
728731
729732/// The link from a module up to its nearest parent node.
@@ -824,6 +827,16 @@ pub struct NameBinding<'a> {
824827 vis : ty:: Visibility ,
825828}
826829
830+ pub trait ToNameBinding < ' a > {
831+ fn to_name_binding ( self ) -> NameBinding < ' a > ;
832+ }
833+
834+ impl < ' a > ToNameBinding < ' a > for NameBinding < ' a > {
835+ fn to_name_binding ( self ) -> NameBinding < ' a > {
836+ self
837+ }
838+ }
839+
827840#[ derive( Clone , Debug ) ]
828841enum NameBindingKind < ' a > {
829842 Def ( Def ) ,
@@ -1203,34 +1216,27 @@ impl<'a> Resolver<'a> {
12031216 match ns { ValueNS => & mut self . value_ribs , TypeNS => & mut self . type_ribs }
12041217 }
12051218
1206- #[ inline]
1207- fn record_use ( & mut self , name : Name , binding : & ' a NameBinding < ' a > ) {
1219+ fn record_use ( & mut self , name : Name , ns : Namespace , binding : & ' a NameBinding < ' a > ) {
12081220 // track extern crates for unused_extern_crate lint
12091221 if let Some ( DefId { krate, .. } ) = binding. module ( ) . and_then ( ModuleS :: def_id) {
12101222 self . used_crates . insert ( krate) ;
12111223 }
12121224
1213- let directive = match binding. kind {
1214- NameBindingKind :: Import { directive, .. } => directive,
1215- _ => return ,
1216- } ;
1217-
1218- if !self . make_glob_map {
1219- return ;
1220- }
1221- if self . glob_map . contains_key ( & directive. id ) {
1222- self . glob_map . get_mut ( & directive. id ) . unwrap ( ) . insert ( name) ;
1223- return ;
1225+ if let NameBindingKind :: Import { directive, .. } = binding. kind {
1226+ self . used_imports . insert ( ( directive. id , ns) ) ;
1227+ self . add_to_glob_map ( directive. id , name) ;
12241228 }
1229+ }
12251230
1226- let mut new_set = FnvHashSet ( ) ;
1227- new_set. insert ( name) ;
1228- self . glob_map . insert ( directive. id , new_set) ;
1231+ fn add_to_glob_map ( & mut self , id : NodeId , name : Name ) {
1232+ if self . make_glob_map {
1233+ self . glob_map . entry ( id) . or_insert_with ( FnvHashSet ) . insert ( name) ;
1234+ }
12291235 }
12301236
1231- /// Resolves the given module path from the given root `module_ `.
1237+ /// Resolves the given module path from the given root `search_module `.
12321238 fn resolve_module_path_from_root ( & mut self ,
1233- module_ : Module < ' a > ,
1239+ mut search_module : Module < ' a > ,
12341240 module_path : & [ Name ] ,
12351241 index : usize ,
12361242 span : Span )
@@ -1247,7 +1253,6 @@ impl<'a> Resolver<'a> {
12471253 }
12481254 }
12491255
1250- let mut search_module = module_;
12511256 let mut index = index;
12521257 let module_path_len = module_path. len ( ) ;
12531258
@@ -1444,31 +1449,30 @@ impl<'a> Resolver<'a> {
14441449 }
14451450
14461451 /// Returns the nearest normal module parent of the given module.
1447- fn get_nearest_normal_module_parent ( & self , module_ : Module < ' a > ) -> Option < Module < ' a > > {
1448- let mut module_ = module_;
1452+ fn get_nearest_normal_module_parent ( & self , mut module : Module < ' a > ) -> Option < Module < ' a > > {
14491453 loop {
1450- match module_ . parent_link {
1454+ match module . parent_link {
14511455 NoParentLink => return None ,
14521456 ModuleParentLink ( new_module, _) |
14531457 BlockParentLink ( new_module, _) => {
14541458 let new_module = new_module;
14551459 if new_module. is_normal ( ) {
14561460 return Some ( new_module) ;
14571461 }
1458- module_ = new_module;
1462+ module = new_module;
14591463 }
14601464 }
14611465 }
14621466 }
14631467
14641468 /// Returns the nearest normal module parent of the given module, or the
14651469 /// module itself if it is a normal module.
1466- fn get_nearest_normal_module_parent_or_self ( & self , module_ : Module < ' a > ) -> Module < ' a > {
1467- if module_ . is_normal ( ) {
1468- return module_ ;
1470+ fn get_nearest_normal_module_parent_or_self ( & self , module : Module < ' a > ) -> Module < ' a > {
1471+ if module . is_normal ( ) {
1472+ return module ;
14691473 }
1470- match self . get_nearest_normal_module_parent ( module_ ) {
1471- None => module_ ,
1474+ match self . get_nearest_normal_module_parent ( module ) {
1475+ None => module ,
14721476 Some ( new_module) => new_module,
14731477 }
14741478 }
@@ -1485,8 +1489,8 @@ impl<'a> Resolver<'a> {
14851489 "super" => 0 ,
14861490 _ => return Success ( NoPrefixFound ) ,
14871491 } ;
1488- let module_ = self . current_module ;
1489- let mut containing_module = self . get_nearest_normal_module_parent_or_self ( module_ ) ;
1492+ let mut containing_module =
1493+ self . get_nearest_normal_module_parent_or_self ( self . current_module ) ;
14901494
14911495 // Now loop through all the `super`s we find.
14921496 while i < module_path. len ( ) && "super" == module_path[ i] . as_str ( ) {
@@ -1525,10 +1529,7 @@ impl<'a> Resolver<'a> {
15251529 self . populate_module_if_necessary ( module) ;
15261530 module. resolve_name ( name, namespace, use_lexical_scope) . and_then ( |binding| {
15271531 if record_used {
1528- if let NameBindingKind :: Import { directive, .. } = binding. kind {
1529- self . used_imports . insert ( ( directive. id , namespace) ) ;
1530- }
1531- self . record_use ( name, binding) ;
1532+ self . record_use ( name, namespace, binding) ;
15321533 }
15331534 Success ( binding)
15341535 } )
@@ -2308,16 +2309,17 @@ impl<'a> Resolver<'a> {
23082309 PatKind :: Ident ( bmode, ref ident, ref opt_pat) => {
23092310 // First try to resolve the identifier as some existing
23102311 // entity, then fall back to a fresh binding.
2311- let resolution = self . resolve_identifier ( ident. node , ValueNS , true )
2312- . map ( |local_def| PathResolution :: new ( local_def . def ) )
2313- . and_then ( |resolution | {
2312+ let binding = self . resolve_ident_in_lexical_scope ( ident. node , ValueNS , false )
2313+ . and_then ( LexicalScopeBinding :: item ) ;
2314+ let resolution = binding . and_then ( NameBinding :: def ) . and_then ( |def | {
23142315 let always_binding = !pat_src. is_refutable ( ) || opt_pat. is_some ( ) ||
23152316 bmode != BindingMode :: ByValue ( Mutability :: Immutable ) ;
2316- match resolution . base_def {
2317+ match def {
23172318 Def :: Struct ( ..) | Def :: Variant ( ..) |
23182319 Def :: Const ( ..) | Def :: AssociatedConst ( ..) if !always_binding => {
23192320 // A constant, unit variant, etc pattern.
2320- Some ( resolution)
2321+ self . record_use ( ident. node . name , ValueNS , binding. unwrap ( ) ) ;
2322+ Some ( PathResolution :: new ( def) )
23212323 }
23222324 Def :: Struct ( ..) | Def :: Variant ( ..) |
23232325 Def :: Const ( ..) | Def :: AssociatedConst ( ..) | Def :: Static ( ..) => {
@@ -2326,7 +2328,7 @@ impl<'a> Resolver<'a> {
23262328 self ,
23272329 ident. span ,
23282330 ResolutionError :: BindingShadowsSomethingUnacceptable (
2329- pat_src. descr ( ) , resolution . kind_name ( ) , ident. node . name )
2331+ pat_src. descr ( ) , ident. node . name , binding . unwrap ( ) )
23302332 ) ;
23312333 None
23322334 }
@@ -3136,10 +3138,10 @@ impl<'a> Resolver<'a> {
31363138 if let NameBindingKind :: Import { directive, .. } = binding. kind {
31373139 let id = directive. id ;
31383140 this. maybe_unused_trait_imports . insert ( id) ;
3141+ this. add_to_glob_map ( id, trait_name) ;
31393142 import_id = Some ( id) ;
31403143 }
31413144 add_trait_info ( & mut found_traits, trait_def_id, import_id, name) ;
3142- this. record_use ( trait_name, binding) ;
31433145 }
31443146 }
31453147 } ;
0 commit comments