Skip to content

Commit e9e178a

Browse files
committed
Refactor away ResolveResult.
1 parent af2d89c commit e9e178a

File tree

4 files changed

+45
-86
lines changed

4 files changed

+45
-86
lines changed

src/librustc_resolve/build_reduced_graph.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
1919
use {Resolver, Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
2020
use Namespace::{self, TypeNS, ValueNS, MacroNS};
21-
use ResolveResult::Success;
2221
use {resolve_error, resolve_struct_error, ResolutionError};
2322

2423
use rustc::middle::cstore::LoadedMacro;
@@ -583,7 +582,7 @@ impl<'b> Resolver<'b> {
583582
} else {
584583
for (name, span) in legacy_imports.imports {
585584
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
586-
if let Success(binding) = result {
585+
if let Ok(binding) = result {
587586
self.legacy_import_macro(name, binding, span, allow_shadowing);
588587
} else {
589588
span_err!(self.session, span, E0469, "imported macro not found");
@@ -595,7 +594,7 @@ impl<'b> Resolver<'b> {
595594
self.used_crates.insert(krate);
596595
self.session.cstore.export_macros(krate);
597596
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
598-
if let Success(binding) = result {
597+
if let Ok(binding) = result {
599598
self.macro_exports.push(Export { name: name, def: binding.def() });
600599
} else {
601600
span_err!(self.session, span, E0470, "reexported macro not found");

src/librustc_resolve/lib.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern crate arena;
3535
extern crate rustc;
3636

3737
use self::Namespace::*;
38-
use self::ResolveResult::*;
3938
use self::FallbackSuggestion::*;
4039
use self::TypeParameters::*;
4140
use self::RibKind::*;
@@ -668,22 +667,6 @@ impl<'a> Visitor for Resolver<'a> {
668667

669668
pub type ErrorMessage = Option<(Span, String)>;
670669

671-
#[derive(Clone, PartialEq, Eq, Debug)]
672-
pub enum ResolveResult<T> {
673-
Failed(ErrorMessage), // Failed to resolve the name, optional helpful error message.
674-
Indeterminate, // Couldn't determine due to unresolved globs.
675-
Success(T), // Successfully resolved the import.
676-
}
677-
678-
impl<T> ResolveResult<T> {
679-
fn success(self) -> Option<T> {
680-
match self {
681-
Success(t) => Some(t),
682-
_ => None,
683-
}
684-
}
685-
}
686-
687670
enum FallbackSuggestion {
688671
NoSuggestion,
689672
Field,
@@ -1417,15 +1400,15 @@ impl<'a> Resolver<'a> {
14171400
if let ModuleRibKind(module) = self.ribs[ns][i].kind {
14181401
let name = ident.name;
14191402
let item = self.resolve_name_in_module(module, name, ns, false, record_used);
1420-
if let Success(binding) = item {
1403+
if let Ok(binding) = item {
14211404
// The ident resolves to an item.
14221405
return Some(LexicalScopeBinding::Item(binding));
14231406
}
14241407

14251408
if let ModuleKind::Block(..) = module.kind { // We can see through blocks
14261409
} else if !module.no_implicit_prelude {
14271410
return self.prelude.and_then(|prelude| {
1428-
self.resolve_name_in_module(prelude, name, ns, false, None).success()
1411+
self.resolve_name_in_module(prelude, name, ns, false, None).ok()
14291412
}).map(LexicalScopeBinding::Item)
14301413
} else {
14311414
return None;
@@ -2398,11 +2381,7 @@ impl<'a> Resolver<'a> {
23982381
allow_super = false;
23992382

24002383
let binding = if let Some(module) = module {
2401-
match self.resolve_name_in_module(module, ident.name, ns, false, record_used) {
2402-
Success(binding) => Ok(binding),
2403-
Indeterminate => Err(Undetermined),
2404-
Failed(_) => Err(Determined),
2405-
}
2384+
self.resolve_name_in_module(module, ident.name, ns, false, record_used)
24062385
} else {
24072386
match self.resolve_ident_in_lexical_scope(ident, ns, record_used) {
24082387
Some(LexicalScopeBinding::Item(binding)) => Ok(binding),

src/librustc_resolve/macros.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use {Module, ModuleKind, NameBinding, NameBindingKind, Resolver, AmbiguityError};
1212
use Namespace::{self, MacroNS};
13-
use ResolveResult::{Success, Indeterminate, Failed};
1413
use build_reduced_graph::BuildReducedGraphVisitor;
1514
use resolve_imports::ImportResolver;
1615
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex};
@@ -254,7 +253,7 @@ impl<'a> Resolver<'a> {
254253
// Since expanded macros may not shadow the lexical scope (enforced below),
255254
// we can ignore unresolved invocations (indicated by the penultimate argument).
256255
match self.resolve_name_in_module(module, name, ns, true, record_used) {
257-
Success(binding) => {
256+
Ok(binding) => {
258257
let span = match record_used {
259258
Some(span) => span,
260259
None => return Some(binding),
@@ -270,8 +269,8 @@ impl<'a> Resolver<'a> {
270269
potential_expanded_shadower = Some(binding);
271270
}
272271
},
273-
Indeterminate => return None,
274-
Failed(..) => {}
272+
Err(Determinacy::Undetermined) => return None,
273+
Err(Determinacy::Determined) => {}
275274
}
276275

277276
match module.kind {

src/librustc_resolve/resolve_imports.rs

+37-55
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use self::ImportDirectiveSubclass::*;
1313
use {Module, PerNS};
1414
use Namespace::{self, TypeNS, MacroNS};
1515
use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError, ToNameBinding};
16-
use ResolveResult;
17-
use ResolveResult::*;
1816
use Resolver;
1917
use {names_to_string, module_to_string};
2018
use {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

Comments
 (0)