Accurately classify assoc. types in paths

This commit is contained in:
Jonas Schievink 2021-03-30 02:08:33 +02:00
parent b3ca06e4fd
commit 41c7448e12
2 changed files with 38 additions and 18 deletions

View file

@ -917,6 +917,21 @@ fn f() -> impl Iterator<Item$0 = u8> {}
); );
} }
#[test]
#[should_panic = "unresolved reference"]
fn unknown_assoc_ty() {
check(
r#"
trait Iterator {
type Item;
//^^^^
}
fn f() -> impl Iterator<Invalid$0 = u8> {}
"#,
)
}
#[test] #[test]
fn goto_def_for_assoc_ty_in_path_multiple() { fn goto_def_for_assoc_ty_in_path_multiple() {
check( check(

View file

@ -330,12 +330,14 @@ impl NameRefClass {
} }
} }
if ast::AssocTypeArg::cast(parent.clone()).is_some() { if let Some(assoc_type_arg) = ast::AssocTypeArg::cast(parent.clone()) {
if assoc_type_arg.name_ref().as_ref() == Some(name_ref) {
// `Trait<Assoc = Ty>` // `Trait<Assoc = Ty>`
// ^^^^^ // ^^^^^
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
let resolved = sema.resolve_path(&path)?; let resolved = sema.resolve_path(&path)?;
if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved { if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved {
// FIXME: resolve in supertraits
if let Some(ty) = tr if let Some(ty) = tr
.items(sema.db) .items(sema.db)
.iter() .iter()
@ -350,6 +352,9 @@ impl NameRefClass {
))); )));
} }
} }
return None;
}
} }
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {