Skip to content

Commit 444f6ca

Browse files
committed
Resolve associated types
1 parent 75011bb commit 444f6ca

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

crates/hir/src/semantics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ impl PathResolution {
7676
pub fn assoc_type_shorthand_candidates<R>(
7777
&self,
7878
db: &dyn HirDatabase,
79-
mut cb: impl FnMut(TypeAlias) -> Option<R>,
79+
mut cb: impl FnMut(&Name, TypeAlias) -> Option<R>,
8080
) -> Option<R> {
81-
associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into()))
81+
associated_type_shorthand_candidates(db, self.in_type_ns()?, |name, _, id| {
82+
cb(name, id.into())
83+
})
8284
}
8385
}
8486

crates/hir/src/source_analyzer.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ fn resolve_hir_path_(
466466
prefer_value_ns: bool,
467467
) -> Option<PathResolution> {
468468
let types = || {
469-
resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
469+
let (ty, remaining) = resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?;
470+
let res = match ty {
470471
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
471472
TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
472473
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
@@ -476,7 +477,21 @@ fn resolve_hir_path_(
476477
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
477478
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
478479
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
479-
})
480+
};
481+
match remaining {
482+
Some(1) => {
483+
let unresolved = path.segments().get(1)?;
484+
res.assoc_type_shorthand_candidates(db, |name, alias| {
485+
(name == unresolved.name).then(|| alias)
486+
})
487+
.map(TypeAlias::from)
488+
.map(Into::into)
489+
.map(PathResolution::Def)
490+
}
491+
// ambiguous
492+
Some(_) => None,
493+
None => Some(res),
494+
}
480495
};
481496

482497
let body_owner = resolver.body_owner();

crates/ide/src/hover.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,4 +3834,46 @@ fn foo() {}
38343834
"#]],
38353835
);
38363836
}
3837+
3838+
#[test]
3839+
fn hover_generic_assoc() {
3840+
check(
3841+
r#"
3842+
fn foo<T: A>() where T::Assoc$0: {}
3843+
3844+
trait A {
3845+
type Assoc;
3846+
}"#,
3847+
expect![[r#"
3848+
*Assoc*
3849+
3850+
```rust
3851+
test
3852+
```
3853+
3854+
```rust
3855+
type Assoc
3856+
```
3857+
"#]],
3858+
);
3859+
check(
3860+
r#"
3861+
trait A where
3862+
Self::Assoc$0: ,
3863+
{
3864+
type Assoc;
3865+
}"#,
3866+
expect![[r#"
3867+
*Assoc*
3868+
3869+
```rust
3870+
test
3871+
```
3872+
3873+
```rust
3874+
type Assoc
3875+
```
3876+
"#]],
3877+
)
3878+
}
38373879
}

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
2424
};
2525

2626
// Add associated types on type parameters and `Self`.
27-
resolution.assoc_type_shorthand_candidates(ctx.db, |alias| {
27+
resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
2828
acc.add_type_alias(ctx, alias);
2929
None::<()>
3030
});

0 commit comments

Comments
 (0)