Skip to content

Commit e43c38a

Browse files
committed
fix: handle missing return type context in loop body instead of panicking
Fixes #9787
1 parent e87ae95 commit e43c38a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

crates/cairo-lang-semantic/src/expr/compute.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,12 @@ fn compute_loop_body_semantic<'db>(
23292329
) -> (ExprId, InnerContext<'db>) {
23302330
let db: &dyn Database = ctx.db;
23312331
ctx.run_in_subscope(|new_ctx| {
2332-
let return_type = new_ctx.get_return_type().unwrap();
2332+
// `None` means we're outside a function/loop context (e.g. a loop in array size position).
2333+
// The invalid usage will be caught by the caller; use `missing` to suppress cascading
2334+
// errors.
2335+
let return_type = new_ctx
2336+
.get_return_type()
2337+
.unwrap_or_else(|| TypeId::missing(new_ctx.db, skip_diagnostic()));
23332338
let old_inner_ctx = new_ctx.inner_ctx.replace(InnerContext { return_type, kind });
23342339
let (statements, tail) = statements_and_tail(ctx.db, syntax.statements(db));
23352340
let mut statements_semantic = vec![];

crates/cairo-lang-semantic/src/expr/test_data/fixed_size_array

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,32 @@ fn foo() {
299299
foo
300300

301301
//! > expected_diagnostics
302+
303+
//! > ==========================================================================
304+
305+
//! > Loop expression in fixed-size array size position.
306+
307+
//! > test_runner_name
308+
test_function_diagnostics(expect_diagnostics: true)
309+
310+
//! > function_code
311+
fn foo() {}
312+
313+
//! > function_name
314+
foo
315+
316+
//! > module_code
317+
fn f() -> [felt252; for _ in 0..1_u32 {}] {
318+
[0]
319+
}
320+
321+
//! > expected_diagnostics
322+
error[E2302]: Type mismatch: `()` and `core::integer::u32`.
323+
--> lib.cairo:1:21
324+
fn f() -> [felt252; for _ in 0..1_u32 {}] {
325+
^^^^^^^^^^^^^^^^^^^^
326+
327+
error[E2172]: Fixed size array type must have a positive integer size.
328+
--> lib.cairo:1:11
329+
fn f() -> [felt252; for _ in 0..1_u32 {}] {
330+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)