Skip to content

fix: handle missing return type context in loop body instead of panicking#9808

Open
orizi wants to merge 1 commit intoorizi/03-31-fix_member_access_on_unreduced_impl_typefrom
orizi/03-31-fix_missing_return_type_in_loop_body
Open

fix: handle missing return type context in loop body instead of panicking#9808
orizi wants to merge 1 commit intoorizi/03-31-fix_member_access_on_unreduced_impl_typefrom
orizi/03-31-fix_missing_return_type_in_loop_body

Conversation

@orizi
Copy link
Copy Markdown
Collaborator

@orizi orizi commented Mar 31, 2026

Summary

Fixed a panic that occurred when loop expressions were used in fixed-size array size positions by handling the case where no return type context is available and providing appropriate error diagnostics.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The compiler was panicking when encountering loop expressions in array size positions because get_return_type() would return None (since loops in array size positions are outside function/loop contexts), but the code was calling unwrap() on this None value.


What was the behavior or documentation before?

The compiler would panic with an unwrap error when processing loop expressions in fixed-size array size positions, preventing proper error reporting.


What is the behavior or documentation after?

The compiler now gracefully handles this invalid usage by using a missing type when no return type context is available, allowing the semantic analysis to continue and report appropriate error diagnostics about type mismatches and invalid array sizes.


Related issue or discussion (if any)

Fixes #9787


Additional context

The fix includes a test case demonstrating the improved error handling for the specific case of fn f() -> [felt252; for _ in 0..1_u32 {}], which now produces clear error messages instead of panicking.


Note

Low Risk
Low risk: a small semantic-analysis guard replaces an unwrap() with a missing-type fallback, primarily affecting invalid code paths and error reporting.

Overview
Prevents a semantic-analysis panic when computing a loop body in contexts without an available return type (e.g. a for used in fixed-size array size position) by falling back to TypeId::missing instead of unwrap().

Adds a regression test ensuring this invalid construct no longer crashes and now produces regular diagnostics (type mismatch and invalid fixed-size array size).

Written by Cursor Bugbot for commit b495f1a. This will update automatically on new commits. Configure here.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Contributor

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware reviewed all commit messages and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Compiler panic in semantic pass: .unwrap() on None in compute_loop_body_semantic

4 participants