diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-12-01 10:28:49 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-01 10:28:49 +0000 |
| commit | c5ad6c8c5f783cd247a42d77a58d21afc91c6984 (patch) | |
| tree | 4f8933803f5e8634c98d192d44bafc62fc4f5385 | |
| parent | 2d0db312b543343f1c208b6be21a7a001cec7dd6 (diff) | |
| parent | b357569d0f43481b0149246768d48b26dcc9273f (diff) | |
| download | rust-c5ad6c8c5f783cd247a42d77a58d21afc91c6984.tar.gz rust-c5ad6c8c5f783cd247a42d77a58d21afc91c6984.zip | |
Merge #10896
10896: hir: resolve assoc trait type in path r=jhgg a=jhgg fixes #9802 - [ ] write tests, maybe, if this is even a good fix... Co-authored-by: Jake Heinz <jh@discordapp.com>
| -rw-r--r-- | crates/hir/src/source_analyzer.rs | 13 | ||||
| -rw-r--r-- | crates/ide/src/syntax_highlighting/test_data/highlighting.html | 9 | ||||
| -rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 9 |
3 files changed, 30 insertions, 1 deletions
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index b12461818e9..23fcd02b57a 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -16,7 +16,7 @@ use hir_def::{ expr::{ExprId, Pat, PatId}, path::{ModPath, Path, PathKind}, resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, - AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, VariantId, + AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, ModuleDefId, VariantId, }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ @@ -544,6 +544,17 @@ fn resolve_hir_path_( } } }?; + + // 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(ModuleDefId::from(type_alias_id).into())); + } + } + let res = match ty { TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index ab810aceca3..d8c9827b5e0 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::<span class="macro">mirror!</span> <span class="brace">{</span> <span class="brace">}</span> <span class="keyword">const</span> <span class="constant declaration">USAGE_OF_BOOL</span><span class="colon">:</span><span class="builtin_type">bool</span> <span class="operator">=</span> <span class="enum public">Bool</span><span class="operator">::</span><span class="enum_variant public">True</span><span class="operator">.</span><span class="function associated consuming public">to_primitive</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span> +<span class="keyword">trait</span> <span class="trait declaration">Baz</span> <span class="brace">{</span> + <span class="keyword">type</span> <span class="type_alias associated declaration trait">Qux</span><span class="semicolon">;</span> +<span class="brace">}</span> + +<span class="keyword">fn</span> <span class="function declaration">baz</span><span class="angle"><</span><span class="type_param declaration">T</span><span class="angle">></span><span class="parenthesis">(</span><span class="value_param declaration">t</span><span class="colon">:</span> <span class="type_param">T</span><span class="parenthesis">)</span> +<span class="keyword">where</span> + <span class="type_param">T</span><span class="colon">:</span> <span class="trait">Baz</span><span class="comma">,</span> + <span class="angle"><</span><span class="type_param">T</span> <span class="keyword">as</span> <span class="trait">Baz</span><span class="angle">></span><span class="operator">::</span><span class="type_alias associated trait">Qux</span><span class="colon">:</span> <span class="trait">Bar</span> <span class="brace">{</span><span class="brace">}</span> + </code></pre> \ 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 05158c169e0..e74f39a8650 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: T) +where + T: Baz, + <T as Baz>::Qux: Bar {} + //- /foo.rs crate:foo pub struct Person { pub name: &'static str, |
