Skip to content

Commit ab55805

Browse files
committed
Auto merge of rust-lang#12198 - jonas-schievink:ide-resolve-supertrait-assoc-types, r=jonas-schievink
fix: Resolve assoc. types of supertraits in the IDE layer Fixes rust-lang/rust-analyzer#12166
2 parents c42cb9a + 2156815 commit ab55805

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

crates/hir/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use hir_def::{
5454
};
5555
use hir_expand::{name::name, MacroCallKind};
5656
use hir_ty::{
57-
autoderef,
57+
all_super_traits, autoderef,
5858
consteval::{unknown_const_as_generic, ComputedExpr, ConstEvalError, ConstExt},
5959
diagnostics::BodyValidationDiagnostic,
6060
method_resolution::{self, TyFingerprint},
@@ -1676,6 +1676,11 @@ impl Trait {
16761676
db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
16771677
}
16781678

1679+
pub fn items_with_supertraits(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
1680+
let traits = all_super_traits(db.upcast(), self.into());
1681+
traits.iter().flat_map(|tr| Trait::from(*tr).items(db)).collect()
1682+
}
1683+
16791684
pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
16801685
db.trait_data(self.id).is_auto
16811686
}

crates/ide-completion/src/completions/type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
168168
if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) =
169169
ctx.sema.resolve_path(&path_seg.parent_path())
170170
{
171-
trait_.items(ctx.sema.db).into_iter().for_each(|it| {
171+
trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each(|it| {
172172
if let hir::AssocItem::TypeAlias(alias) = it {
173+
cov_mark::hit!(complete_assoc_type_in_generics_list);
173174
acc.add_type_alias_with_eq(ctx, alias)
174175
}
175176
});

crates/ide-completion/src/tests/type_pos.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,13 @@ fn foo<'lt, T, const C: usize>() {
336336

337337
#[test]
338338
fn completes_types_and_const_in_arg_list() {
339+
cov_mark::check!(complete_assoc_type_in_generics_list);
339340
check(
340341
r#"
341-
trait Trait2 {
342+
trait Trait1 {
343+
type Super;
344+
}
345+
trait Trait2: Trait1 {
342346
type Foo;
343347
}
344348
@@ -348,14 +352,16 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
348352
ct CONST
349353
cp CONST_PARAM
350354
en Enum
351-
ma makro!(…) macro_rules! makro
355+
ma makro!(…) macro_rules! makro
352356
md module
353357
st Record
354358
st Tuple
355359
st Unit
356360
tt Trait
361+
tt Trait1
357362
tt Trait2
358-
ta Foo = (as Trait2) type Foo
363+
ta Foo = (as Trait2) type Foo
364+
ta Super = (as Trait1) type Super
359365
tp T
360366
un Union
361367
bt u32

crates/ide-db/src/defs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,8 @@ impl NameRefClass {
386386
let containing_path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
387387
let resolved = sema.resolve_path(&containing_path)?;
388388
if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved {
389-
// FIXME: resolve in supertraits
390389
if let Some(ty) = tr
391-
.items(sema.db)
390+
.items_with_supertraits(sema.db)
392391
.iter()
393392
.filter_map(|&assoc| match assoc {
394393
hir::AssocItem::TypeAlias(it) => Some(it),

crates/ide/src/goto_definition.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,22 @@ fn f() -> impl Iterator<Item$0 = u8> {}
10121012
);
10131013
}
10141014

1015+
#[test]
1016+
fn goto_def_for_super_assoc_ty_in_path() {
1017+
check(
1018+
r#"
1019+
trait Super {
1020+
type Item;
1021+
//^^^^
1022+
}
1023+
1024+
trait Sub: Super {}
1025+
1026+
fn f() -> impl Sub<Item$0 = u8> {}
1027+
"#,
1028+
);
1029+
}
1030+
10151031
#[test]
10161032
fn unknown_assoc_ty() {
10171033
check_unresolved(

0 commit comments

Comments
 (0)