Resolve only type params in type ns

This commit is contained in:
hkalbasi 2022-03-04 12:30:53 +03:30
parent 4fa8749c44
commit 660fd4ab41
5 changed files with 17 additions and 16 deletions

View file

@ -70,7 +70,7 @@ impl PathResolution {
| PathResolution::Local(_)
| PathResolution::Macro(_)
| PathResolution::ConstParam(_) => None,
PathResolution::TypeParam(param) => Some(TypeNs::GenericParam(param.merge().into())),
PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())),
PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
PathResolution::AssocItem(AssocItem::Const(_) | AssocItem::Function(_)) => None,
PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => {

View file

@ -34,7 +34,7 @@ use syntax::{
use crate::{
db::HirDatabase, semantics::PathResolution, Adt, BuiltinAttr, BuiltinType, Const, Field,
Function, Local, MacroDef, ModuleDef, Static, Struct, ToolModule, Trait, Type, TypeAlias,
TypeOrConstParam, Variant,
Variant,
};
use base_db::CrateId;
@ -609,10 +609,7 @@ fn resolve_hir_path_(
let res = match ty {
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
TypeNs::GenericParam(id) => match (TypeOrConstParam { id }).split(db) {
either::Either::Left(x) => PathResolution::ConstParam(x),
either::Either::Right(x) => PathResolution::TypeParam(x),
},
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
PathResolution::Def(Adt::from(it).into())
}
@ -706,10 +703,7 @@ fn resolve_hir_path_qualifier(
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) => match (TypeOrConstParam { id }).split(db) {
either::Either::Left(x) => PathResolution::ConstParam(x),
either::Either::Right(x) => PathResolution::TypeParam(x),
},
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),

View file

@ -365,6 +365,13 @@ impl GenericParams {
where_predicates.shrink_to_fit();
}
pub fn find_type_by_name(&self, name: &Name) -> Option<LocalTypeOrConstParamId> {
self.types
.iter()
.filter(|x| matches!(x.1, TypeOrConstParamData::TypeParamData(_)))
.find_map(|(id, p)| if p.name().as_ref() == Some(&name) { Some(id) } else { None })
}
pub fn find_type_or_const_by_name(&self, name: &Name) -> Option<LocalTypeOrConstParamId> {
self.types
.iter()

View file

@ -25,7 +25,7 @@ use crate::{
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId,
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
TypeOrConstParamId, VariantId,
TypeOrConstParamId, TypeParamId, VariantId,
};
#[derive(Debug, Clone, Default)]
@ -68,7 +68,7 @@ enum Scope {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TypeNs {
SelfType(ImplId),
GenericParam(TypeOrConstParamId),
GenericParam(TypeParamId),
AdtId(AdtId),
AdtSelfType(AdtId),
// Yup, enum variants are added to the types ns, but any usage of variant as
@ -192,7 +192,7 @@ impl Resolver {
Scope::GenericParams { .. } | Scope::ImplDefScope(_) if skip_to_mod => continue,
Scope::GenericParams { params, def } => {
if let Some(local_id) = params.find_type_or_const_by_name(first_name) {
if let Some(local_id) = params.find_type_by_name(first_name) {
let idx = if path.segments().len() == 1 { None } else { Some(1) };
return Some((
TypeNs::GenericParam(

View file

@ -372,7 +372,7 @@ impl<'a> TyLoweringContext<'a> {
_ => return None,
};
match resolution {
TypeNs::GenericParam(param_id) => Some(param_id),
TypeNs::GenericParam(param_id) => Some(param_id.into()),
_ => None,
}
}
@ -991,9 +991,9 @@ fn named_associated_type_shorthand_candidates<R>(
return res;
}
// Handle `Self::Type` referring to own associated type in trait definitions
if let GenericDefId::TraitId(trait_id) = param_id.parent {
if let GenericDefId::TraitId(trait_id) = param_id.parent() {
let generics = generics(db.upcast(), trait_id.into());
if generics.params.types[param_id.local_id].is_trait_self() {
if generics.params.types[param_id.local_id()].is_trait_self() {
let trait_ref = TyBuilder::trait_ref(db, trait_id)
.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0)
.build();