Skip to content

Commit

Permalink
When running tests without gas not adding redeposits. (#6668) (#6670)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Nov 18, 2024
2 parents 9224df0 + a69fbd0 commit aac3a51
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 35 deletions.
6 changes: 3 additions & 3 deletions crates/cairo-lang-semantic/src/diagnostic_test_data/neg_impl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ impl BadImpl<-NegImpl> of NegImpl {

//! > expected_diagnostics
error: Inference cycle detected
--> lib.cairo:8:16
fn test_func() { {
^**^
--> lib.cairo:10:14
NegImpl::bar()
^*^
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 @@ -1795,6 +1795,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 @@ -1810,8 +1815,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
11 changes: 9 additions & 2 deletions crates/cairo-lang-semantic/src/expr/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,18 @@ impl<'db> Inference<'db> {
/// Returns whether the inference was successful. If not, the error may be found by
/// `.error_state()`.
pub fn solve(&mut self) -> InferenceResult<()> {
self.solve_ex().map_err(|(err_set, _)| err_set)
}

/// Same as `solve`, but returns the error stable pointer if an error occurred.
fn solve_ex(&mut self) -> Result<(), (ErrorSet, Option<SyntaxStablePtrId>)> {
let mut ambiguous = std::mem::take(&mut self.ambiguous);
self.pending.extend(ambiguous.drain(..).map(|(var, _)| var));
while let Some(var) = self.pending.pop_front() {
// First inference error stops inference.
self.solve_single_pending(var)?;
self.solve_single_pending(var).map_err(|err_set| {
(err_set, self.stable_ptrs.get(&InferenceVar::Impl(var)).copied())
})?;
}
Ok(())
}
Expand Down Expand Up @@ -668,7 +675,7 @@ impl<'db> Inference<'db> {
// Conform all uninferred numeric literals to felt252.
loop {
let mut changed = false;
self.solve().map_err(|err_set| (err_set, None))?;
self.solve_ex()?;
for (var, _) in self.ambiguous.clone() {
let impl_var = self.impl_var(var).clone();
if impl_var.concrete_trait_id.trait_id(self.db) != numeric_trait_id {
Expand Down
18 changes: 9 additions & 9 deletions crates/cairo-lang-semantic/src/expr/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ fn bar<T, +core::ops::FnOnce<T, (u32,)>>(c: T) -> core::ops::FnOnce::<T, (u32,)>

//! > expected_diagnostics
error: Type mismatch: `core::integer::u32` and `core::integer::u64`.
--> lib.cairo:4:10
fn foo() {
^
--> lib.cairo:10:23
let _k: felt252 = bar(c);
^*^

//! > ==========================================================================

Expand Down Expand Up @@ -267,9 +267,9 @@ fn bar<T, +core::ops::FnOnce<T, (u32,)>>(c: T) -> core::ops::FnOnce::<T, (u32,)>

//! > expected_diagnostics
error: Type mismatch: `core::felt252` and `core::integer::u128`.
--> lib.cairo:4:10
fn foo() {
^
--> lib.cairo:10:23
let _k: felt252 = bar(c);
^*^

//! > ==========================================================================

Expand Down Expand Up @@ -410,9 +410,9 @@ fn bar(a: felt252) -> u32 {

//! > expected_diagnostics
error: Type mismatch: `(?6,)` and `(?0, ?1, ?2)`.
--> lib.cairo:4:10
fn foo() {
^
--> lib.cairo:8:19
let _f: u32 = bar(2);
^****^

//! > ==========================================================================

Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-semantic/src/expr/test_data/inference
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ impl MyImpl<+MyTrait> of MyTrait {

//! > expected_diagnostics
error: Inference cycle detected
--> lib.cairo:8:10
fn foo() {
^
--> lib.cairo:9:14
MyTrait::foo();
^*^

//! > ==========================================================================

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 @@ -123,8 +123,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 `Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<test::Struct1, ?3>.
Candidate `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
26 changes: 22 additions & 4 deletions crates/cairo-lang-semantic/src/items/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,29 @@ pub fn evaluate_constant_expr(
.collect(),
expr.ty,
),
Expr::StructCtor(ExprStructCtor { members, base_struct: None, ty, .. }) => {
Expr::StructCtor(ExprStructCtor {
members,
base_struct: None,
ty,
concrete_struct_id,
..
}) => {
let member_order = match db.concrete_struct_members(*concrete_struct_id) {
Ok(member_order) => member_order,
Err(diag_add) => return ConstValue::Missing(diag_add),
};
ConstValue::Struct(
members
.iter()
.map(|(_, expr_id)| evaluate_constant_expr(db, exprs, *expr_id, diagnostics))
member_order
.values()
.map(|m| {
members
.iter()
.find(|(member_id, _)| m.id == *member_id)
.map(|(_, expr_id)| {
evaluate_constant_expr(db, exprs, *expr_id, diagnostics)
})
.unwrap_or_else(|| ConstValue::Missing(skip_diagnostic()))
})
.collect(),
*ty,
)
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 `MyInto::my_into` inference failed with: Trait has no implementation in context: test::MyInto::<core::integer::u16, ?3>.
Candidate `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 `MyInto::my_into` inference failed with: Trait has no implementation in context: test::MyInto::<core::integer::u16, ?5>.
Candidate `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
16 changes: 8 additions & 8 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 `Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<core::integer::u32, ?1>.
Candidate `IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<core::integer::u32, ?1>.
Candidate `Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<core::integer::u32, ?2>.
Candidate `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 Expand Up @@ -1150,9 +1150,9 @@ error: Unexpected argument type. Expected: "core::integer::u32", found: "core::i
^

error: Type mismatch: `core::integer::u32` and `core::integer::u16`.
--> lib.cairo:25:11
fn bar3() {
^
--> lib.cairo:26:21
let _: MyTrait::MyType = 3_u32;
^****^

//! > ==========================================================================

Expand Down Expand Up @@ -1525,9 +1525,9 @@ fn ret_expr(x: MyTrait::MyType1) -> MyTrait::MyType2 {
^

error: Type mismatch: `core::integer::u32` and `core::integer::u16`.
--> lib.cairo:15:39
fn let_statement(x: MyTrait::MyType2) {
^
--> lib.cairo:16:21
let _: MyTrait::MyType1 = x;
^*****^

error: Loop has incompatible return types: "core::integer::u16" and "core::integer::u32"
--> lib.cairo:23:19
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];
}
12 changes: 12 additions & 0 deletions tests/bug_samples/issue6623.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
struct S {
a: u64,
b: u64,
}

const X: S = S { b: 31, a: 252 };

#[test]
fn test_const() {
assert_eq!(X.a, 252);
assert_eq!(X.b, 31);
}
2 changes: 2 additions & 0 deletions tests/bug_samples/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ mod issue5629;
mod issue5680;
mod issue5764;
mod issue5967;
mod issue6580;
mod issue6623;
mod loop_break_in_match;
mod loop_only_change;
mod partial_param_local;
Expand Down

0 comments on commit aac3a51

Please sign in to comment.