Skip to content

Commit

Permalink
Change full_path for impl, trait, struct and enum items (#6409)
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 authored Sep 24, 2024
1 parent 8a23375 commit f9697a4
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 80 deletions.
47 changes: 37 additions & 10 deletions crates/cairo-lang-defs/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl ImplTypeDefId {
}
impl TopLevelLanguageElementId for ImplTypeDefId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.impl_def_id(db).name(db), self.name(db))
format!("{}::{}", self.impl_def_id(db).full_path(db), self.name(db))
}
}

Expand All @@ -431,7 +431,7 @@ impl ImplConstantDefId {
}
impl TopLevelLanguageElementId for ImplConstantDefId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.impl_def_id(db).name(db), self.name(db))
format!("{}::{}", self.impl_def_id(db).full_path(db), self.name(db))
}
}

Expand All @@ -454,7 +454,7 @@ impl ImplImplDefId {
}
impl TopLevelLanguageElementId for ImplImplDefId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.impl_def_id(db).name(db), self.name(db))
format!("{}::{}", self.impl_def_id(db).full_path(db), self.name(db))
}
}

Expand Down Expand Up @@ -566,7 +566,7 @@ impl TraitTypeId {
}
impl TopLevelLanguageElementId for TraitTypeId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.trait_id(db).name(db), self.name(db))
format!("{}::{}", self.trait_id(db).full_path(db), self.name(db))
}
}

Expand All @@ -588,7 +588,7 @@ impl TraitConstantId {
}
impl TopLevelLanguageElementId for TraitConstantId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.trait_id(db).name(db), self.name(db))
format!("{}::{}", self.trait_id(db).full_path(db), self.name(db))
}
}

Expand All @@ -610,7 +610,7 @@ impl TraitImplId {
}
impl TopLevelLanguageElementId for TraitImplId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.trait_id(db).name(db), self.name(db))
format!("{}::{}", self.trait_id(db).full_path(db), self.name(db))
}
}

Expand All @@ -632,26 +632,53 @@ impl TraitFunctionId {
}
impl TopLevelLanguageElementId for TraitFunctionId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.trait_id(db).name(db), self.name(db))
format!("{}::{}", self.trait_id(db).full_path(db), self.name(db))
}
}

// --- Struct items ---
// TODO(spapini): Override full_path for to include parents, for better debug.
define_top_level_language_element_id!(
define_named_language_element_id!(
MemberId,
MemberLongId,
ast::Member,
lookup_intern_member,
intern_member
);
define_top_level_language_element_id!(
impl MemberId {
pub fn struct_id(&self, db: &dyn DefsGroup) -> StructId {
let MemberLongId(module_file_id, ptr) = self.lookup_intern(db);
let struct_ptr = ast::ItemStructPtr(ptr.untyped().nth_parent(db.upcast(), 2));
StructLongId(module_file_id, struct_ptr).intern(db)
}
}

impl TopLevelLanguageElementId for MemberId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.struct_id(db).full_path(db), self.name(db))
}
}

// --- Enum variants ---
define_named_language_element_id!(
VariantId,
VariantLongId,
ast::Variant,
lookup_intern_variant,
intern_variant
);
impl VariantId {
pub fn enum_id(&self, db: &dyn DefsGroup) -> EnumId {
let VariantLongId(module_file_id, ptr) = self.lookup_intern(db);
let struct_ptr = ast::ItemEnumPtr(ptr.untyped().nth_parent(db.upcast(), 2));
EnumLongId(module_file_id, struct_ptr).intern(db)
}
}

impl TopLevelLanguageElementId for VariantId {
fn full_path(&self, db: &dyn DefsGroup) -> String {
format!("{}::{}", self.enum_id(db).full_path(db), self.name(db))
}
}

