Prefer ValueNS when resolving hir path for PathExpressions

This commit is contained in:
Lukas Wirth 2021-01-29 15:59:52 +01:00
parent 426ad8e165
commit e3eeccf8ef

View file

@ -222,6 +222,7 @@ impl SourceAnalyzer {
db: &dyn HirDatabase,
path: &ast::Path,
) -> Option<PathResolution> {
let mut prefer_value_ns = false;
if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
let expr_id = self.expr_id(db, &path_expr.into())?;
let infer = self.infer.as_ref()?;
@ -233,9 +234,7 @@ impl SourceAnalyzer {
{
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
}
if let Some(func) = infer[expr_id].as_fn_def() {
return Some(PathResolution::Def(ModuleDef::Function(func.into())));
}
prefer_value_ns = true;
}
if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) {
@ -281,7 +280,7 @@ impl SourceAnalyzer {
}
}
resolve_hir_path(db, &self.resolver, &hir_path)
resolve_hir_path_(db, &self.resolver, &hir_path, prefer_value_ns)
}
pub(crate) fn record_literal_missing_fields(
@ -451,12 +450,22 @@ fn adjust(
.map(|(_ptr, scope)| *scope)
}
#[inline]
pub(crate) fn resolve_hir_path(
db: &dyn HirDatabase,
resolver: &Resolver,
path: &Path,
) -> Option<PathResolution> {
let types =
resolve_hir_path_(db, resolver, path, false)
}
fn resolve_hir_path_(
db: &dyn HirDatabase,
resolver: &Resolver,
path: &Path,
prefer_value_ns: bool,
) -> Option<PathResolution> {
let types = || {
resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
@ -467,10 +476,11 @@ pub(crate) fn resolve_hir_path(
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
});
})
};
let body_owner = resolver.body_owner();
let values =
let values = || {
resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| {
let res = match val {
ValueNs::LocalBinding(pat_id) => {
@ -486,18 +496,25 @@ pub(crate) fn resolve_hir_path(
ValueNs::GenericParam(it) => PathResolution::ConstParam(it.into()),
};
Some(res)
});
})
};
let items = resolver
.resolve_module_path_in_items(db.upcast(), path.mod_path())
.take_types()
.map(|it| PathResolution::Def(it.into()));
let items = || {
resolver
.resolve_module_path_in_items(db.upcast(), path.mod_path())
.take_types()
.map(|it| PathResolution::Def(it.into()))
};
types.or(values).or(items).or_else(|| {
let macros = || {
resolver
.resolve_path_as_macro(db.upcast(), path.mod_path())
.map(|def| PathResolution::Macro(def.into()))
})
};
if prefer_value_ns { values().or_else(types) } else { types().or_else(values) }
.or_else(items)
.or_else(macros)
}
/// Resolves a path where we know it is a qualifier of another path.