]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix export/import of range loop.
authorDan Scales <danscales@google.com>
Sun, 19 Sep 2021 16:23:21 +0000 (09:23 -0700)
committerDan Scales <danscales@google.com>
Mon, 20 Sep 2021 00:13:47 +0000 (00:13 +0000)
As with other recent issues, the Init field of a range loop was not
being handled properly. Generally, it is much better to explicitly
import/export the Init statements, else they are incorrectly added
before the associated node, rather than as the Init value of the node.
This was causing labels to not be correctly added to the range loop that
it is immediately preceding.

Made the ORANGE handling completely similar to the OFOR handling.

Fixes #48462

Change-Id: I999530e84f9357f81deaa3dda50660061f710e7c
Reviewed-on: https://go-review.googlesource.com/c/go/+/350911
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>

src/cmd/compile/internal/typecheck/iexport.go
src/cmd/compile/internal/typecheck/iimport.go
test/typeparam/issue48462.dir/a.go [new file with mode: 0644]
test/typeparam/issue48462.dir/main.go [new file with mode: 0644]
test/typeparam/issue48462.go [new file with mode: 0644]

index a9522c38875221a04d0039fc47fc705bc3da4b7b..def94085447e2e1b6d886c7fb1a006fe7dd68f96 100644 (file)
@@ -1472,7 +1472,7 @@ func isNonEmptyAssign(n ir.Node) bool {
 // non-empty n.Ninit and where n is not a non-empty assignment or a node with a natural init
 // section (such as in "if", "for", etc.).
 func (w *exportWriter) stmt(n ir.Node) {
-       if len(n.Init()) > 0 && !ir.StmtWithInit(n.Op()) && !isNonEmptyAssign(n) {
+       if len(n.Init()) > 0 && !ir.StmtWithInit(n.Op()) && !isNonEmptyAssign(n) && n.Op() != ir.ORANGE {
                // can't use stmtList here since we don't want the final OEND
                for _, n := range n.Init() {
                        w.stmt(n)
@@ -1573,6 +1573,7 @@ func (w *exportWriter) stmt(n ir.Node) {
                n := n.(*ir.RangeStmt)
                w.op(ir.ORANGE)
                w.pos(n.Pos())
+               w.stmtList(n.Init())
                w.exprsOrNil(n.Key, n.Value)
                w.expr(n.X)
                w.stmtList(n.Body)
index 3b3c2a2e2abd8517947d3918a3be129642ffee71..a7339903fcdc6d4aaf7afbbeae5fc181b6a775ef 100644 (file)
@@ -1515,7 +1515,7 @@ func (r *importReader) node() ir.Node {
                if go117ExportTypes {
                        n.SetOp(op)
                }
-               *n.PtrInit() = init
+               n.SetInit(init)
                n.IsDDD = r.bool()
                if go117ExportTypes {
                        n.SetType(r.exoticType())
@@ -1660,26 +1660,28 @@ func (r *importReader) node() ir.Node {
        case ir.OIF:
                pos, init := r.pos(), r.stmtList()
                n := ir.NewIfStmt(pos, r.expr(), r.stmtList(), r.stmtList())
-               *n.PtrInit() = init
+               n.SetInit(init)
                return n
 
        case ir.OFOR:
                pos, init := r.pos(), r.stmtList()
                cond, post := r.exprsOrNil()
                n := ir.NewForStmt(pos, nil, cond, post, r.stmtList())
-               *n.PtrInit() = init
+               n.SetInit(init)
                return n
 
        case ir.ORANGE:
-               pos := r.pos()
+               pos, init := r.pos(), r.stmtList()
                k, v := r.exprsOrNil()
-               return ir.NewRangeStmt(pos, k, v, r.expr(), r.stmtList())
+               n := ir.NewRangeStmt(pos, k, v, r.expr(), r.stmtList())
+               n.SetInit(init)
+               return n
 
        case ir.OSELECT:
                pos := r.pos()
                init := r.stmtList()
                n := ir.NewSelectStmt(pos, r.commList())
-               *n.PtrInit() = init
+               n.SetInit(init)
                return n
 
        case ir.OSWITCH:
@@ -1687,7 +1689,7 @@ func (r *importReader) node() ir.Node {
                init := r.stmtList()
                x, _ := r.exprsOrNil()
                n := ir.NewSwitchStmt(pos, x, r.caseList(x))
-               *n.PtrInit() = init
+               n.SetInit(init)
                return n
 
        // case OCASE:
diff --git a/test/typeparam/issue48462.dir/a.go b/test/typeparam/issue48462.dir/a.go
new file mode 100644 (file)
index 0000000..26c704d
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
+
+func Unique[T comparable](set []T) []T {
+       nset := make([]T, 0, 8)
+
+loop:
+       for _, s := range set {
+               for _, e := range nset {
+                       if s == e {
+                               continue loop
+                       }
+               }
+
+               nset = append(nset, s)
+       }
+
+       return nset
+}
diff --git a/test/typeparam/issue48462.dir/main.go b/test/typeparam/issue48462.dir/main.go
new file mode 100644 (file)
index 0000000..8054ddd
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+       "fmt"
+       "reflect"
+
+       "a"
+)
+
+func main() {
+       e := []int{1, 2, 2, 3, 1, 6}
+
+       got := a.Unique(e)
+       want := []int{1, 2, 3, 6}
+       if !reflect.DeepEqual(got, want) {
+               panic(fmt.Sprintf("got %d, want %d", got, want))
+       }
+
+}
diff --git a/test/typeparam/issue48462.go b/test/typeparam/issue48462.go
new file mode 100644 (file)
index 0000000..76930e5
--- /dev/null
@@ -0,0 +1,7 @@
+// rundir -G=3
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ignored