Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix definition node for functions #197

Merged
merged 1 commit into from
Jan 28, 2025
Merged
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
39 changes: 30 additions & 9 deletions src/lang/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -680,6 +680,7 @@ fn resolved_generic_item_def(
) -> Option<SyntaxStablePtrId> {
Some(match item {
ResolvedGenericItem::GenericConstant(item) => item.untyped_stable_ptr(db.upcast()),

ResolvedGenericItem::Module(module_id) => {
match module_id {
ModuleId::CrateRoot(_) => {
Expand All @@ -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()
}

Draggu marked this conversation as resolved.
Show resolved Hide resolved
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()),
})
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/find_references/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn fn_via_definition() {
let x = pow2(2) + pow2(3);
}
"#, @r"
<sel=declaration>fn <sel>pow2</sel>(x: felt252) -> felt252 { x * x }</sel>
fn <sel=declaration>pow2</sel>(x: felt252) -> felt252 { x * x }
fn main() {
let x = <sel>pow2</sel>(2) + <sel>pow2</sel>(3);
}
Expand All @@ -26,7 +26,7 @@ fn fn_via_call() {
let x = po<caret>w2(2) + pow2(3);
}
"#, @r"
<sel=declaration>fn <sel>pow2</sel>(x: felt252) -> felt252 { x * x }</sel>
fn <sel=declaration>pow2</sel>(x: felt252) -> felt252 { x * x }
fn main() {
let x = <sel>pow2</sel>(2) + <sel>pow2</sel>(3);
}
Expand All @@ -43,7 +43,7 @@ fn unused_function() {
let x = pow2 + pow2;
}
"#, @r"
<sel=declaration>fn <sel>pow2</sel>(x: felt252) -> felt252 { x * x }</sel>
fn <sel=declaration>pow2</sel>(x: felt252) -> felt252 { x * x }
fn main() {
let <sel>pow2</sel> = 2;
let x = pow2 + pow2;
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/find_references/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn trait_method_via_definition() {
#[derive(Drop)]
struct Foo {}
trait FooTrait {
<sel=declaration>fn <sel>area</sel>(self: @Foo) -> u64;</sel>
fn <sel=declaration>area</sel>(self: @Foo) -> u64;
}
impl FooImpl of FooTrait {
fn area(self: @Foo) -> u64 { 0 }
Expand Down Expand Up @@ -117,7 +117,7 @@ fn trait_method_via_dot_call() {
#[derive(Drop)]
struct Foo {}
trait FooTrait {
<sel=declaration>fn <sel>area</sel>(self: @Foo) -> u64;</sel>
fn <sel=declaration>area</sel>(self: @Foo) -> u64;
}
impl FooImpl of FooTrait {
fn area(self: @Foo) -> u64 { 0 }
Expand Down Expand Up @@ -166,7 +166,7 @@ fn trait_method_via_path_call() {
#[derive(Drop)]
struct Foo {}
trait FooTrait {
<sel=declaration>fn <sel>area</sel>(self: @Foo) -> u64;</sel>
fn <sel=declaration>area</sel>(self: @Foo) -> u64;
}
impl FooImpl of FooTrait {
fn area(self: @Foo) -> u64 { 0 }
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/find_references/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ fn param_via_binding() {
num * num
}
"#, @r"
<sel=declaration>fn <sel>pow</sel>(num: felt252) -> felt252 {
fn <sel=declaration>pow</sel>(num: felt252) -> felt252 {
num * num
}</sel>
}
")
}

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/goto_definition/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn fn_call() {
}
", @r"
fn main() { foo(); }
<sel>fn foo() {}</sel> // good
fn <sel>foo</sel>() {} // good
mod bar {
fn foo() {} // bad
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/goto_definition/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn fn_in_submodule() {
}
fn foo() {} // bad
mod module {
<sel>fn foo() {}</sel> // good
fn <sel>foo</sel>() {} // good
}
")
}
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/goto_definition/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn dot_method_in_expr() {
}
", @r"
pub trait Foo<T> {
<sel>fn foo(self: T);</sel>
fn <sel>foo</sel>(self: T);
}
#[derive(Copy, Drop)]
pub struct Bar {}
Expand Down Expand Up @@ -122,7 +122,7 @@ fn full_path_method_in_expr() {
}
", @r"
pub trait Foo<T> {
<sel>fn foo(self: T);</sel>
fn <sel>foo</sel>(self: T);
}
#[derive(Copy, Drop)]
pub struct Bar {}
Expand Down
Loading