Skip to content

Commit

Permalink
bugfix: TestForRangeUDT3_NoAssign
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Nov 10, 2021
1 parent 550726d commit 1afa79f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
32 changes: 31 additions & 1 deletion gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1afa79f

Please sign in to comment.