define_language_element_id_as_enum! {
/// Id for any variable definition.
Expand Down
10 changes: 5 additions & 5 deletions crates/cairo-lang-lowering/src/lower/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ End:
Return(v4, v5, v6)


Generated Destruct::destruct lowering for source location:
Generated core::traits::Destruct::destruct lowering for source location:
|| {
^^

Expand All @@ -62,7 +62,7 @@ End:
Return(v5, v6, v7)


Generated FnOnce::call lowering for source location:
Generated core::ops::function::FnOnce::call lowering for source location:
|| {
^^

Expand Down Expand Up @@ -160,7 +160,7 @@ End:
Return(v13)


Generated PanicDestruct::panic_destruct lowering for source location:
Generated core::traits::PanicDestruct::panic_destruct lowering for source location:
|| {
^^

Expand All @@ -183,7 +183,7 @@ End:
Return(v3)


Generated FnOnce::call lowering for source location:
Generated core::ops::function::FnOnce::call lowering for source location:
|| {
^^

Expand Down Expand Up @@ -256,7 +256,7 @@ End:
Return()


Generated FnOnce::call lowering for source location:
Generated core::ops::function::FnOnce::call lowering for source location:
let c = || a;
^^

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/expr/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ error: Function not found.
let _f: u32 = bar(a).unwrap();
^*^

error: Ambiguous method call. More than one applicable trait function with a suitable self type was found: OptionTrait::unwrap and ResultTrait::unwrap. Consider adding type annotations or explicitly refer to the impl function.
error: Ambiguous method call. More than one applicable trait function with a suitable self type was found: core::option::OptionTrait::unwrap and core::result::ResultTrait::unwrap. Consider adding type annotations or explicitly refer to the impl function.
--> lib.cairo:10:26
let _f: u32 = bar(a).unwrap();
^****^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ error[E0002]: Method `bar` not found on type `test::MyStruct`. Did you import th
^*^

error[E0002]: Method `baz` could not be called on type `test::MyStruct`.
Candidate `MyTrait::baz` inference failed with: Trait has no implementation in context: test::MyTrait::<test::MyStruct>.
Candidate `test::MyTrait::baz` inference failed with: Trait has no implementation in context: test::MyTrait::<test::MyStruct>.
--> lib.cairo:16:7
d.baz();
^*^
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-semantic/src/expr/test_data/method
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ error[E0002]: Method `is_foo` not found on type `core::option::Option::<?0>`. Di
x.is_foo();
^****^

error: Ambiguous method call. More than one applicable trait function with a suitable self type was found: MyTrait::is_bar and AnotherTrait::is_bar. Consider adding type annotations or explicitly refer to the impl function.
error: Ambiguous method call. More than one applicable trait function with a suitable self type was found: test::MyTrait::is_bar and test::AnotherTrait::is_bar. Consider adding type annotations or explicitly refer to the impl function.
--> lib.cairo:40:7
y.is_bar();
^****^
Expand Down Expand Up @@ -317,7 +317,7 @@ trait MyTrait<T> {

//! > expected_diagnostics
error[E0002]: Method `bar` could not be called on type `core::integer::u16`.
Candidate `MyTrait::bar` inference failed with: Trait has no implementation in context: test::MyTrait::<core::integer::u16>.
Candidate `test::MyTrait::bar` inference failed with: Trait has no implementation in context: test::MyTrait::<core::integer::u16>.
--> lib.cairo:5:11
3_u16.bar();
^*^
Expand All @@ -344,7 +344,7 @@ trait MyTrait<T> {

//! > expected_diagnostics
error[E0002]: Method `bar` could not be called on type `core::integer::u16`.
Candidate `MyTrait::bar` inference failed with: Trait has no implementation in context: test::MyTrait::<core::integer::u16>.
Candidate `test::MyTrait::bar` inference failed with: Trait has no implementation in context: test::MyTrait::<core::integer::u16>.
--> lib.cairo:5:21
(3_u16 + 3_u16).bar();
^*^
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/operators
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl Struct2Index of Index<Struct2, usize, felt252> {

//! > expected_diagnostics
error: Type `test::Struct1` could not be indexed.
Candidate `Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<test::Struct1, ?2>.
Candidate `IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<test::Struct1, ?2>.
Candidate `core::ops::index::Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<test::Struct1, ?2>.
Candidate `core::ops::index::IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<test::Struct1, ?2>.
--> lib.cairo:21:15
let _y1 = x1[0];
^***^
Expand Down
Loading

0 comments on commit f9697a4

Please sign in to comment.