Skip to content

Commit 660fd4a

Browse files
committed
Resolve only type params in type ns
1 parent 4fa8749 commit 660fd4a

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

crates/hir/src/semantics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl PathResolution {
7070
| PathResolution::Local(_)
7171
| PathResolution::Macro(_)
7272
| PathResolution::ConstParam(_) => None,
73-
PathResolution::TypeParam(param) => Some(TypeNs::GenericParam(param.merge().into())),
73+
PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())),
7474
PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
7575
PathResolution::AssocItem(AssocItem::Const(_) | AssocItem::Function(_)) => None,
7676
PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => {

crates/hir/src/source_analyzer.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use syntax::{
3434
use crate::{
3535
db::HirDatabase, semantics::PathResolution, Adt, BuiltinAttr, BuiltinType, Const, Field,
3636
Function, Local, MacroDef, ModuleDef, Static, Struct, ToolModule, Trait, Type, TypeAlias,
37-
TypeOrConstParam, Variant,
37+
Variant,
3838
};
3939
use base_db::CrateId;
4040

@@ -609,10 +609,7 @@ fn resolve_hir_path_(
609609

610610
let res = match ty {
611611
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
612-
TypeNs::GenericParam(id) => match (TypeOrConstParam { id }).split(db) {
613-
either::Either::Left(x) => PathResolution::ConstParam(x),
614-
either::Either::Right(x) => PathResolution::TypeParam(x),
615-
},
612+
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
616613
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
617614
PathResolution::Def(Adt::from(it).into())
618615
}
@@ -706,10 +703,7 @@ fn resolve_hir_path_qualifier(
706703

707704
resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
708705
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
709-
TypeNs::GenericParam(id) => match (TypeOrConstParam { id }).split(db) {
710-
either::Either::Left(x) => PathResolution::ConstParam(x),
711-
either::Either::Right(x) => PathResolution::TypeParam(x),
712-
},
706+
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
713707
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
714708
TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
715709
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),

crates/hir_def/src/generics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ impl GenericParams {
365365
where_predicates.shrink_to_fit();
366366
}
367367

368+
pub fn find_type_by_name(&self, name: &Name) -> Option<LocalTypeOrConstParamId> {
369+
self.types
370+
.iter()
371+
.filter(|x| matches!(x.1, TypeOrConstParamData::TypeParamData(_)))
372+
.find_map(|(id, p)| if p.name().as_ref() == Some(&name) { Some(id) } else { None })
373+
}
374+
368375
pub fn find_type_or_const_by_name(&self, name: &Name) -> Option<LocalTypeOrConstParamId> {
369376
self.types
370377
.iter()

crates/hir_def/src/resolver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId,
2626
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
2727
LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
28-
TypeOrConstParamId, VariantId,
28+
TypeOrConstParamId, TypeParamId, VariantId,
2929
};
3030

3131
#[derive(Debug, Clone, Default)]
@@ -68,7 +68,7 @@ enum Scope {
6868
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6969
pub enum TypeNs {
7070
SelfType(ImplId),
71-
GenericParam(TypeOrConstParamId),
71+
GenericParam(TypeParamId),
7272
AdtId(AdtId),
7373
AdtSelfType(AdtId),
7474
// Yup, enum variants are added to the types ns, but any usage of variant as
@@ -192,7 +192,7 @@ impl Resolver {
192192
Scope::GenericParams { .. } | Scope::ImplDefScope(_) if skip_to_mod => continue,
193193

194194
Scope::GenericParams { params, def } => {
195-
if let Some(local_id) = params.find_type_or_const_by_name(first_name) {
195+
if let Some(local_id) = params.find_type_by_name(first_name) {
196196
let idx = if path.segments().len() == 1 { None } else { Some(1) };
197197
return Some((
198198
TypeNs::GenericParam(

crates/hir_ty/src/lower.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'a> TyLoweringContext<'a> {
372372
_ => return None,
373373
};
374374
match resolution {
375-
TypeNs::GenericParam(param_id) => Some(param_id),
375+
TypeNs::GenericParam(param_id) => Some(param_id.into()),
376376
_ => None,
377377
}
378378
}
@@ -991,9 +991,9 @@ fn named_associated_type_shorthand_candidates<R>(
991991
return res;
992992
}
993993
// Handle `Self::Type` referring to own associated type in trait definitions
994-
if let GenericDefId::TraitId(trait_id) = param_id.parent {
994+
if let GenericDefId::TraitId(trait_id) = param_id.parent() {
995995
let generics = generics(db.upcast(), trait_id.into());
996-
if generics.params.types[param_id.local_id].is_trait_self() {
996+
if generics.params.types[param_id.local_id()].is_trait_self() {
997997
let trait_ref = TyBuilder::trait_ref(db, trait_id)
998998
.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0)
999999
.build();

0 commit comments

Comments
 (0)