]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: look for function in instantiations in all global assignments
authorDan Scales <danscales@google.com>
Wed, 14 Apr 2021 21:54:14 +0000 (14:54 -0700)
committerDan Scales <danscales@google.com>
Thu, 15 Apr 2021 00:14:55 +0000 (00:14 +0000)
Add in some missing global assignment ops to the list of globals ops
that should be traversed to look for generic function instantiations.
The most common other one for global assigments (and the relevant one
for this bug) is OAS2FUNC, but also look at global assigments with
OAS2DOTTYPE, OAS2MAPR, OAS2RECV, and OASOP.

Bonus small fix: get rid of -G=3 case in ir.IsAddressable. Now that we
don't call the old typechecker from noder2, we don't need this -G-3
check anymore.

Fixes #45547.

Change-Id: I75fecec55ea0d6f62e1c2294d4d77447ed9be6ae
Reviewed-on: https://go-review.googlesource.com/c/go/+/310210
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/ir/expr.go
src/cmd/compile/internal/noder/stencil.go
test/typeparam/issue45547.go [new file with mode: 0644]

index c95ea36909a7c93d428783290b29b14850cfa034..112d3941ce416e2bf780d4250e37b79a3fb4dd70 100644 (file)
@@ -748,13 +748,6 @@ func IsAddressable(n Node) bool {
        case ODEREF, ODOTPTR:
                return true
 
-       case OXDOT:
-               // TODO(danscales): remove this case as we remove calls to the old
-               // typechecker in (*irgen).funcBody().
-               if base.Flag.G == 0 {
-                       return false
-               }
-               fallthrough
        case ODOT:
                n := n.(*SelectorExpr)
                return IsAddressable(n.X)
index 329c80098a34e7d2daab79bef4a44cdd2ce0d073..2745016545af0355dc8d04ca93e8505e9b48f37f 100644 (file)
@@ -53,11 +53,15 @@ func (g *irgen) stencil() {
                                continue
                        }
 
-               case ir.OAS:
-
-               case ir.OAS2:
+               case ir.OAS, ir.OAS2, ir.OAS2DOTTYPE, ir.OAS2FUNC, ir.OAS2MAPR, ir.OAS2RECV, ir.OASOP:
+                       // These are all the various kinds of global assignments,
+                       // whose right-hand-sides might contain a function
+                       // instantiation.
 
                default:
+                       // The other possible ops at the top level are ODCLCONST
+                       // and ODCLTYPE, which don't have any function
+                       // instantiations.
                        continue
                }
 
diff --git a/test/typeparam/issue45547.go b/test/typeparam/issue45547.go
new file mode 100644 (file)
index 0000000..0a08d66
--- /dev/null
@@ -0,0 +1,20 @@
+// compile -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 p
+
+func f[T any]() (f, g T) { return f, g }
+
+// Tests for generic function instantiation on the right hande side of multi-value
+// assignments.
+
+func _() {
+       // Multi-value assignment within a function
+       var _, _ = f[int]()
+}
+
+// Multi-value assignment outside a function.
+var _, _ = f[int]()