@@ -749,10 +749,6 @@ impl<'a> LexicalScopeBinding<'a> {
749
749
_ => None ,
750
750
}
751
751
}
752
-
753
- fn module ( self ) -> Option < Module < ' a > > {
754
- self . item ( ) . and_then ( NameBinding :: module)
755
- }
756
752
}
757
753
758
754
/// The link from a module up to its nearest parent node.
@@ -884,12 +880,13 @@ enum NameBindingKind<'a> {
884
880
struct PrivacyError < ' a > ( Span , Name , & ' a NameBinding < ' a > ) ;
885
881
886
882
impl < ' a > NameBinding < ' a > {
887
- fn module ( & self ) -> Option < Module < ' a > > {
883
+ fn module ( & self ) -> Result < Module < ' a > , bool /* true if an error has already been reported */ > {
888
884
match self . kind {
889
- NameBindingKind :: Module ( module) => Some ( module) ,
890
- NameBindingKind :: Def ( _) => None ,
885
+ NameBindingKind :: Module ( module) => Ok ( module) ,
891
886
NameBindingKind :: Import { binding, .. } => binding. module ( ) ,
892
- NameBindingKind :: Ambiguity { .. } => None ,
887
+ NameBindingKind :: Def ( Def :: Err ) => Err ( true ) ,
888
+ NameBindingKind :: Def ( _) => Err ( false ) ,
889
+ NameBindingKind :: Ambiguity { .. } => Err ( false ) ,
893
890
}
894
891
}
895
892
@@ -915,7 +912,7 @@ impl<'a> NameBinding<'a> {
915
912
}
916
913
917
914
fn is_extern_crate ( & self ) -> bool {
918
- self . module ( ) . and_then ( |module| module. extern_crate_id ) . is_some ( )
915
+ self . module ( ) . ok ( ) . and_then ( |module| module. extern_crate_id ) . is_some ( )
919
916
}
920
917
921
918
fn is_import ( & self ) -> bool {
@@ -1269,7 +1266,7 @@ impl<'a> Resolver<'a> {
1269
1266
fn record_use ( & mut self , name : Name , ns : Namespace , binding : & ' a NameBinding < ' a > , span : Span )
1270
1267
-> bool /* true if an error was reported */ {
1271
1268
// track extern crates for unused_extern_crate lint
1272
- if let Some ( DefId { krate, .. } ) = binding. module ( ) . and_then ( ModuleS :: def_id) {
1269
+ if let Some ( DefId { krate, .. } ) = binding. module ( ) . ok ( ) . and_then ( ModuleS :: def_id) {
1273
1270
self . used_crates . insert ( krate) ;
1274
1271
}
1275
1272
@@ -1292,6 +1289,18 @@ impl<'a> Resolver<'a> {
1292
1289
}
1293
1290
}
1294
1291
1292
+ fn expect_module ( & mut self , name : Name , binding : & ' a NameBinding < ' a > , span : Option < Span > )
1293
+ -> ResolveResult < Module < ' a > > {
1294
+ match binding. module ( ) {
1295
+ Ok ( module) => Success ( module) ,
1296
+ Err ( true ) => Failed ( None ) ,
1297
+ Err ( false ) => {
1298
+ let msg = format ! ( "Not a module `{}`" , name) ;
1299
+ Failed ( span. map ( |span| ( span, msg) ) )
1300
+ }
1301
+ }
1302
+ }
1303
+
1295
1304
/// Resolves the given module path from the given root `search_module`.
1296
1305
fn resolve_module_path_from_root ( & mut self ,
1297
1306
mut search_module : Module < ' a > ,
@@ -1357,11 +1366,9 @@ impl<'a> Resolver<'a> {
1357
1366
Success ( binding) => {
1358
1367
// Check to see whether there are type bindings, and, if
1359
1368
// so, whether there is a module within.
1360
- if let Some ( module_def) = binding. module ( ) {
1361
- search_module = module_def;
1362
- } else {
1363
- let msg = format ! ( "Not a module `{}`" , name) ;
1364
- return Failed ( span. map ( |span| ( span, msg) ) ) ;
1369
+ match self . expect_module ( name, binding, span) {
1370
+ Success ( module) => search_module = module,
1371
+ result @ _ => return result,
1365
1372
}
1366
1373
}
1367
1374
}
@@ -1414,17 +1421,20 @@ impl<'a> Resolver<'a> {
1414
1421
// first component of the path in the current lexical
1415
1422
// scope and then proceed to resolve below that.
1416
1423
let ident = ast:: Ident :: with_empty_ctxt ( module_path[ 0 ] ) ;
1417
- match self . resolve_ident_in_lexical_scope ( ident, TypeNS , span)
1418
- . and_then ( LexicalScopeBinding :: module) {
1419
- None => {
1420
- let msg =
1421
- format ! ( "Use of undeclared type or module `{}`" , ident. name) ;
1422
- return Failed ( span. map ( |span| ( span, msg) ) ) ;
1423
- }
1424
- Some ( containing_module) => {
1425
- search_module = containing_module;
1426
- start_index = 1 ;
1424
+ let lexical_binding =
1425
+ self . resolve_ident_in_lexical_scope ( ident, TypeNS , span) ;
1426
+ if let Some ( binding) = lexical_binding. and_then ( LexicalScopeBinding :: item) {
1427
+ match self . expect_module ( ident. name , binding, span) {
1428
+ Success ( containing_module) => {
1429
+ search_module = containing_module;
1430
+ start_index = 1 ;
1431
+ }
1432
+ result @ _ => return result,
1427
1433
}
1434
+ } else {
1435
+ let msg =
1436
+ format ! ( "Use of undeclared type or module `{}`" , ident. name) ;
1437
+ return Failed ( span. map ( |span| ( span, msg) ) ) ;
1428
1438
}
1429
1439
}
1430
1440
}
@@ -3202,7 +3212,7 @@ impl<'a> Resolver<'a> {
3202
3212
}
3203
3213
3204
3214
// collect submodules to explore
3205
- if let Some ( module) = name_binding. module ( ) {
3215
+ if let Ok ( module) = name_binding. module ( ) {
3206
3216
// form the path
3207
3217
let path_segments = match module. parent_link {
3208
3218
NoParentLink => path_segments. clone ( ) ,
@@ -3341,9 +3351,9 @@ impl<'a> Resolver<'a> {
3341
3351
let msg = {
3342
3352
let kind = match ( ns, old_binding. module ( ) ) {
3343
3353
( ValueNS , _) => "a value" ,
3344
- ( TypeNS , Some ( module) ) if module. extern_crate_id . is_some ( ) => "an extern crate" ,
3345
- ( TypeNS , Some ( module) ) if module. is_normal ( ) => "a module" ,
3346
- ( TypeNS , Some ( module) ) if module. is_trait ( ) => "a trait" ,
3354
+ ( TypeNS , Ok ( module) ) if module. extern_crate_id . is_some ( ) => "an extern crate" ,
3355
+ ( TypeNS , Ok ( module) ) if module. is_normal ( ) => "a module" ,
3356
+ ( TypeNS , Ok ( module) ) if module. is_trait ( ) => "a trait" ,
3347
3357
( TypeNS , _) => "a type" ,
3348
3358
} ;
3349
3359
format ! ( "{} named `{}` has already been {} in this {}" ,
0 commit comments