]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: add CONVIFACE nodes needed in generic code due to assignments
authorDan Scales <danscales@google.com>
Tue, 31 Aug 2021 15:01:44 +0000 (08:01 -0700)
committerDan Scales <danscales@google.com>
Tue, 7 Sep 2021 23:27:08 +0000 (23:27 +0000)
Added new function earlyTransformAssign() to add needed CONVIFACE nodes
due to assignments in generic functions.

Fixes #48049

Change-Id: I7cd9cee6ecf34ed2ef0743d1b17645b9f520fa00
Reviewed-on: https://go-review.googlesource.com/c/go/+/347914
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/noder/stmt.go
src/cmd/compile/internal/noder/transform.go
test/typeparam/issue48049.go [new file with mode: 0644]

index eeb994d34328696165a28f134d21b6ec42386493..146761c23f79aceeacc48e8f26aa58cbea1bd9e2 100644 (file)
@@ -101,6 +101,8 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
                        n.Def = initDefn(n, names)
 
                        if delay {
+                               earlyTransformAssign(n, lhs, rhs)
+                               n.X, n.Y = lhs[0], rhs[0]
                                n.SetTypecheck(3)
                                return n
                        }
@@ -115,6 +117,7 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
                n := ir.NewAssignListStmt(g.pos(stmt), ir.OAS2, lhs, rhs)
                n.Def = initDefn(n, names)
                if delay {
+                       earlyTransformAssign(n, lhs, rhs)
                        n.SetTypecheck(3)
                        return n
                }
index 180891b5b50c81460b188aa2f6813d702e0e114c..b278f3db09c7db4a8a4b4e65472ca5e57d44b260 100644 (file)
@@ -365,6 +365,59 @@ assignOK:
        }
 }
 
+// Version of transformAssign that can run on generic code that adds CONVIFACE calls
+// as needed (and rewrites multi-value calls).
+func earlyTransformAssign(stmt ir.Node, lhs, rhs []ir.Node) {
+       cr := len(rhs)
+       if len(rhs) == 1 {
+               if rtyp := rhs[0].Type(); rtyp != nil && rtyp.IsFuncArgStruct() {
+                       cr = rtyp.NumFields()
+               }
+       }
+
+       // x,y,z = f()
+       _, isCallExpr := rhs[0].(*ir.CallExpr)
+       if isCallExpr && cr > len(rhs) {
+               stmt := stmt.(*ir.AssignListStmt)
+               stmt.SetOp(ir.OAS2FUNC)
+               r := rhs[0].(*ir.CallExpr)
+               rtyp := r.Type()
+
+               mismatched := false
+               failed := false
+               for i := range lhs {
+                       result := rtyp.Field(i).Type
+
+                       if lhs[i].Type() == nil || result == nil {
+                               failed = true
+                       } else if lhs[i] != ir.BlankNode && !types.Identical(lhs[i].Type(), result) {
+                               mismatched = true
+                       }
+               }
+               if mismatched && !failed {
+                       typecheck.RewriteMultiValueCall(stmt, r)
+               }
+               return
+       }
+
+       // x, ok = y
+       if len(lhs) != len(rhs) {
+               assert(len(lhs) == 2 && len(rhs) == 1)
+               // TODO(danscales): deal with case where x or ok is an interface
+               // type. We want to add CONVIFACE now, but that is tricky, because
+               // the rhs may be AS2MAPR, AS2RECV, etc. which has two result values,
+               // and that is not rewritten until the order phase (o.stmt, as2ok).
+               return
+       }
+
+       // Check for interface conversion on each assignment
+       for i, r := range rhs {
+               if lhs[i].Type() != nil && lhs[i].Type().IsInterface() {
+                       rhs[i] = assignconvfn(r, lhs[i].Type())
+               }
+       }
+}
+
 // Corresponds to typecheck.typecheckargs.  Really just deals with multi-value calls.
 func transformArgs(n ir.InitNode) {
        var list []ir.Node
diff --git a/test/typeparam/issue48049.go b/test/typeparam/issue48049.go
new file mode 100644 (file)
index 0000000..3a00514
--- /dev/null
@@ -0,0 +1,33 @@
+// run -gcflags=-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 main
+
+func main() {
+       Gooer2[byte]()
+}
+
+type Fooer[T any] interface {
+       Foo(p T)
+}
+
+type fooer1[T any] struct{}
+
+func (fooer1[T]) Foo(T) {}
+
+type fooer2[T any] struct {
+       r []Fooer[T]
+}
+
+//go:noinline
+func (mr fooer2[T]) Foo(p T) {
+       mr.r[0] = fooer1[T]{}
+       return
+}
+
+func Gooer2[T any]() Fooer[T] {
+       return fooer2[T]{}
+}