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

Solving current inference state prior to finding index traits. #6587

Merged
merged 1 commit into from
Nov 5, 2024
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
7 changes: 5 additions & 2 deletions crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,11 @@ fn compute_expr_indexed_semantic(
) -> Maybe<Expr> {
let syntax_db = ctx.db.upcast();
let expr = compute_expr_semantic(ctx, &syntax.expr(syntax_db));
let index_expr_syntax = &syntax.index_expr(syntax_db);
let index_expr = compute_expr_semantic(ctx, index_expr_syntax);
// Make sure the maximal amount of types is known when trying to access. Ignoring the returned
// value, as any errors will be reported later.
ctx.resolver.inference().solve().ok();
let candidate_traits: Vec<_> = ["Index", "IndexView"]
.iter()
.map(|trait_name| get_core_trait(ctx.db, CoreTraitContext::Ops, (*trait_name).into()))
Expand All @@ -1881,8 +1886,6 @@ fn compute_expr_indexed_semantic(
|ty, _, _| Some(MultipleImplementationOfIndexOperator(ty)),
)?;

let index_expr_syntax = &syntax.index_expr(syntax_db);
let index_expr = compute_expr_semantic(ctx, index_expr_syntax);
expr_function_call(
ctx,
function_id,
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 `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>.
Candidate `core::ops::index::Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<test::Struct1, ?3>.
Candidate `core::ops::index::IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<test::Struct1, ?3>.
--> lib.cairo:21:15
let _y1 = x1[0];
^***^
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/items/tests/early_conform
Original file line number Diff line number Diff line change
Expand Up @@ -981,13 +981,13 @@ impl MyIndexView of IndexView<u64, MyType, ()> {

//! > expected_diagnostics
error[E0002]: Method `my_into` could not be called on type `core::integer::u16`.
Candidate `test::MyInto::my_into` inference failed with: Trait has no implementation in context: test::MyInto::<core::integer::u16, ?3>.
Candidate `test::MyInto::my_into` inference failed with: Trait has no implementation in context: test::MyInto::<core::integer::u16, ?1>.
--> lib.cairo:15:13
x[3_u16.my_into()];
^*****^

error[E0002]: Method `my_into` could not be called on type `core::integer::u16`.
Candidate `test::MyInto::my_into` inference failed with: Trait has no implementation in context: test::MyInto::<core::integer::u16, ?5>.
Candidate `test::MyInto::my_into` inference failed with: Trait has no implementation in context: test::MyInto::<core::integer::u16, ?4>.
--> lib.cairo:16:17
3_u64[3_u16.my_into()];
^*****^
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/items/tests/trait_type
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,8 @@ impl MyImpl of MyTrait {

//! > expected_diagnostics
error: Type `core::integer::u32` could not be indexed.
Candidate `core::ops::index::Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<core::integer::u32, ?1>.
Candidate `core::ops::index::IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<core::integer::u32, ?1>.
Candidate `core::ops::index::Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<core::integer::u32, ?2>.
Candidate `core::ops::index::IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<core::integer::u32, ?2>.
--> lib.cairo:8:9
x[0];
^**^
Expand Down
22 changes: 22 additions & 0 deletions tests/bug_samples/issue6580.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[derive(Drop, Debug)]
struct SomeStruct {
num: felt252
}

#[test]
fn inner_index_access() {
let arr_of_bytearrays: Array<ByteArray> = array!["str1", "str2", "str3"];
let arr_of_arrays_of_nums: Array<Array<u64>> = array![array![9], array![2], array![19, 20, 21]];
let arr_of_arrays_of_structs: Array<Array<SomeStruct>> = array![
array![SomeStruct { num: 8 }],
array![SomeStruct { num: 2 }],
array![SomeStruct { num: 9 }, SomeStruct { num: 10 }, SomeStruct { num: 11 }]
];

let index_1 = 2_usize;
let index_2 = 2_usize;

let _nested_item = arr_of_arrays_of_nums[index_1][index_2];
let _char = arr_of_bytearrays[index_1][index_2];
let _some_struct = arr_of_arrays_of_structs[index_1][index_2];
}
1 change: 1 addition & 0 deletions tests/bug_samples/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mod issue5629;
mod issue5680;
mod issue5764;
mod issue5967;
mod issue6580;
mod loop_break_in_match;
mod loop_only_change;
mod partial_param_local;
Expand Down