diff --git a/gop_test.go b/gop_test.go index 8b569868..2b76104e 100644 --- a/gop_test.go +++ b/gop_test.go @@ -536,7 +536,7 @@ func bar(v *foo.Bar) { `) } -func TestForRangeUDT3(t *testing.T) { +func TestForRangeUDT3_WithAssign(t *testing.T) { pkg := newMainPackage() foo := pkg.Import("github.com/goplus/gox/internal/foo") bar := foo.Ref("Bar").Type() @@ -567,6 +567,36 @@ func bar(v *foo.Bar) { `) } +// bugfix: for range udt { ... } +func TestForRangeUDT3_NoAssign(t *testing.T) { + pkg := newMainPackage() + foo := pkg.Import("github.com/goplus/gox/internal/foo") + bar := foo.Ref("Bar").Type() + v := pkg.NewParam(token.NoPos, "v", types.NewPointer(bar)) + pkg.NewFunc(nil, "bar", types.NewTuple(v), nil, false).BodyStart(pkg). + ForRange().Val(v).RangeAssignThen(token.NoPos). + Val(pkg.Import("fmt").Ref("Println")).Val("Hi").Call(1).EndStmt(). + End().End() + domTest(t, pkg, `package main + +import ( + foo "github.com/goplus/gox/internal/foo" + fmt "fmt" +) + +func bar(v *foo.Bar) { + for _gop_it := v.Gop_Enum(); ; { + var _gop_ok bool + _, _gop_ok = _gop_it.Next() + if !_gop_ok { + break + } + fmt.Println("Hi") + } +} +`) +} + func TestForRangeUDT4(t *testing.T) { pkg := newMainPackage() foo := pkg.Import("github.com/goplus/gox/internal/foo") diff --git a/stmt.go b/stmt.go index 08fd2937..7e3a8fc0 100644 --- a/stmt.go +++ b/stmt.go @@ -634,6 +634,12 @@ func (p *forRangeStmt) End(cb *CodeBuilder) { */ lhs := make([]ast.Expr, n) lhs[0] = p.stmt.Key + if lhs[0] == nil { // bugfix: for range udt { ... } + lhs[0] = underscore + if p.stmt.Tok == 0 { + p.stmt.Tok = token.ASSIGN + } + } lhs[1] = p.stmt.Value lhs[n-1] = identGopOk body := make([]ast.Stmt, len(stmts)+3)