diff --git a/src/lang/defs.rs b/src/lang/defs.rs index 405e6533..704ce51d 100644 --- a/src/lang/defs.rs +++ b/src/lang/defs.rs @@ -2,8 +2,8 @@ use std::iter; use cairo_lang_defs::db::DefsGroup; use cairo_lang_defs::ids::{ - FunctionTitleId, LanguageElementId, LookupItemId, MemberId, ModuleId, ModuleItemId, - NamedLanguageElementId, SubmoduleLongId, TopLevelLanguageElementId, TraitItemId, + LanguageElementId, LookupItemId, MemberId, ModuleId, ModuleItemId, NamedLanguageElementId, + SubmoduleLongId, TopLevelLanguageElementId, TraitItemId, }; use cairo_lang_diagnostics::ToOption; use cairo_lang_doc::db::DocGroup; @@ -680,6 +680,7 @@ fn resolved_generic_item_def( ) -> Option { Some(match item { ResolvedGenericItem::GenericConstant(item) => item.untyped_stable_ptr(db.upcast()), + ResolvedGenericItem::Module(module_id) => { match module_id { ModuleId::CrateRoot(_) => { @@ -699,28 +700,48 @@ fn resolved_generic_item_def( } } } + ResolvedGenericItem::GenericFunction(item) => { - let title = match item { - GenericFunctionId::Free(id) => FunctionTitleId::Free(id), - GenericFunctionId::Extern(id) => FunctionTitleId::Extern(id), - GenericFunctionId::Impl(id) => { - // Note: Only the trait title is returned. - FunctionTitleId::Trait(id.function) + let declaration: ast::FunctionDeclaration = match item { + GenericFunctionId::Free(id) => { + id.stable_ptr(db.upcast()).lookup(db.upcast()).declaration(db.upcast()) + } + GenericFunctionId::Extern(id) => { + id.stable_ptr(db.upcast()).lookup(db.upcast()).declaration(db.upcast()) } + GenericFunctionId::Impl(id) => match id.impl_function(db.upcast()) { + Ok(Some(id)) => { + id.stable_ptr(db.upcast()).lookup(db.upcast()).declaration(db.upcast()) + } + // It is possible (Marek didn't find out how it happens), that we hop into a + // situation where concrete impl is not inferred yet, so we can't find the + // declaration. Fall back to trait function in such cases. + _ => id + .function + .stable_ptr(db.upcast()) + .lookup(db.upcast()) + .declaration(db.upcast()), + }, }; - title.untyped_stable_ptr(db.upcast()) + declaration.name(db.upcast()).stable_ptr().untyped() } + ResolvedGenericItem::GenericType(generic_type) => { generic_type.untyped_stable_ptr(db.upcast()) } + ResolvedGenericItem::GenericTypeAlias(type_alias) => { type_alias.untyped_stable_ptr(db.upcast()) } + ResolvedGenericItem::GenericImplAlias(impl_alias) => { impl_alias.untyped_stable_ptr(db.upcast()) } + ResolvedGenericItem::Variant(variant) => variant.id.stable_ptr(db.upcast()).untyped(), + ResolvedGenericItem::Trait(trt) => trt.stable_ptr(db.upcast()).untyped(), + ResolvedGenericItem::Impl(imp) => imp.stable_ptr(db.upcast()).untyped(), ResolvedGenericItem::Variable(var) => var.untyped_stable_ptr(db.upcast()), }) diff --git a/tests/e2e/find_references/fns.rs b/tests/e2e/find_references/fns.rs index 8ccf84b6..228b0ca2 100644 --- a/tests/e2e/find_references/fns.rs +++ b/tests/e2e/find_references/fns.rs @@ -10,7 +10,7 @@ fn fn_via_definition() { let x = pow2(2) + pow2(3); } "#, @r" - fn pow2(x: felt252) -> felt252 { x * x } + fn pow2(x: felt252) -> felt252 { x * x } fn main() { let x = pow2(2) + pow2(3); } @@ -26,7 +26,7 @@ fn fn_via_call() { let x = pow2(2) + pow2(3); } "#, @r" - fn pow2(x: felt252) -> felt252 { x * x } + fn pow2(x: felt252) -> felt252 { x * x } fn main() { let x = pow2(2) + pow2(3); } @@ -43,7 +43,7 @@ fn unused_function() { let x = pow2 + pow2; } "#, @r" - fn pow2(x: felt252) -> felt252 { x * x } + fn pow2(x: felt252) -> felt252 { x * x } fn main() { let pow2 = 2; let x = pow2 + pow2; diff --git a/tests/e2e/find_references/traits.rs b/tests/e2e/find_references/traits.rs index 65ada3bf..96ce85cc 100644 --- a/tests/e2e/find_references/traits.rs +++ b/tests/e2e/find_references/traits.rs @@ -68,7 +68,7 @@ fn trait_method_via_definition() { #[derive(Drop)] struct Foo {} trait FooTrait { - fn area(self: @Foo) -> u64; + fn area(self: @Foo) -> u64; } impl FooImpl of FooTrait { fn area(self: @Foo) -> u64 { 0 } @@ -117,7 +117,7 @@ fn trait_method_via_dot_call() { #[derive(Drop)] struct Foo {} trait FooTrait { - fn area(self: @Foo) -> u64; + fn area(self: @Foo) -> u64; } impl FooImpl of FooTrait { fn area(self: @Foo) -> u64 { 0 } @@ -166,7 +166,7 @@ fn trait_method_via_path_call() { #[derive(Drop)] struct Foo {} trait FooTrait { - fn area(self: @Foo) -> u64; + fn area(self: @Foo) -> u64; } impl FooImpl of FooTrait { fn area(self: @Foo) -> u64 { 0 } diff --git a/tests/e2e/find_references/vars.rs b/tests/e2e/find_references/vars.rs index 8e4d442a..f6951131 100644 --- a/tests/e2e/find_references/vars.rs +++ b/tests/e2e/find_references/vars.rs @@ -133,9 +133,9 @@ fn param_via_binding() { num * num } "#, @r" - fn pow(num: felt252) -> felt252 { + fn pow(num: felt252) -> felt252 { num * num - } + } ") } diff --git a/tests/e2e/goto_definition/fns.rs b/tests/e2e/goto_definition/fns.rs index ec7aa4ea..510fa4e6 100644 --- a/tests/e2e/goto_definition/fns.rs +++ b/tests/e2e/goto_definition/fns.rs @@ -11,7 +11,7 @@ fn fn_call() { } ", @r" fn main() { foo(); } - fn foo() {} // good + fn foo() {} // good mod bar { fn foo() {} // bad } diff --git a/tests/e2e/goto_definition/paths.rs b/tests/e2e/goto_definition/paths.rs index 9e8fcf0b..1c918529 100644 --- a/tests/e2e/goto_definition/paths.rs +++ b/tests/e2e/goto_definition/paths.rs @@ -61,7 +61,7 @@ fn fn_in_submodule() { } fn foo() {} // bad mod module { - fn foo() {} // good + fn foo() {} // good } ") } diff --git a/tests/e2e/goto_definition/traits.rs b/tests/e2e/goto_definition/traits.rs index 6093411f..900751a2 100644 --- a/tests/e2e/goto_definition/traits.rs +++ b/tests/e2e/goto_definition/traits.rs @@ -91,7 +91,7 @@ fn dot_method_in_expr() { } ", @r" pub trait Foo { - fn foo(self: T); + fn foo(self: T); } #[derive(Copy, Drop)] pub struct Bar {} @@ -122,7 +122,7 @@ fn full_path_method_in_expr() { } ", @r" pub trait Foo { - fn foo(self: T); + fn foo(self: T); } #[derive(Copy, Drop)] pub struct Bar {}