Skip to content

fix: emit pre-range statements to outer scope in ForRange#593

Merged
xushiwei merged 2 commits intomainfrom
xgopilot/claude/fix-forrange-scope-1771217831
Feb 16, 2026
Merged

fix: emit pre-range statements to outer scope in ForRange#593
xushiwei merged 2 commits intomainfrom
xgopilot/claude/fix-forrange-scope-1771217831

Conversation

@xgopilot
Copy link
Contributor

@xgopilot xgopilot bot commented Feb 16, 2026

Requested by @xushiwei

Fixes goplus/xgo#2629

Summary

  • When ForRange() enters a new block scope, any statements emitted during range expression compilation (e.g., auto-generated type assertions like _autoGo_1, _ := doc.(map[string]any)) were incorrectly placed in the for-loop body instead of the outer scope
  • RangeAssignThen now extracts these pre-range statements using clearBlockStmt()
  • End emits them to the outer scope before the for-range statement

Changes

  • stmt.go: Added preStmts field to forRangeStmt, extract pre-range statements in RangeAssignThen, emit them in End

Test plan

  • All existing gogen tests pass (go test ./...)
  • The XGo test case cl/_testgop/dql7 in goplus/xgo will validate this fix end-to-end

When ForRange() enters a new block scope, any statements emitted during
range expression compilation (e.g., auto-generated type assertions like
`_autoGo_1, _ := doc.(map[string]any)`) were incorrectly placed in the
for-loop body. RangeAssignThen now extracts these pre-range statements,
and End emits them to the outer scope before the for-range statement.

Fixes goplus/xgo#2629

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xushiwei <396972+xushiwei@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.94%. Comparing base (2210ad3) to head (b174b81).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #593   +/-   ##
=======================================
  Coverage   95.94%   95.94%           
=======================================
  Files          22       22           
  Lines        6850     6853    +3     
=======================================
+ Hits         6572     6575    +3     
  Misses        207      207           
  Partials       71       71           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

xgopilot bot pushed a commit to goplus/xgo that referenced this pull request Feb 16, 2026
Revert the XGo-side workaround (reordering compileExpr before ForRange)
and instead update gogen to v1.21.2-0.20260216045904-8435674ebccf which
properly fixes the scoping issue in ForRange/RangeAssignThen/End.

See goplus/gogen#593

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xushiwei <396972+xushiwei@users.noreply.github.com>
@xushiwei
Copy link
Member

@xgopilot Add a test case for this scenario

@xgopilot
Copy link
Contributor Author

xgopilot bot commented Feb 16, 2026

Test case added successfully!

I've added TestForRangePreStmts to xgo_test.go (line 1088-1127) which validates the fix for goplus/xgo#2629.

What the test does

The test creates a scenario where a type assertion statement is emitted during range expression compilation:

_autoGo_1, _ := data.(map[string]interface{})
for k, v := range _autoGo_1 {
    fmt.Println(k, v)
}

The key behavior being tested:

  • When ForRange() enters a new block scope, any statements emitted during range expression compilation (like the type assertion above) should be extracted by RangeAssignThen() using clearBlockStmt()
  • These pre-statements should be emitted to the outer scope before the for-range statement, not inside the loop body

Test results

✅ All tests pass (go test ./...)

The test has been committed and pushed to the PR branch.

Add regression test for goplus/xgo#2629 to verify that statements
emitted during range expression compilation (like type assertions)
are correctly placed in the outer scope before the for-range loop,
not inside the loop body.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xushiwei <396972+xushiwei@users.noreply.github.com>
@xushiwei xushiwei merged commit ffa1ceb into main Feb 16, 2026
20 checks passed
@xgopilot xgopilot bot deleted the xgopilot/claude/fix-forrange-scope-1771217831 branch February 16, 2026 05:20
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.

error code generated by DQL

2 participants