@@ -13,8 +13,6 @@ use self::ImportDirectiveSubclass::*;
1313use { Module , PerNS } ;
1414use Namespace :: { self , TypeNS , MacroNS } ;
1515use { NameBinding , NameBindingKind , PathResult , PathScope , PrivacyError , ToNameBinding } ;
16- use ResolveResult ;
17- use ResolveResult :: * ;
1816use Resolver ;
1917use { names_to_string, module_to_string} ;
2018use { resolve_error, ResolutionError } ;
@@ -142,32 +140,32 @@ impl<'a> Resolver<'a> {
142140 ns : Namespace ,
143141 ignore_unresolved_invocations : bool ,
144142 record_used : Option < Span > )
145- -> ResolveResult < & ' a NameBinding < ' a > > {
143+ -> Result < & ' a NameBinding < ' a > , Determinacy > {
146144 self . populate_module_if_necessary ( module) ;
147145
148146 let resolution = self . resolution ( module, name, ns) ;
149147 let resolution = match resolution. borrow_state ( ) {
150148 :: 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
152150 } ;
153151
154152 if let Some ( span) = record_used {
155153 if let Some ( binding) = resolution. binding {
156154 if self . record_use ( name, ns, binding, span) {
157- return Success ( self . dummy_binding ) ;
155+ return Ok ( self . dummy_binding ) ;
158156 }
159157 if !self . is_accessible ( binding. vis ) {
160158 self . privacy_errors . push ( PrivacyError ( span, name, binding) ) ;
161159 }
162160 }
163161
164- return resolution. binding . map ( Success ) . unwrap_or ( Failed ( None ) ) ;
162+ return resolution. binding . ok_or ( Determined ) ;
165163 }
166164
167165 let check_usable = |this : & mut Self , binding : & ' a NameBinding < ' a > | {
168166 // `extern crate` are always usable for backwards compatability, see issue #37020.
169167 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 ) }
171169 } ;
172170
173171 // Items and single imports are not shadowable.
@@ -179,19 +177,19 @@ impl<'a> Resolver<'a> {
179177
180178 // Check if a single import can still define the name.
181179 match resolution. single_imports {
182- SingleImports :: AtLeastOne => return Indeterminate ,
180+ SingleImports :: AtLeastOne => return Err ( Undetermined ) ,
183181 SingleImports :: MaybeOne ( directive) if self . is_accessible ( directive. vis . get ( ) ) => {
184182 let module = match directive. imported_module . get ( ) {
185183 Some ( module) => module,
186- None => return Indeterminate ,
184+ None => return Err ( Undetermined ) ,
187185 } ;
188186 let name = match directive. subclass {
189187 SingleImport { source, .. } => source,
190188 _ => unreachable ! ( ) ,
191189 } ;
192190 match self . resolve_name_in_module ( module, name, ns, false , None ) {
193- Failed ( _ ) => { }
194- _ => return Indeterminate ,
191+ Err ( Determined ) => { }
192+ _ => return Err ( Undetermined ) ,
195193 }
196194 }
197195 SingleImports :: MaybeOne ( _) | SingleImports :: None => { } ,
@@ -204,24 +202,24 @@ impl<'a> Resolver<'a> {
204202 Some ( binding) if no_unresolved_invocations || ns == MacroNS =>
205203 return check_usable ( self , binding) ,
206204 None if no_unresolved_invocations => { }
207- _ => return Indeterminate ,
205+ _ => return Err ( Undetermined ) ,
208206 }
209207
210208 // Check if the globs are determined
211209 for directive in module. globs . borrow ( ) . iter ( ) {
212210 if self . is_accessible ( directive. vis . get ( ) ) {
213211 if let Some ( module) = directive. imported_module . get ( ) {
214212 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 ) ;
217215 }
218216 } else {
219- return Indeterminate ;
217+ return Err ( Undetermined ) ;
220218 }
221219 }
222220 }
223221
224- Failed ( None )
222+ Err ( Determined )
225223 }
226224
227225 // Add an import directive to the current module.
@@ -421,9 +419,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
421419 prev_num_indeterminates = self . indeterminate_imports . len ( ) ;
422420 for import in mem:: replace ( & mut self . indeterminate_imports , Vec :: new ( ) ) {
423421 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) ,
427424 }
428425 }
429426 }
@@ -437,19 +434,15 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
437434 let mut errors = false ;
438435 for i in 0 .. self . determined_imports . len ( ) {
439436 let import = self . determined_imports [ i] ;
440- if let Failed ( err) = self . finalize_import ( import) {
437+ if let Some ( err) = self . finalize_import ( import) {
441438 errors = true ;
442- let ( span, help) = match err {
443- Some ( ( span, msg) ) => ( span, msg) ,
444- None => continue ,
445- } ;
446439
447440 // If the error is a single failed import then create a "fake" import
448441 // resolution for it so that later resolve stages won't complain.
449442 self . import_dummy_binding ( import) ;
450443 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) ;
453446 }
454447 }
455448
@@ -463,12 +456,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
463456 }
464457 }
465458
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.
470460 /// 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 {
472462 debug ! ( "(resolving import for module) resolving import `{}::...` in `{}`" ,
473463 names_to_string( & directive. module_path) ,
474464 module_to_string( self . current_module) ) ;
@@ -487,8 +477,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
487477
488478 match result {
489479 PathResult :: Module ( module) => module,
490- PathResult :: Indeterminate => return Indeterminate ,
491- _ => return Failed ( None ) ,
480+ PathResult :: Indeterminate => return false ,
481+ _ => return true ,
492482 }
493483 } ;
494484
@@ -497,21 +487,15 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
497487 SingleImport { source, target, ref result } => ( source, target, result) ,
498488 GlobImport { .. } => {
499489 self . resolve_glob_import ( directive) ;
500- return Success ( ( ) ) ;
490+ return true ;
501491 }
502492 _ => unreachable ! ( ) ,
503493 } ;
504494
505495 let mut indeterminate = false ;
506496 self . per_ns ( |this, ns| {
507497 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 ) ) ;
515499 } else {
516500 return
517501 } ;
@@ -543,37 +527,35 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
543527 }
544528 } ) ;
545529
546- if indeterminate { Indeterminate } else { Success ( ( ) ) }
530+ ! indeterminate
547531 }
548532
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 > {
550535 self . current_module = directive. parent ;
551536
552537 let ImportDirective { ref module_path, span, .. } = * directive;
553538 let module_result = self . resolve_path ( & module_path, PathScope :: Import , None , Some ( span) ) ;
554539 let module = match module_result {
555540 PathResult :: Module ( module) => module,
556- PathResult :: NonModule ( ..) => return Success ( ( ) ) ,
557- PathResult :: Indeterminate => return Indeterminate ,
558541 PathResult :: Failed ( msg, _) => {
559542 let mut path = vec ! [ keywords:: SelfValue . ident( ) ] ;
560543 path. extend ( module_path) ;
561544 let result = self . resolve_path ( & path, PathScope :: Import , None , None ) ;
562545 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) ) )
565547 } else {
566- Failed ( Some ( ( span , msg) ) )
548+ Some ( msg)
567549 } ;
568550 } ,
551+ _ => return None ,
569552 } ;
570553
571554 let ( name, result) = match directive. subclass {
572555 SingleImport { source, ref result, .. } => ( source, result) ,
573556 GlobImport { .. } if module. def_id ( ) == directive. parent . def_id ( ) => {
574557 // 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 ( ) ) ;
577559 }
578560 GlobImport { is_prelude, ref max_vis } => {
579561 if !is_prelude &&
@@ -582,7 +564,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
582564 let msg = "A non-empty glob must import something with the glob's visibility" ;
583565 self . session . span_err ( directive. span , msg) ;
584566 }
585- return Success ( ( ) ) ;
567+ return None ;
586568 }
587569 _ => unreachable ! ( ) ,
588570 } ;
@@ -602,7 +584,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
602584 let mut all_ns_failed = true ;
603585 self . per_ns ( |this, ns| {
604586 match this. resolve_name_in_module ( module, name, ns, false , Some ( span) ) {
605- Success ( _) => all_ns_failed = false ,
587+ Ok ( _) => all_ns_failed = false ,
606588 _ => { }
607589 }
608590 } ) ;
@@ -627,11 +609,11 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
627609 } else {
628610 format ! ( "no `{}` in `{}`{}" , name, module_str, lev_suggestion)
629611 } ;
630- Failed ( Some ( ( directive . span , msg) ) )
612+ Some ( msg)
631613 } else {
632614 // `resolve_name_in_module` reported a privacy error.
633615 self . import_dummy_binding ( directive) ;
634- Success ( ( ) )
616+ None
635617 }
636618 }
637619
@@ -680,7 +662,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
680662 } ) ;
681663
682664 debug ! ( "(resolving single import) successfully resolved import" ) ;
683- return Success ( ( ) ) ;
665+ None
684666 }
685667
686668 fn resolve_glob_import ( & mut self , directive : & ' b ImportDirective < ' b > ) {
0 commit comments