Skip to content
Open
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: 6 additions & 1 deletion crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,12 @@ fn compute_loop_body_semantic<'db>(
) -> (ExprId, InnerContext<'db>) {
let db: &dyn Database = ctx.db;
ctx.run_in_subscope(|new_ctx| {
let return_type = new_ctx.get_return_type().unwrap();
// `None` means we're outside a function/loop context (e.g. a loop in array size position).
// The invalid usage will be caught by the caller; use `missing` to suppress cascading
// errors.
let return_type = new_ctx
.get_return_type()
.unwrap_or_else(|| TypeId::missing(new_ctx.db, skip_diagnostic()));
let old_inner_ctx = new_ctx.inner_ctx.replace(InnerContext { return_type, kind });
let (statements, tail) = statements_and_tail(ctx.db, syntax.statements(db));
let mut statements_semantic = vec![];
Expand Down
29 changes: 29 additions & 0 deletions crates/cairo-lang-semantic/src/expr/test_data/fixed_size_array
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,32 @@ fn foo() {
foo

//! > expected_diagnostics

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

//! > Loop expression in fixed-size array size position.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {}

//! > function_name
foo

//! > module_code
fn f() -> [felt252; for _ in 0..1_u32 {}] {
[0]
}

//! > expected_diagnostics
error[E2302]: Type mismatch: `()` and `core::integer::u32`.
--> lib.cairo:1:21
fn f() -> [felt252; for _ in 0..1_u32 {}] {
^^^^^^^^^^^^^^^^^^^^

error[E2172]: Fixed size array type must have a positive integer size.
--> lib.cairo:1:11
fn f() -> [felt252; for _ in 0..1_u32 {}] {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Loading