Skip to content

Protect variables in inner scopes. - #484

Closed
prozacchiwawa wants to merge 1 commit into
mainfrom
20260518-protect-variables-in-inner-scopes
Closed

prozacchiwawa wants to merge 1 commit into
mainfrom
20260518-protect-variables-in-inner-scopes

Conversation

@prozacchiwawa

@prozacchiwawa prozacchiwawa commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

This would have prevented compilation so it doesn't need to be feature gated.


Note

Medium Risk
Medium risk because it changes common-subexpression elimination placement logic in the optimizer, which can subtly affect generated programs and correctness when assign/let bindings are involved.

Overview
Fixes a CSE optimizer bug where a common subexpression could be lifted outside the scope of variables it depends on (notably when one occurrence is inside an assign binding expression).

The CSE pass now computes a ceiling based on which binding names the subexpression uses, restricts detect_common_cse_root accordingly (skipping the optimization when no safe common root exists), and adds a regression test program (cse-complex-2.clsp) plus a new classic test that compiles and executes it to ensure the result remains correct.

Reviewed by Cursor Bugbot for commit 1dfac3c. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1dfac3c. Configure here.

if let Some(ceiling) = ceiling {
if ceiling.len() > idx || (ceiling.len() == idx && target_path[0..idx] != *ceiling) {
return None;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ceiling check bypassed when BodyOf found first

Medium Severity

In detect_common_cse_root, the BodyOf check at line 443 returns immediately without verifying the ceiling constraint. Since the loop iterates in reverse (deepest to shallowest), if target_path ends with BodyOf (or encounters one before hitting a non-BodyOf element where the ceiling check fires), the function returns that position even if it's above the ceiling. The ceiling check on lines 447–451 only executes for non-BodyOf elements, so it never gets a chance to reject a BodyOf that violates the ceiling constraint.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 1dfac3c. Configure here.

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.

1 participant