1
0
mirror of https://github.com/golang/go synced 2024-11-22 21:10:03 -07:00

cmd/compile: look for function in instantiations in all global assignments

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>
This commit is contained in:
Dan Scales 2021-04-14 14:54:14 -07:00
parent 567a9322ad
commit bf634c76b2
3 changed files with 27 additions and 10 deletions

View 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)

View 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
}

View File

@ -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]()