Skip to content

Commit 6be83b8

Browse files
committed
Fix IDE layer not resolving assoc type paths in path qualifiers
1 parent a892237 commit 6be83b8

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

crates/hir/src/source_analyzer.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,9 +1162,39 @@ fn resolve_hir_path_qualifier(
11621162
resolver: &Resolver,
11631163
path: &Path,
11641164
) -> Option<PathResolution> {
1165-
resolver
1166-
.resolve_path_in_type_ns_fully(db.upcast(), &path)
1167-
.map(|ty| match ty {
1165+
(|| {
1166+
let (ty, unresolved) = match path.type_anchor() {
1167+
Some(type_ref) => {
1168+
let (_, res) = TyLoweringContext::new(db, resolver, resolver.module().into())
1169+
.lower_ty_ext(type_ref);
1170+
res.map(|ty_ns| (ty_ns, path.segments().first()))
1171+
}
1172+
None => {
1173+
let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?;
1174+
match remaining_idx {
1175+
Some(remaining_idx) => {
1176+
if remaining_idx + 1 == path.segments().len() {
1177+
Some((ty, path.segments().last()))
1178+
} else {
1179+
None
1180+
}
1181+
}
1182+
None => Some((ty, None)),
1183+
}
1184+
}
1185+
}?;
1186+
1187+
// If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type
1188+
// within the trait's associated types.
1189+
if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) {
1190+
if let Some(type_alias_id) =
1191+
db.trait_data(trait_id).associated_type_by_name(unresolved.name)
1192+
{
1193+
return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into()));
1194+
}
1195+
}
1196+
1197+
let res = match ty {
11681198
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
11691199
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
11701200
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
@@ -1175,11 +1205,28 @@ fn resolve_hir_path_qualifier(
11751205
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
11761206
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
11771207
TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()),
1178-
})
1179-
.or_else(|| {
1180-
resolver
1181-
.resolve_module_path_in_items(db.upcast(), path.mod_path()?)
1182-
.take_types()
1183-
.map(|it| PathResolution::Def(it.into()))
1184-
})
1208+
};
1209+
match unresolved {
1210+
Some(unresolved) => resolver
1211+
.generic_def()
1212+
.and_then(|def| {
1213+
hir_ty::associated_type_shorthand_candidates(
1214+
db,
1215+
def,
1216+
res.in_type_ns()?,
1217+
|name, id| (name == unresolved.name).then_some(id),
1218+
)
1219+
})
1220+
.map(TypeAlias::from)
1221+
.map(Into::into)
1222+
.map(PathResolution::Def),
1223+
None => Some(res),
1224+
}
1225+
})()
1226+
.or_else(|| {
1227+
resolver
1228+
.resolve_module_path_in_items(db.upcast(), path.mod_path()?)
1229+
.take_types()
1230+
.map(|it| PathResolution::Def(it.into()))
1231+
})
11851232
}

0 commit comments

Comments
 (0)