@@ -13,8 +13,6 @@ use self::ImportDirectiveSubclass::*;
13
13
use { Module , PerNS } ;
14
14
use Namespace :: { self , TypeNS , MacroNS } ;
15
15
use { NameBinding , NameBindingKind , PathResult , PathScope , PrivacyError , ToNameBinding } ;
16
- use ResolveResult ;
17
- use ResolveResult :: * ;
18
16
use Resolver ;
19
17
use { names_to_string, module_to_string} ;
20
18
use { resolve_error, ResolutionError } ;
@@ -142,32 +140,32 @@ impl<'a> Resolver<'a> {
142
140
ns : Namespace ,
143
141
ignore_unresolved_invocations : bool ,
144
142
record_used : Option < Span > )
145
- -> ResolveResult < & ' a NameBinding < ' a > > {
143
+ -> Result < & ' a NameBinding < ' a > , Determinacy > {
146
144
self . populate_module_if_necessary ( module) ;
147
145
148
146
let resolution = self . resolution ( module, name, ns) ;
149
147
let resolution = match resolution. borrow_state ( ) {
150
148
:: std:: cell:: BorrowState :: Unused => resolution. borrow_mut ( ) ,
151
- _ => return Failed ( None ) , // This happens when there is a cycle of imports
149
+ _ => return Err ( Determined ) , // This happens when there is a cycle of imports
152
150
} ;
153
151
154
152
if let Some ( span) = record_used {
155
153
if let Some ( binding) = resolution. binding {
156
154
if self . record_use ( name, ns, binding, span) {
157
- return Success ( self . dummy_binding ) ;
155
+ return Ok ( self . dummy_binding ) ;
158
156
}
159
157
if !self . is_accessible ( binding. vis ) {
160
158
self . privacy_errors . push ( PrivacyError ( span, name, binding) ) ;
161
159
}
162
160
}
163
161
164
- return resolution. binding . map ( Success ) . unwrap_or ( Failed ( None ) ) ;
162
+ return resolution. binding . ok_or ( Determined ) ;
165
163
}
166
164
167
165
let check_usable = |this : & mut Self , binding : & ' a NameBinding < ' a > | {
168
166
// `extern crate` are always usable for backwards compatability, see issue #37020.
169
167
let usable = this. is_accessible ( binding. vis ) || binding. is_extern_crate ( ) ;
170
- if usable { Success ( binding) } else { Failed ( None ) }
168
+ if usable { Ok ( binding) } else { Err ( Determined ) }
171
169
} ;
172
170
173
171
// Items and single imports are not shadowable.
@@ -179,19 +177,19 @@ impl<'a> Resolver<'a> {
179
177
180
178
// Check if a single import can still define the name.
181
179
match resolution. single_imports {
182
- SingleImports :: AtLeastOne => return Indeterminate ,
180
+ SingleImports :: AtLeastOne => return Err ( Undetermined ) ,
183
181
SingleImports :: MaybeOne ( directive) if self . is_accessible ( directive. vis . get ( ) ) => {
184
182
let module = match directive. imported_module . get ( ) {
185
183
Some ( module) => module,
186
- None => return Indeterminate ,
184
+ None => return Err ( Undetermined ) ,
187
185
} ;
188
186
let name = match directive. subclass {
189
187
SingleImport { source, .. } => source,
190
188
_ => unreachable ! ( ) ,
191
189
} ;
192
190
match self . resolve_name_in_module ( module, name, ns, false , None ) {
193
- Failed ( _ ) => { }
194
- _ => return Indeterminate ,
191
+ Err ( Determined ) => { }
192
+ _ => return Err ( Undetermined ) ,
195
193
}
196
194
}
197
195
SingleImports :: MaybeOne ( _) | SingleImports :: None => { } ,
@@ -204,24 +202,24 @@ impl<'a> Resolver<'a> {
204
202
Some ( binding) if no_unresolved_invocations || ns == MacroNS =>
205
203
return check_usable ( self , binding) ,
206
204
None if no_unresolved_invocations => { }
207
- _ => return Indeterminate ,
205
+ _ => return Err ( Undetermined ) ,
208
206
}
209
207
210
208
// Check if the globs are determined
211
209
for directive in module. globs . borrow ( ) . iter ( ) {
212
210
if self . is_accessible ( directive. vis . get ( ) ) {
213
211
if let Some ( module) = directive. imported_module . get ( ) {
214
212
let result = self . resolve_name_in_module ( module, name, ns, false , None ) ;
215
- if let Indeterminate = result {
216
- return Indeterminate ;
213
+ if let Err ( Undetermined ) = result {
214
+ return Err ( Undetermined ) ;
217
215
}
218
216
} else {
219
- return Indeterminate ;
217
+ return Err ( Undetermined ) ;
220
218
}
221
219
}
222
220
}
223
221
224
- Failed ( None )
222
+ Err ( Determined )
225
223
}
226
224
227
225
// Add an import directive to the current module.
@@ -421,9 +419,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
421
419
prev_num_indeterminates = self . indeterminate_imports . len ( ) ;
422
420
for import in mem:: replace ( & mut self . indeterminate_imports , Vec :: new ( ) ) {
423
421
match self . resolve_import ( & import) {
424
- Failed ( _) => self . determined_imports . push ( import) ,
425
- Indeterminate => self . indeterminate_imports . push ( import) ,
426
- Success ( ( ) ) => self . determined_imports . push ( import) ,
422
+ true => self . determined_imports . push ( import) ,
423
+ false => self . indeterminate_imports . push ( import) ,
427
424
}
428
425
}
429
426
}
@@ -437,19 +434,15 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
437
434
let mut errors = false ;
438
435
for i in 0 .. self . determined_imports . len ( ) {
439
436
let import = self . determined_imports [ i] ;
440
- if let Failed ( err) = self . finalize_import ( import) {
437
+ if let Some ( err) = self . finalize_import ( import) {
441
438
errors = true ;
442
- let ( span, help) = match err {
443
- Some ( ( span, msg) ) => ( span, msg) ,
444
- None => continue ,
445
- } ;
446
439
447
440
// If the error is a single failed import then create a "fake" import
448
441
// resolution for it so that later resolve stages won't complain.
449
442
self . import_dummy_binding ( import) ;
450
443
let path = import_path_to_string ( & import. module_path , & import. subclass ) ;
451
- let error = ResolutionError :: UnresolvedImport ( Some ( ( & path, & help ) ) ) ;
452
- resolve_error ( self . resolver , span, error) ;
444
+ let error = ResolutionError :: UnresolvedImport ( Some ( ( & path, & err ) ) ) ;
445
+ resolve_error ( self . resolver , import . span , error) ;
453
446
}
454
447
}
455
448
@@ -463,12 +456,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
463
456
}
464
457
}
465
458
466
- /// Attempts to resolve the given import. The return value indicates
467
- /// failure if we're certain the name does not exist, indeterminate if we
468
- /// don't know whether the name exists at the moment due to other
469
- /// currently-unresolved imports, or success if we know the name exists.
459
+ /// Attempts to resolve the given import, returning true if its resolution is determined.
470
460
/// If successful, the resolved bindings are written into the module.
471
- fn resolve_import ( & mut self , directive : & ' b ImportDirective < ' b > ) -> ResolveResult < ( ) > {
461
+ fn resolve_import ( & mut self , directive : & ' b ImportDirective < ' b > ) -> bool {
472
462
debug ! ( "(resolving import for module) resolving import `{}::...` in `{}`" ,
473
463
names_to_string( & directive. module_path) ,
474
464
module_to_string( self . current_module) ) ;
@@ -487,8 +477,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
487
477
488
478
match result {
489
479
PathResult :: Module ( module) => module,
490
- PathResult :: Indeterminate => return Indeterminate ,
491
- _ => return Failed ( None ) ,
480
+ PathResult :: Indeterminate => return false ,
481
+ _ => return true ,
492
482
}
493
483
} ;
494
484
@@ -497,21 +487,15 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
497
487
SingleImport { source, target, ref result } => ( source, target, result) ,
498
488
GlobImport { .. } => {
499
489
self . resolve_glob_import ( directive) ;
500
- return Success ( ( ) ) ;
490
+ return true ;
501
491
}
502
492
_ => unreachable ! ( ) ,
503
493
} ;
504
494
505
495
let mut indeterminate = false ;
506
496
self . per_ns ( |this, ns| {
507
497
if let Err ( Undetermined ) = result[ ns] . get ( ) {
508
- result[ ns] . set ( {
509
- match this. resolve_name_in_module ( module, source, ns, false , None ) {
510
- Success ( binding) => Ok ( binding) ,
511
- Indeterminate => Err ( Undetermined ) ,
512
- Failed ( _) => Err ( Determined ) ,
513
- }
514
- } ) ;
498
+ result[ ns] . set ( this. resolve_name_in_module ( module, source, ns, false , None ) ) ;
515
499
} else {
516
500
return
517
501
} ;
@@ -543,37 +527,35 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
543
527
}
544
528
} ) ;
545
529
546
- if indeterminate { Indeterminate } else { Success ( ( ) ) }
530
+ ! indeterminate
547
531
}
548
532
549
- fn finalize_import ( & mut self , directive : & ' b ImportDirective < ' b > ) -> ResolveResult < ( ) > {
533
+ // If appropriate, returns an error to report.
534
+ fn finalize_import ( & mut self , directive : & ' b ImportDirective < ' b > ) -> Option < String > {
550
535
self . current_module = directive. parent ;
551
536
552
537
let ImportDirective { ref module_path, span, .. } = * directive;
553
538
let module_result = self . resolve_path ( & module_path, PathScope :: Import , None , Some ( span) ) ;
554
539
let module = match module_result {
555
540
PathResult :: Module ( module) => module,
556
- PathResult :: NonModule ( ..) => return Success ( ( ) ) ,
557
- PathResult :: Indeterminate => return Indeterminate ,
558
541
PathResult :: Failed ( msg, _) => {
559
542
let mut path = vec ! [ keywords:: SelfValue . ident( ) ] ;
560
543
path. extend ( module_path) ;
561
544
let result = self . resolve_path ( & path, PathScope :: Import , None , None ) ;
562
545
return if let PathResult :: Module ( ..) = result {
563
- let msg = format ! ( "Did you mean `self::{}`?" , & names_to_string( module_path) ) ;
564
- Failed ( Some ( ( span, msg) ) )
546
+ Some ( format ! ( "Did you mean `self::{}`?" , & names_to_string( module_path) ) )
565
547
} else {
566
- Failed ( Some ( ( span , msg) ) )
548
+ Some ( msg)
567
549
} ;
568
550
} ,
551
+ _ => return None ,
569
552
} ;
570
553
571
554
let ( name, result) = match directive. subclass {
572
555
SingleImport { source, ref result, .. } => ( source, result) ,
573
556
GlobImport { .. } if module. def_id ( ) == directive. parent . def_id ( ) => {
574
557
// Importing a module into itself is not allowed.
575
- let msg = "Cannot glob-import a module into itself." . into ( ) ;
576
- return Failed ( Some ( ( directive. span , msg) ) ) ;
558
+ return Some ( "Cannot glob-import a module into itself." . to_string ( ) ) ;
577
559
}
578
560
GlobImport { is_prelude, ref max_vis } => {
579
561
if !is_prelude &&
@@ -582,7 +564,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
582
564
let msg = "A non-empty glob must import something with the glob's visibility" ;
583
565
self . session . span_err ( directive. span , msg) ;
584
566
}
585
- return Success ( ( ) ) ;
567
+ return None ;
586
568
}
587
569
_ => unreachable ! ( ) ,
588
570
} ;
@@ -602,7 +584,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
602
584
let mut all_ns_failed = true ;
603
585
self . per_ns ( |this, ns| {
604
586
match this. resolve_name_in_module ( module, name, ns, false , Some ( span) ) {
605
- Success ( _) => all_ns_failed = false ,
587
+ Ok ( _) => all_ns_failed = false ,
606
588
_ => { }
607
589
}
608
590
} ) ;
@@ -627,11 +609,11 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
627
609
} else {
628
610
format ! ( "no `{}` in `{}`{}" , name, module_str, lev_suggestion)
629
611
} ;
630
- Failed ( Some ( ( directive . span , msg) ) )
612
+ Some ( msg)
631
613
} else {
632
614
// `resolve_name_in_module` reported a privacy error.
633
615
self . import_dummy_binding ( directive) ;
634
- Success ( ( ) )
616
+ None
635
617
}
636
618
}
637
619
@@ -680,7 +662,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
680
662
} ) ;
681
663
682
664
debug ! ( "(resolving single import) successfully resolved import" ) ;
683
- return Success ( ( ) ) ;
665
+ None
684
666
}
685
667
686
668
fn resolve_glob_import ( & mut self , directive : & ' b ImportDirective < ' b > ) {
0 commit comments