@@ -158,7 +158,7 @@ enum ResolutionError<'a> {
158
158
/// error E0435: attempt to use a non-constant value in a constant
159
159
AttemptToUseNonConstantValueInConstant ,
160
160
/// error E0530: X bindings cannot shadow Ys
161
- BindingShadowsSomethingUnacceptable ( & ' a str , & ' a str , Name ) ,
161
+ BindingShadowsSomethingUnacceptable ( & ' a str , Name , & ' a NameBinding < ' a > ) ,
162
162
/// error E0531: unresolved pattern path kind `name`
163
163
PatPathUnresolved ( & ' a str , & ' a Path ) ,
164
164
/// 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>,
428
428
E0435 ,
429
429
"attempt to use a non-constant value in a constant" )
430
430
}
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 ( ) ;
432
433
let mut err = struct_span_err ! ( resolver. session,
433
434
span,
434
435
E0530 ,
435
436
"{}s cannot shadow {}s" , what_binding, shadows_what) ;
436
437
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) ;
442
441
err
443
442
}
444
443
ResolutionError :: PatPathUnresolved ( expected_what, path) => {
@@ -718,12 +717,16 @@ impl<'a> LexicalScopeBinding<'a> {
718
717
}
719
718
}
720
719
721
- fn module ( self ) -> Option < Module < ' a > > {
720
+ fn item ( self ) -> Option < & ' a NameBinding < ' a > > {
722
721
match self {
723
- LexicalScopeBinding :: Item ( binding) => binding . module ( ) ,
722
+ LexicalScopeBinding :: Item ( binding) => Some ( binding ) ,
724
723
_ => None ,
725
724
}
726
725
}
726
+
727
+ fn module ( self ) -> Option < Module < ' a > > {
728
+ self . item ( ) . and_then ( NameBinding :: module)
729
+ }
727
730
}
728
731
729
732
/// The link from a module up to its nearest parent node.
@@ -824,6 +827,16 @@ pub struct NameBinding<'a> {
824
827
vis : ty:: Visibility ,
825
828
}
826
829
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
+
827
840
#[ derive( Clone , Debug ) ]
828
841
enum NameBindingKind < ' a > {
829
842
Def ( Def ) ,
@@ -1203,34 +1216,27 @@ impl<'a> Resolver<'a> {
1203
1216
match ns { ValueNS => & mut self . value_ribs , TypeNS => & mut self . type_ribs }
1204
1217
}
1205
1218
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 > ) {
1208
1220
// track extern crates for unused_extern_crate lint
1209
1221
if let Some ( DefId { krate, .. } ) = binding. module ( ) . and_then ( ModuleS :: def_id) {
1210
1222
self . used_crates . insert ( krate) ;
1211
1223
}
1212
1224
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) ;
1224
1228
}
1229
+ }
1225
1230
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
+ }
1229
1235
}
1230
1236
1231
- /// Resolves the given module path from the given root `module_ `.
1237
+ /// Resolves the given module path from the given root `search_module `.
1232
1238
fn resolve_module_path_from_root ( & mut self ,
1233
- module_ : Module < ' a > ,
1239
+ mut search_module : Module < ' a > ,
1234
1240
module_path : & [ Name ] ,
1235
1241
index : usize ,
1236
1242
span : Span )
@@ -1247,7 +1253,6 @@ impl<'a> Resolver<'a> {
1247
1253
}
1248
1254
}
1249
1255
1250
- let mut search_module = module_;
1251
1256
let mut index = index;
1252
1257
let module_path_len = module_path. len ( ) ;
1253
1258
@@ -1444,31 +1449,30 @@ impl<'a> Resolver<'a> {
1444
1449
}
1445
1450
1446
1451
/// 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 > > {
1449
1453
loop {
1450
- match module_ . parent_link {
1454
+ match module . parent_link {
1451
1455
NoParentLink => return None ,
1452
1456
ModuleParentLink ( new_module, _) |
1453
1457
BlockParentLink ( new_module, _) => {
1454
1458
let new_module = new_module;
1455
1459
if new_module. is_normal ( ) {
1456
1460
return Some ( new_module) ;
1457
1461
}
1458
- module_ = new_module;
1462
+ module = new_module;
1459
1463
}
1460
1464
}
1461
1465
}
1462
1466
}
1463
1467
1464
1468
/// Returns the nearest normal module parent of the given module, or the
1465
1469
/// 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 ;
1469
1473
}
1470
- match self . get_nearest_normal_module_parent ( module_ ) {
1471
- None => module_ ,
1474
+ match self . get_nearest_normal_module_parent ( module ) {
1475
+ None => module ,
1472
1476
Some ( new_module) => new_module,
1473
1477
}
1474
1478
}
@@ -1485,8 +1489,8 @@ impl<'a> Resolver<'a> {
1485
1489
"super" => 0 ,
1486
1490
_ => return Success ( NoPrefixFound ) ,
1487
1491
} ;
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 ) ;
1490
1494
1491
1495
// Now loop through all the `super`s we find.
1492
1496
while i < module_path. len ( ) && "super" == module_path[ i] . as_str ( ) {
@@ -1525,10 +1529,7 @@ impl<'a> Resolver<'a> {
1525
1529
self . populate_module_if_necessary ( module) ;
1526
1530
module. resolve_name ( name, namespace, use_lexical_scope) . and_then ( |binding| {
1527
1531
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) ;
1532
1533
}
1533
1534
Success ( binding)
1534
1535
} )
@@ -2308,16 +2309,17 @@ impl<'a> Resolver<'a> {
2308
2309
PatKind :: Ident ( bmode, ref ident, ref opt_pat) => {
2309
2310
// First try to resolve the identifier as some existing
2310
2311
// 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 | {
2314
2315
let always_binding = !pat_src. is_refutable ( ) || opt_pat. is_some ( ) ||
2315
2316
bmode != BindingMode :: ByValue ( Mutability :: Immutable ) ;
2316
- match resolution . base_def {
2317
+ match def {
2317
2318
Def :: Struct ( ..) | Def :: Variant ( ..) |
2318
2319
Def :: Const ( ..) | Def :: AssociatedConst ( ..) if !always_binding => {
2319
2320
// A constant, unit variant, etc pattern.
2320
- Some ( resolution)
2321
+ self . record_use ( ident. node . name , ValueNS , binding. unwrap ( ) ) ;
2322
+ Some ( PathResolution :: new ( def) )
2321
2323
}
2322
2324
Def :: Struct ( ..) | Def :: Variant ( ..) |
2323
2325
Def :: Const ( ..) | Def :: AssociatedConst ( ..) | Def :: Static ( ..) => {
@@ -2326,7 +2328,7 @@ impl<'a> Resolver<'a> {
2326
2328
self ,
2327
2329
ident. span ,
2328
2330
ResolutionError :: BindingShadowsSomethingUnacceptable (
2329
- pat_src. descr ( ) , resolution . kind_name ( ) , ident. node . name )
2331
+ pat_src. descr ( ) , ident. node . name , binding . unwrap ( ) )
2330
2332
) ;
2331
2333
None
2332
2334
}
@@ -3136,10 +3138,10 @@ impl<'a> Resolver<'a> {
3136
3138
if let NameBindingKind :: Import { directive, .. } = binding. kind {
3137
3139
let id = directive. id ;
3138
3140
this. maybe_unused_trait_imports . insert ( id) ;
3141
+ this. add_to_glob_map ( id, trait_name) ;
3139
3142
import_id = Some ( id) ;
3140
3143
}
3141
3144
add_trait_info ( & mut found_traits, trait_def_id, import_id, name) ;
3142
- this. record_use ( trait_name, binding) ;
3143
3145
}
3144
3146
}
3145
3147
} ;
0 commit comments