fix: emit pre-range statements to outer scope in ForRange#593
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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>
Member
|
@xgopilot Add a test case for this scenario |
Contributor
Author
Test case added successfully!I've added What the test doesThe 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:
Test results✅ All tests pass ( 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by @xushiwei
Fixes goplus/xgo#2629
Summary
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 scopeRangeAssignThennow extracts these pre-range statements usingclearBlockStmt()Endemits them to the outer scope before the for-range statementChanges
stmt.go: AddedpreStmtsfield toforRangeStmt, extract pre-range statements inRangeAssignThen, emit them inEndTest plan
go test ./...)cl/_testgop/dql7in goplus/xgo will validate this fix end-to-end