Skip to content

Commit c5ad6c8

Browse files
bors[bot]jhgg
andauthored
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 <[email protected]>
2 parents 2d0db31 + b357569 commit c5ad6c8

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

crates/hir/src/source_analyzer.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use hir_def::{
1616
expr::{ExprId, Pat, PatId},
1717
path::{ModPath, Path, PathKind},
1818
resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
19-
AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, VariantId,
19+
AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, ModuleDefId, VariantId,
2020
};
2121
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
2222
use hir_ty::{
@@ -544,6 +544,17 @@ fn resolve_hir_path_(
544544
}
545545
}
546546
}?;
547+
548+
// If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type
549+
// within the trait's associated types.
550+
if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) {
551+
if let Some(type_alias_id) =
552+
db.trait_data(trait_id).associated_type_by_name(&unresolved.name)
553+
{
554+
return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into()));
555+
}
556+
}
557+
547558
let res = match ty {
548559
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
549560
TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),

crates/ide/src/syntax_highlighting/test_data/highlighting.html

+9
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,13 @@
265265
<span class="brace">}</span>
266266
<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>
267267

268+
<span class="keyword">trait</span> <span class="trait declaration">Baz</span> <span class="brace">{</span>
269+
<span class="keyword">type</span> <span class="type_alias associated declaration trait">Qux</span><span class="semicolon">;</span>
270+
<span class="brace">}</span>
271+
272+
<span class="keyword">fn</span> <span class="function declaration">baz</span><span class="angle">&lt;</span><span class="type_param declaration">T</span><span class="angle">&gt;</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>
273+
<span class="keyword">where</span>
274+
<span class="type_param">T</span><span class="colon">:</span> <span class="trait">Baz</span><span class="comma">,</span>
275+
<span class="angle">&lt;</span><span class="type_param">T</span> <span class="keyword">as</span> <span class="trait">Baz</span><span class="angle">&gt;</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>
276+
268277
</code></pre>

crates/ide/src/syntax_highlighting/tests.rs

+9
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ impl Bool {
238238
}
239239
const USAGE_OF_BOOL:bool = Bool::True.to_primitive();
240240
241+
trait Baz {
242+
type Qux;
243+
}
244+
245+
fn baz<T>(t: T)
246+
where
247+
T: Baz,
248+
<T as Baz>::Qux: Bar {}
249+
241250
//- /foo.rs crate:foo
242251
pub struct Person {
243252
pub name: &'static str,

0 commit comments

Comments
 (0)