Skip to content

Hoist Hole conditions and fix while loop variable scoping#670

Draft
joehendrix wants to merge 2 commits intomainfrom
jhx/control-flow-fixes
Draft

Hoist Hole conditions and fix while loop variable scoping#670
joehendrix wants to merge 2 commits intomainfrom
jhx/control-flow-fixes

Conversation

@joehendrix
Copy link
Contributor

Summary

  • Condition hoisting for if/while: When an if or while condition translates to a Hole (e.g., from subscript expressions like dict[key]), it's hoisted into a fresh variable declaration. This prevents free variable errors during Core type checking.
  • While loop variable scoping fix: Variable declarations from inside while loop bodies are now emitted as separate statements in the parent scope (not wrapped in a Block), matching Python's scoping semantics where variables assigned inside loops remain visible after the loop.

Test plan

  • lake build passes cleanly
  • All AnalyzeLaurelTest cases pass

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

🤖 Generated with Claude Code

When if/while conditions translate to Hole (e.g., subscript expressions),
hoist them into a fresh variable declaration to avoid free variable errors
in Core type checking.

Fix while loop variable hoisting: emit declarations as separate statements
in the parent scope rather than wrapped in a Block, so variables assigned
inside the loop body remain in scope after the loop (matching Python
scoping semantics).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@joehendrix joehendrix requested a review from a team March 26, 2026 06:52
@joehendrix joehendrix marked this pull request as draft March 26, 2026 07:04
Add 2 end-to-end tests:
- test_while_var_scope.py: variable declared inside while loop body
  accessed after the loop (Python scoping)
- test_unlifted_hole.py: non-modeled call nested in keyword argument
  produces Hole that gets hoisted to avoid free variable errors

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@olivier-aws
Copy link
Contributor

This seems (at least from the PR description) similar to #639 . Can you explain the difference?

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.

2 participants