//! > Test loop.
//! > test_runner_name
test_function_lowering
//! > function
fn foo(y: felt252) {
let mut x = 5;
while x != y {
x = x - 1;
}
}
//! > function_name
foo
//! > module_code
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v12: core::RangeCheck, v13: core::gas::GasBuiltin, v0: core::felt252
blk0 (root):
Statements:
(v1: core::felt252) <- 5
(v14: core::RangeCheck, v15: core::gas::GasBuiltin, v4: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr12](v12, v13, v1, v0)
End:
Match(match_enum(v4) {
PanicResult::Ok(v5) => blk1,
PanicResult::Err(v8) => blk2,
})
blk1:
Statements:
(v6: core::felt252, v7: ()) <- struct_destructure(v5)
(v9: ((),)) <- struct_construct(v7)
(v10: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v9)
End:
Return(v14, v15, v10)
blk2:
Statements:
(v11: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v8)
End:
Return(v14, v15, v11)
//! > ==========================================================================
//! > Test while let
//! > test_runner_name
test_function_lowering
//! > function
fn foo(a: myEnum) -> felt252 {
let mut y = 0;
while let myEnum::A(x) = a {
y = y + x;
};
y
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum myEnum {
A: felt252,
B,
C,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v12: core::RangeCheck, v13: core::gas::GasBuiltin, v0: test::myEnum
blk0 (root):
Statements:
(v1: core::felt252) <- 0
(v14: core::RangeCheck, v15: core::gas::GasBuiltin, v4: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr8](v12, v13, v0, v1)
End:
Match(match_enum(v4) {
PanicResult::Ok(v5) => blk1,
PanicResult::Err(v8) => blk2,
})
blk1:
Statements:
(v6: core::felt252, v7: ()) <- struct_destructure(v5)
(v9: (core::felt252,)) <- struct_construct(v6)
(v10: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v9)
End:
Return(v14, v15, v10)
blk2:
Statements:
(v11: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v8)
End:
Return(v14, v15, v11)
//! > ==========================================================================
//! > Test while let extern enum
//! > test_runner_name
test_function_lowering
//! > function
fn foo() -> felt252 {
let mut y = 0;
while let myEnum::A(x) = a() {
y = y + x
};
y = y + 1;
return y;
}
//! > function_name
foo
//! > module_code
enum myEnum {
A: felt252,
B,
C,
}
extern fn a() -> myEnum nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v14: core::RangeCheck, v15: core::gas::GasBuiltin
blk0 (root):
Statements:
(v0: core::felt252) <- 0
(v16: core::RangeCheck, v17: core::gas::GasBuiltin, v5: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr8](v14, v15, v0)
End:
Match(match_enum(v5) {
PanicResult::Ok(v6) => blk1,
PanicResult::Err(v9) => blk2,
})
blk1:
Statements:
(v7: core::felt252, v8: ()) <- struct_destructure(v6)
(v3: core::felt252) <- 1
(v13: core::felt252) <- core::felt252_add(v7, v3)
(v10: (core::felt252,)) <- struct_construct(v13)
(v11: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v10)
End:
Return(v16, v17, v11)
blk2:
Statements:
(v12: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v9)
End:
Return(v16, v17, v12)
//! > ==========================================================================
//! > Test while let tuple
//! > test_runner_name
test_function_lowering
//! > function
fn foo() -> felt252 {
let mut y = 0;
while let (myEnum::A(x), true) = (a(), 5 == 6) {
y = y + x
};
y = y + 1;
return y;
}
//! > function_name
foo
//! > module_code
enum myEnum {
A: felt252,
B,
C,
}
extern fn a() -> myEnum nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v14: core::RangeCheck, v15: core::gas::GasBuiltin
blk0 (root):
Statements:
(v0: core::felt252) <- 0
(v16: core::RangeCheck, v17: core::gas::GasBuiltin, v5: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr15](v14, v15, v0)
End:
Match(match_enum(v5) {
PanicResult::Ok(v6) => blk1,
PanicResult::Err(v9) => blk2,
})
blk1:
Statements:
(v7: core::felt252, v8: ()) <- struct_destructure(v6)
(v3: core::felt252) <- 1
(v13: core::felt252) <- core::felt252_add(v7, v3)
(v10: (core::felt252,)) <- struct_construct(v13)
(v11: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v10)
End:
Return(v16, v17, v11)
blk2:
Statements:
(v12: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v9)
End:
Return(v16, v17, v12)
//! > ==========================================================================
//! > Test while let unsupported tuple
//! > test_runner_name
test_function_lowering
//! > function
fn foo() -> felt252 {
let mut y = 0;
while let (myEnum::A(x), 3) = (a(), 3) {
y = y + x
};
y = y + 1;
return y;
}
//! > function_name
foo
//! > module_code
enum myEnum {
A: felt252,
B,
C,
}
extern fn a() -> myEnum nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
error: Unsupported value in while-let. Currently, while-let on tuples only supports enums as tuple members.
--> lib.cairo:9:35
while let (myEnum::A(x), 3) = (a(), 3) {
^******^
//! > lowering_flat
Parameters:
blk0 (root):
Statements:
(v0: core::felt252) <- 0
(v2: core::felt252) <- test::foo[expr10](v0)
(v3: core::felt252) <- 1
(v4: core::felt252) <- core::Felt252Add::add(v2, v3)
End:
Return(v4)
//! > ==========================================================================
//! > Test while let numeric
//! > test_runner_name
test_function_lowering
//! > function
fn foo() -> felt252 {
let mut y = 0;
while let x = y {
y = y + x
};
y = y + 1;
return y;
}
//! > function_name
foo
//! > module_code
//! > semantic_diagnostics
//! > lowering_diagnostics
error: Numeric values are not supported in while-let conditions.
--> lib.cairo:3:5
while let x = y {
^***************^
//! > lowering_flat
Parameters:
blk0 (root):
Statements:
(v0: core::felt252) <- 0
(v2: core::felt252) <- test::foo[expr8](v0)
(v3: core::felt252) <- 1
(v4: core::felt252) <- core::Felt252Add::add(v2, v3)
End:
Return(v4)
//! > ==========================================================================
//! > Test while let underscore
//! > test_runner_name
test_function_lowering
//! > function
fn foo(a: myEnum) -> felt252 {
let mut y = 0;
while let _ = a {
y = y + 5;
};
y = y + 1;
return y;
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum myEnum {
A: felt252,
B,
C,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v15: core::RangeCheck, v16: core::gas::GasBuiltin, v0: test::myEnum
blk0 (root):
Statements:
(v1: core::felt252) <- 0
(v17: core::RangeCheck, v18: core::gas::GasBuiltin, v6: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr8](v15, v16, v0, v1)
End:
Match(match_enum(v6) {
PanicResult::Ok(v7) => blk1,
PanicResult::Err(v10) => blk2,
})
blk1:
Statements:
(v8: core::felt252, v9: ()) <- struct_destructure(v7)
(v4: core::felt252) <- 1
(v14: core::felt252) <- core::felt252_add(v8, v4)
(v11: (core::felt252,)) <- struct_construct(v14)
(v12: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v11)
End:
Return(v17, v18, v12)
blk2:
Statements:
(v13: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v10)
End:
Return(v17, v18, v13)
//! > ==========================================================================
//! > Test while let unsupported type
//! > test_runner_name
test_function_lowering
//! > function
fn foo(a: MyStruct) -> felt252 {
let mut y = 0;
while let _ = a {
y = y + 5;
};
y = y + 1;
return y;
}
//! > function_name
foo
//! > module_code
struct MyStruct {
A: felt252,
B: felt252,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
error: Unsupported type in while-let. Type: `test::MyStruct`.
--> lib.cairo:7:19
while let _ = a {
^
//! > lowering_flat
Parameters: v0: test::MyStruct
blk0 (root):
Statements:
(v1: core::felt252) <- 0
(v3: core::felt252) <- test::foo[expr8](v0, v1)
(v4: core::felt252) <- 1
(v5: core::felt252) <- core::Felt252Add::add(v3, v4)
End:
Return(v5)