From a1b2d258100477ec9d6e6f29fec767a91076cd00 Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Wed, 1 Dec 2021 08:44:30 +0000 Subject: [PATCH 1/3] hir: resolve assoc trait type --- crates/hir/src/source_analyzer.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index b12461818e..e249139836 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -15,7 +15,7 @@ use hir_def::{ }, expr::{ExprId, Pat, PatId}, path::{ModPath, Path, PathKind}, - resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, + resolver::{resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, VariantId, }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; @@ -544,6 +544,17 @@ fn resolve_hir_path_( } } }?; + + if let (Some(_), TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { + let resolver = trait_id.resolver(db.upcast()); + if let Some(module_def_id) = resolver + .resolve_module_path_in_trait_assoc_items(db.upcast(), path.mod_path()) + .and_then(|ns| ns.take_types()) + { + return Some(PathResolution::Def(module_def_id.into())); + } + } + let res = match ty { TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), From fec2d39f3c316f379b6aad0947b77e24445f3b8b Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Wed, 1 Dec 2021 09:23:42 +0000 Subject: [PATCH 2/3] simplify?? --- crates/hir/src/source_analyzer.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index e249139836..23fcd02b57 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -15,8 +15,8 @@ use hir_def::{ }, expr::{ExprId, Pat, PatId}, path::{ModPath, Path, PathKind}, - resolver::{resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, - AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, VariantId, + resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, + AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, ModuleDefId, VariantId, }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ @@ -545,13 +545,13 @@ fn resolve_hir_path_( } }?; - if let (Some(_), TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { - let resolver = trait_id.resolver(db.upcast()); - if let Some(module_def_id) = resolver - .resolve_module_path_in_trait_assoc_items(db.upcast(), path.mod_path()) - .and_then(|ns| ns.take_types()) + // If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type + // within the trait's associated types. + if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { + if let Some(type_alias_id) = + db.trait_data(trait_id).associated_type_by_name(&unresolved.name) { - return Some(PathResolution::Def(module_def_id.into())); + return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into())); } } From b357569d0f43481b0149246768d48b26dcc9273f Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Wed, 1 Dec 2021 10:28:18 +0000 Subject: [PATCH 3/3] add test --- .../src/syntax_highlighting/test_data/highlighting.html | 9 +++++++++ crates/ide/src/syntax_highlighting/tests.rs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index ab810aceca..d8c9827b5e 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -265,4 +265,13 @@ proc_macros::mirror! { } const USAGE_OF_BOOL:bool = Bool::True.to_primitive(); +trait Baz { + type Qux; +} + +fn baz<T>(t: T) +where + T: Baz, + <T as Baz>::Qux: Bar {} + \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 05158c169e..e74f39a865 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -238,6 +238,15 @@ impl Bool { } const USAGE_OF_BOOL:bool = Bool::True.to_primitive(); +trait Baz { + type Qux; +} + +fn baz(t: T) +where + T: Baz, + ::Qux: Bar {} + //- /foo.rs crate:foo pub struct Person { pub name: &'static str,