Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/hir-ty/src/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,16 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
.skip_binder();
(assoc_type, EarlyBinder::bind(trait_args).instantiate(interner, impl_trait.args))
}
// associated types in inherent impl
Some(TypeNs::AdtId(adt)) => {
let Some(alias_id) =
crate::method_resolution::find_inherent_assoc_type(db, adt, assoc_name)
else {
return error_ty();
};

return self.lower_path_inner(TyDefId::TypeAliasId(alias_id), infer_args);
}
_ => return error_ty(),
};

Expand Down
43 changes: 41 additions & 2 deletions crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use tracing::{debug, instrument};

use base_db::Crate;
use hir_def::{
AssocItemId, BlockId, BuiltinDeriveImplId, ConstId, FunctionId, GenericParamId, HasModule,
ImplId, ItemContainerId, ModuleId, TraitId,
AdtId, AssocItemId, BlockId, BuiltinDeriveImplId, ConstId, FunctionId, GenericParamId,
HasModule, ImplId, ItemContainerId, ModuleId, TraitId, TypeAliasId,
attrs::AttrFlags,
builtin_derive::BuiltinDeriveImplMethod,
expr_store::path::GenericArgs as HirGenericArgs,
Expand Down Expand Up @@ -877,3 +877,42 @@ impl TraitImpls {
}
}
}

pub fn find_inherent_assoc_type(
db: &dyn HirDatabase,
adt: AdtId,
name: &Name,
) -> Option<TypeAliasId> {
use rustc_type_ir::inherent::GenericArgs;

let interner = DbInterner::new_no_crate(db);

let adt_ty = Ty::new_adt(interner, adt, GenericArgs::identity_for_item(interner, adt.into()));
let simplified = simplify_type(interner, adt_ty, TreatParams::AsRigid)?;

let module = adt.module(db);
let mut found = None;

InherentImpls::for_each_crate_and_block(db, module.krate(db), module.block(db), &mut |impls| {
if found.is_some() {
return;
}
for &impl_id in impls.for_self_ty(&simplified) {
if db.impl_signature(impl_id).target_trait.is_some() {
continue;
}
for (n, item) in impl_id.impl_items(db).items.iter() {
if n == name
&& let AssocItemId::TypeAliasId(id) = item
{
found = Some(*id);
break;
}
}
if found.is_some() {
break;
}
}
});
found
}
31 changes: 31 additions & 0 deletions crates/hir-ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5142,3 +5142,34 @@ fn foo(v: Struct<f32>) {
"#,
);
}

#[test]
fn inherent_associated_type_path() {
check_infer(
r#"
#![feature(inherent_associated_types)]

struct A;
impl A {
type B = i32;
}

trait C { fn foo() -> Self; }
impl C for A::B {
fn foo() -> Self { 1 }
}

fn main() {
let x: A::B = C::foo();
}
"#,
expect![[r#"
149..154 '{ 1 }': i32
151..152 '1': i32
168..199 '{ ...o(); }': ()
178..179 'x': i32
188..194 'C::foo': fn foo<i32>() -> i32
188..196 'C::foo()': i32
"#]],
);
}
16 changes: 16 additions & 0 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,14 @@ fn resolve_hir_path_(
return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into()));
}

// inherent associated types
if let (Some(unresolved), TypeNs::AdtId(adt_id)) = (&unresolved, &ty)
&& let Some(alias_id) =
method_resolution::find_inherent_assoc_type(db, *adt_id, unresolved.name)
{
return Some(PathResolution::Def(ModuleDefId::from(alias_id).into()));
}

let res = match ty {
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
Expand Down Expand Up @@ -1827,6 +1835,14 @@ fn resolve_hir_path_qualifier(
return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into()));
}

// inherent associated types
if let (Some(unresolved), TypeNs::AdtId(adt_id)) = (&unresolved, &ty)
&& let Some(alias_id) =
method_resolution::find_inherent_assoc_type(db, *adt_id, unresolved.name)
{
return Some(PathResolution::Def(ModuleDefId::from(alias_id).into()));
}

let res = match ty {
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()),
Expand Down
34 changes: 34 additions & 0 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,40 @@ fn f() -> impl Iterator<Item$0 = u8> {}
);
}

#[test]
fn goto_def_for_inherent_assoc_ty_in_path() {
check(
r#"
struct A;
impl A {
type B = i32;
//^
}

fn main() {
let _x: A::B$0;
}
"#,
);
}

#[test]
fn goto_def_for_inherent_assoc_ty_in_qualifier() {
check(
r#"
struct A;
impl A {
type B = [u8; 3];
//^
}

fn main() {
let _x = A::B$0::default();
}
"#,
);
}

#[test]
fn goto_def_for_super_assoc_ty_in_path() {
check(
Expand Down