1
0
mirror of https://github.com/golang/go synced 2024-11-16 21:24:53 -07:00

cmd/compile: fix delayTransform condition

The delayTransform only checks whether ir.CurFunc is generic function or
not. but when compiling a non-generic closure inside a generic function,
we also want to delay the transformation, which delayTransform fails to
detect, since when ir.CurFunc is the closure, not the top level function.

Instead, we must rely on irgen.topFuncIsGeneric field to decide whether
to delay the transformation, the same logic with what is being done for
not adding closure inside a generic function to g.target.Decls list.

Fixes #48609

Change-Id: I5bf5592027d112fe8b19c92eb906add424c46507
Reviewed-on: https://go-review.googlesource.com/c/go/+/351855
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
This commit is contained in:
Cuong Manh Le 2021-09-24 23:11:18 +07:00
parent c94543b85f
commit cfd0868fc3
5 changed files with 34 additions and 16 deletions

View File

@ -168,7 +168,7 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node {
if index.Op() != ir.OTYPE {
// This is just a normal index expression
n := Index(pos, g.typ(typ), g.expr(expr.X), index)
if !delayTransform() {
if !g.delayTransform() {
// transformIndex will modify n.Type() for OINDEXMAP.
transformIndex(n)
}
@ -206,7 +206,7 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node {
case *syntax.SliceExpr:
n := Slice(pos, g.typ(typ), g.expr(expr.X), g.expr(expr.Index[0]), g.expr(expr.Index[1]), g.expr(expr.Index[2]))
if !delayTransform() {
if !g.delayTransform() {
transformSlice(n)
}
return n
@ -218,7 +218,7 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node {
switch op := g.op(expr.Op, binOps[:]); op {
case ir.OEQ, ir.ONE, ir.OLT, ir.OLE, ir.OGT, ir.OGE:
n := Compare(pos, g.typ(typ), op, g.expr(expr.X), g.expr(expr.Y))
if !delayTransform() {
if !g.delayTransform() {
transformCompare(n)
}
return n
@ -228,7 +228,7 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node {
return typed(x.Type(), ir.NewLogicalExpr(pos, op, x, y))
default:
n := Binary(pos, op, g.typ(typ), g.expr(expr.X), g.expr(expr.Y))
if op == ir.OADD && !delayTransform() {
if op == ir.OADD && !g.delayTransform() {
return transformAdd(n)
}
return n

View File

@ -317,9 +317,3 @@ func IncDec(pos src.XPos, op ir.Op, x ir.Node) *ir.AssignOpStmt {
}
return ir.NewAssignOpStmt(pos, op, x, bl)
}
// delayTransform returns true if we should delay all transforms, because we are
// creating the nodes for a generic function/method.
func delayTransform() bool {
return ir.CurFunc != nil && ir.CurFunc.Type().HasTParam()
}

View File

@ -319,3 +319,9 @@ func (g *irgen) unhandled(what string, p poser) {
base.FatalfAt(g.pos(p), "unhandled %s: %T", what, p)
panic("unreachable")
}
// delayTransform returns true if we should delay all transforms, because we are
// creating the nodes for a generic function/method.
func (g *irgen) delayTransform() bool {
return g.topFuncIsGeneric
}

View File

@ -40,7 +40,7 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
return wrapname(g.pos(stmt.X), g.expr(stmt.X))
case *syntax.SendStmt:
n := ir.NewSendStmt(g.pos(stmt), g.expr(stmt.Chan), g.expr(stmt.Value))
if !delayTransform() {
if !g.delayTransform() {
transformSend(n)
}
n.SetTypecheck(1)
@ -62,7 +62,7 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
lhs := g.expr(stmt.Lhs)
n = ir.NewAssignOpStmt(g.pos(stmt), op, lhs, rhs)
}
if !delayTransform() {
if !g.delayTransform() {
transformAsOp(n)
}
n.SetTypecheck(1)
@ -77,7 +77,7 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
n := ir.NewAssignStmt(g.pos(stmt), lhs[0], rhs[0])
n.Def = initDefn(n, names)
if !delayTransform() {
if !g.delayTransform() {
lhs, rhs := []ir.Node{n.X}, []ir.Node{n.Y}
transformAssign(n, lhs, rhs)
n.X, n.Y = lhs[0], rhs[0]
@ -88,7 +88,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 !delayTransform() {
if !g.delayTransform() {
transformAssign(n, n.Lhs, n.Rhs)
}
n.SetTypecheck(1)
@ -100,7 +100,7 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
return ir.NewGoDeferStmt(g.pos(stmt), g.tokOp(int(stmt.Tok), callOps[:]), g.expr(stmt.Call))
case *syntax.ReturnStmt:
n := ir.NewReturnStmt(g.pos(stmt), g.exprList(stmt.Results))
if !delayTransform() {
if !g.delayTransform() {
transformReturn(n)
}
n.SetTypecheck(1)
@ -112,7 +112,7 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
case *syntax.SelectStmt:
n := g.selectStmt(stmt)
if !delayTransform() {
if !g.delayTransform() {
transformSelect(n.(*ir.SelectStmt))
}
n.SetTypecheck(1)

View File

@ -0,0 +1,18 @@
// 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
import "constraints"
func f[T constraints.Chan[E], E any](e E) T {
ch := make(T)
go func() {
defer close(ch)
ch <- e
}()
return ch
}