Skip to content

Fix with-block variable scoping in Python-to-Laurel translation#680

Merged
MikaelMayer merged 5 commits intomainfrom
fix-with-block-scoping
Mar 27, 2026
Merged

Fix with-block variable scoping in Python-to-Laurel translation#680
MikaelMayer merged 5 commits intomainfrom
fix-with-block-scoping

Conversation

@MikaelMayer
Copy link
Contributor

Fix with statement variable scoping in Python-to-Laurel translation

Python with blocks (e.g., with open(...) as f:) were placing variable declarations inside a Laurel Block, which scopes them in Core. When a function contained multiple with blocks reusing the same variable name, the second block would generate a havoc for a variable that wasn't declared at the outer Core scope, causing a "Cannot havoc undeclared variable" type-checking error.

The fix hoists the manager and as-variable declarations out of the Block, matching Python's scoping semantics where with-block variables remain visible after the block ends.

New test added in VerifyPythonTest.lean exercising multiple with blocks with the same variable name.

Python with-blocks placed variable declarations inside a Laurel Block,
which scopes them in Core. Multiple with-blocks reusing the same
variable name caused a 'Cannot havoc undeclared variable' type-checking
error because the second block generated a havoc for a variable not
declared at the outer Core scope.

Hoist manager and as-variable declarations out of the Block, matching
Python scoping where with-block variables remain visible after the
block ends. Add test for multiple with-blocks with the same variable.
Copy link
Contributor Author

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

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

Clean fix that correctly hoists with-block variable declarations to the outer scope, matching Python's scoping semantics and the pattern already used by try/except. The separation into declStmts (outside block) vs setupStmts (inside block) is clear and well-structured. Test covers the exact failure scenario.

One potential follow-up noted inline (not blocking).

@MikaelMayer

This comment was marked as resolved.

@MikaelMayer MikaelMayer marked this pull request as ready for review March 26, 2026 21:34
@MikaelMayer MikaelMayer requested a review from a team March 26, 2026 21:34
@MikaelMayer MikaelMayer enabled auto-merge March 26, 2026 21:34
@thanhnguyen-aws
Copy link
Contributor

Could you please check whether this PR overlaps with this one #639.

@MikaelMayer
Copy link
Contributor Author

Checked — PR #639 also fixes this bug. I checked out #639, added this PR's test, and it passes. #639 takes a more comprehensive approach (hoisting all declarations to function top), while this PR is a targeted fix for the with-block case specifically. If #639 lands first, this PR can be closed. If this one lands first, #639 should still be compatible since it restructures the same area more broadly.

@MikaelMayer MikaelMayer added this pull request to the merge queue Mar 27, 2026
Merged via the queue into main with commit dbbf064 Mar 27, 2026
15 checks passed
@MikaelMayer MikaelMayer deleted the fix-with-block-scoping branch March 27, 2026 01:01
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.

4 participants