Skip to content

Commit 904bdff

Browse files
Merge #8250
8250: More accurately classify assoc. types in paths r=jonas-schievink a=jonas-schievink Previously `Iterator<Whoops$0 = ()>` would go to the `Iterator` trait. This fixes that and correctly marks `Whoops` as unresolved. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents b3ca06e + 41c7448 commit 904bdff

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

crates/ide/src/goto_definition.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,21 @@ fn f() -> impl Iterator<Item$0 = u8> {}
917917
);
918918
}
919919

920+
#[test]
921+
#[should_panic = "unresolved reference"]
922+
fn unknown_assoc_ty() {
923+
check(
924+
r#"
925+
trait Iterator {
926+
type Item;
927+
//^^^^
928+
}
929+
930+
fn f() -> impl Iterator<Invalid$0 = u8> {}
931+
"#,
932+
)
933+
}
934+
920935
#[test]
921936
fn goto_def_for_assoc_ty_in_path_multiple() {
922937
check(

crates/ide_db/src/defs.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,25 +330,30 @@ impl NameRefClass {
330330
}
331331
}
332332

333-
if ast::AssocTypeArg::cast(parent.clone()).is_some() {
334-
// `Trait<Assoc = Ty>`
335-
// ^^^^^
336-
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
337-
let resolved = sema.resolve_path(&path)?;
338-
if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved {
339-
if let Some(ty) = tr
340-
.items(sema.db)
341-
.iter()
342-
.filter_map(|assoc| match assoc {
343-
hir::AssocItem::TypeAlias(it) => Some(*it),
344-
_ => None,
345-
})
346-
.find(|alias| &alias.name(sema.db).to_string() == &name_ref.text())
347-
{
348-
return Some(NameRefClass::Definition(Definition::ModuleDef(
349-
ModuleDef::TypeAlias(ty),
350-
)));
333+
if let Some(assoc_type_arg) = ast::AssocTypeArg::cast(parent.clone()) {
334+
if assoc_type_arg.name_ref().as_ref() == Some(name_ref) {
335+
// `Trait<Assoc = Ty>`
336+
// ^^^^^
337+
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
338+
let resolved = sema.resolve_path(&path)?;
339+
if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved {
340+
// FIXME: resolve in supertraits
341+
if let Some(ty) = tr
342+
.items(sema.db)
343+
.iter()
344+
.filter_map(|assoc| match assoc {
345+
hir::AssocItem::TypeAlias(it) => Some(*it),
346+
_ => None,
347+
})
348+
.find(|alias| &alias.name(sema.db).to_string() == &name_ref.text())
349+
{
350+
return Some(NameRefClass::Definition(Definition::ModuleDef(
351+
ModuleDef::TypeAlias(ty),
352+
)));
353+
}
351354
}
355+
356+
return None;
352357
}
353358
}
354359

0 commit comments

Comments
 (0)