1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:40:04 -07:00

cmd/compile: allow delaying of transformCompLit, new transformAddr

For this unusual case, where a constraint specifies exactly one type, we
can have a COMPLIT expression with a type that is/has typeparams.

Therefore, we add code to delay transformCompLit for generic functions.
We also need to break out transformAddr (which corresponds to tcAddr),
and added code for delaying it as well. Also, we now need to export
generic functions containing untransformed OCOMPLIT and OKEY nodes, so
added support for that in iexport.go/iimport.go. Untransformed OKEY
nodes include an ir.Ident/ONONAME which we can now export.

Had to adjust some code/asserts in transformCompLit(), since we may now
be transforming an OCOMPLIT from an imported generic function (i.e. from
a non-local package).

Fixes #48537

Change-Id: I09e1b3bd08b4e013c0b098b8a25d082efa1fef51
Reviewed-on: https://go-review.googlesource.com/c/go/+/354354
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Dan Scales 2021-09-22 10:05:33 -07:00
parent 99c1b249b1
commit 0d838ea5a2
7 changed files with 80 additions and 11 deletions

View File

@ -154,7 +154,11 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node {
case *syntax.Operation:
if expr.Y == nil {
return Unary(pos, g.typ(typ), g.op(expr.Op, unOps[:]), g.expr(expr.X))
n := Unary(pos, g.typ(typ), g.op(expr.Op, unOps[:]), g.expr(expr.X))
if n.Op() == ir.OADDR && !g.delayTransform() {
transformAddr(n.(*ir.AddrExpr))
}
return n
}
switch op := g.op(expr.Op, binOps[:]); op {
case ir.OEQ, ir.ONE, ir.OLT, ir.OLE, ir.OGT, ir.OGE:
@ -353,15 +357,27 @@ func (g *irgen) compLit(typ types2.Type, lit *syntax.CompositeLit) ir.Node {
key = g.expr(elem.Key)
}
value := wrapname(g.pos(elem.Value), g.expr(elem.Value))
if value.Op() == ir.OPAREN {
// Make sure any PAREN node added by wrapper has a type
typed(value.(*ir.ParenExpr).X.Type(), value)
}
exprs[i] = ir.NewKeyExpr(g.pos(elem), key, value)
default:
exprs[i] = wrapname(g.pos(elem), g.expr(elem))
if exprs[i].Op() == ir.OPAREN {
// Make sure any PAREN node added by wrapper has a type
typed(exprs[i].(*ir.ParenExpr).X.Type(), exprs[i])
}
}
}
n := ir.NewCompLitExpr(g.pos(lit), ir.OCOMPLIT, nil, exprs)
typed(g.typ(typ), n)
return transformCompLit(n)
var r ir.Node = n
if !g.delayTransform() {
r = transformCompLit(n)
}
return r
}
func (g *irgen) funcLit(typ2 types2.Type, expr *syntax.FuncLit) ir.Node {

View File

@ -77,10 +77,6 @@ func Nil(pos src.XPos, typ *types.Type) ir.Node {
func Addr(pos src.XPos, x ir.Node) *ir.AddrExpr {
n := typecheck.NodAddrAt(pos, x)
switch x.Op() {
case ir.OARRAYLIT, ir.OMAPLIT, ir.OSLICELIT, ir.OSTRUCTLIT:
n.SetOp(ir.OPTRLIT)
}
typed(types.NewPtr(x.Type()), n)
return n
}

View File

@ -996,6 +996,12 @@ func (subst *subster) node(n ir.Node) ir.Node {
case ir.OSELECT:
transformSelect(m.(*ir.SelectStmt))
case ir.OCOMPLIT:
transformCompLit(m.(*ir.CompLitExpr))
case ir.OADDR:
transformAddr(m.(*ir.AddrExpr))
}
}

View File

@ -933,7 +933,7 @@ func transformArrayLit(elemType *types.Type, bound int64, elts []ir.Node) int64
// transformCompLit transforms n to an OARRAYLIT, OSLICELIT, OMAPLIT, or
// OSTRUCTLIT node, with any needed conversions. Corresponds to
// typecheck.tcCompLit.
// typecheck.tcCompLit (and includes parts corresponding to tcStructLitKey).
func transformCompLit(n *ir.CompLitExpr) (res ir.Node) {
assert(n.Type() != nil && n.Typecheck() == 1)
lno := base.Pos
@ -1007,12 +1007,20 @@ func transformCompLit(n *ir.CompLitExpr) (res ir.Node) {
if id, ok := key.(*ir.Ident); ok && typecheck.DotImportRefs[id] != nil {
s = typecheck.Lookup(s.Name)
}
if types.IsExported(s.Name) && s.Pkg != types.LocalPkg {
// Exported field names should always have
// local pkg. We only need to do this
// adjustment for generic functions that are
// being transformed after being imported
// from another package.
s = typecheck.Lookup(s.Name)
}
// An OXDOT uses the Sym field to hold
// the field to the right of the dot,
// so s will be non-nil, but an OXDOT
// is never a valid struct literal key.
assert(!(s == nil || s.Pkg != types.LocalPkg || key.Op() == ir.OXDOT || s.IsBlank()))
assert(!(s == nil || key.Op() == ir.OXDOT || s.IsBlank()))
f := typecheck.Lookdot1(nil, s, t, t.Fields(), 0)
l := ir.NewStructKeyExpr(l.Pos(), f, kv.Value)
@ -1027,3 +1035,11 @@ func transformCompLit(n *ir.CompLitExpr) (res ir.Node) {
return n
}
// transformAddr corresponds to typecheck.tcAddr.
func transformAddr(n *ir.AddrExpr) {
switch n.X.Op() {
case ir.OARRAYLIT, ir.OMAPLIT, ir.OSLICELIT, ir.OSTRUCTLIT:
n.SetOp(ir.OPTRLIT)
}
}

View File

@ -1746,7 +1746,17 @@ func (w *exportWriter) expr(n ir.Node) {
}
w.localName(n)
// case OPACK, ONONAME:
case ir.ONONAME:
w.op(ir.ONONAME)
// This should only be for OKEY nodes in generic functions
s := n.Sym()
w.string(s.Name)
w.pkg(s.Pkg)
if go117ExportTypes {
w.typ(n.Type())
}
// case OPACK:
// should have been resolved by typechecking - handled by default case
case ir.OTYPE:
@ -1818,7 +1828,7 @@ func (w *exportWriter) expr(n ir.Node) {
w.typ(n.Type())
w.fieldList(n.List) // special handling of field names
case ir.OARRAYLIT, ir.OSLICELIT, ir.OMAPLIT:
case ir.OCOMPLIT, ir.OARRAYLIT, ir.OSLICELIT, ir.OMAPLIT:
n := n.(*ir.CompLitExpr)
if go117ExportTypes {
w.op(n.Op())

View File

@ -1368,7 +1368,11 @@ func (r *importReader) node() ir.Node {
return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()), r.fieldList())
case ir.OCOMPLIT:
return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()), r.exprList())
pos := r.pos()
t := r.typ()
n := ir.NewCompLitExpr(pos, ir.OCOMPLIT, ir.TypeNode(t), r.exprList())
n.SetType(t)
return n
case ir.OARRAYLIT, ir.OSLICELIT, ir.OMAPLIT:
if !go117ExportTypes {

View File

@ -0,0 +1,21 @@
// 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 main
func main() {
}
type C interface {
map[int]string
}
func f[A C]() A {
return A{
1: "a",
2: "b",
}
}