Skip to content

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

Merged
orizi merged 1 commit intomainfrom
orizi/03-31-fix_missing_return_type_in_loop_body
Apr 1, 2026
Merged

fix: handle missing return type context in loop body instead of panicking#9808
orizi merged 1 commit intomainfrom
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
Small, localized change in semantic loop-body handling plus a test; primary risk is subtle changes in diagnostics/type inference for loops in unusual contexts.

Overview
Fixes a semantic-analysis panic in compute_loop_body_semantic by handling a missing return-type context (when a loop appears outside function/loop contexts, e.g. in fixed-size array length expressions) and substituting TypeId::missing to avoid cascading failures.

Adds a regression test to expr/test_data/fixed_size_array covering fn f() -> [felt252; for _ in 0..1_u32 {}] and asserting proper diagnostics are emitted instead of crashing.

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

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Collaborator Author

orizi commented Mar 31, 2026

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).

@orizi orizi changed the base branch from orizi/03-31-fix_member_access_on_unreduced_impl_type to graphite-base/9808 April 1, 2026 09:01
@orizi orizi force-pushed the graphite-base/9808 branch from 64b11af to e87ae95 Compare April 1, 2026 09:05
@orizi orizi force-pushed the orizi/03-31-fix_missing_return_type_in_loop_body branch from b495f1a to e43c38a Compare April 1, 2026 09:05
@orizi orizi changed the base branch from graphite-base/9808 to main April 1, 2026 09:05
Copy link
Copy Markdown
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

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

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

@orizi orizi enabled auto-merge April 1, 2026 09:06
@orizi orizi added this pull request to the merge queue Apr 1, 2026
Merged via the queue into main with commit 8dd8779 Apr 1, 2026
105 checks passed
@orizi orizi deleted the orizi/03-31-fix_missing_return_type_in_loop_body branch April 1, 2026 09:49
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