Skip to content

Commit

Permalink
Resolve only type params in type ns
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Mar 4, 2022
1 parent 4fa8749 commit 660fd4a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down
12 changes: 3 additions & 9 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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()),
Expand Down
7 changes: 7 additions & 0 deletions crates/hir_def/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions crates/hir_def/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions crates/hir_ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 660fd4a

Please sign in to comment.