Resolution - PathResolver
Return the segment which cannot be resolved, when we resolve paths. The API is quite ugly because we have Option and Result. The option will be later replaced with PerNs in the future; see comment
use super::import::{resolve_path_to_ns, ImportDirective, PathResolution};
-use crate::Path;
+use crate::{Ident, Path};
use std::collections::HashMap;
use crate::graph::CrateId;
use crate::hir::def_map::{CrateDefMap, ModuleDefId, ModuleId};
pub trait PathResolver {
- fn resolve(&self, def_maps: &HashMap<CrateId, CrateDefMap>, path: Path) -> Option<ModuleDefId>;
+ fn resolve(
+ &self,
+ def_maps: &HashMap<CrateId, CrateDefMap>,
+ path: Path,
+ ) -> Result<Option<ModuleDefId>, Ident>;
}
pub struct FunctionPathResolver {
@@ -21,7 +25,11 @@ impl FunctionPathResolver {
}
impl PathResolver for FunctionPathResolver {
- fn resolve(&self, def_maps: &HashMap<CrateId, CrateDefMap>, path: Path) -> Option<ModuleDefId> {
+ fn resolve(
+ &self,
+ def_maps: &HashMap<CrateId, CrateDefMap>,
+ path: Path,
+ ) -> Result<Option<ModuleDefId>, Ident> {
resolve_function_call_path(def_maps, self.module_id, path)
}
}
@@ -31,7 +39,7 @@ pub fn resolve_function_call_path(
def_maps: &HashMap<CrateId, CrateDefMap>,
module_id: ModuleId,
path: Path,
-) -> Option<ModuleDefId> {
+) -> Result<Option<ModuleDefId>, Ident> {
// lets package up the path into an ImportDirective and resolve it using that
let import = ImportDirective {
module_id: module_id.local_id,
@@ -42,8 +50,7 @@ pub fn resolve_function_call_path(
let def_map = &def_maps[&module_id.krate];
let path_res = resolve_path_to_ns(&import, def_map, def_maps);
let ns = match path_res {
- // XXX: It is possible to propagate the actual segment of the resolver which failed
- PathResolution::Unresolved(seg) => return None,
+ PathResolution::Unresolved(seg) => return Err(seg),
PathResolution::Resolved(ns) => ns,
};
@@ -51,5 +58,5 @@ pub fn resolve_function_call_path(
// In the future we can generalise and return a PerNs
// Which the Resolver will then deal with
// For now, since this is used only for function call paths, it is fine
- ns.take_values()
+ Ok(ns.take_values())
}
