Skip to content

Commit 07b30a4

Browse files
committed
cmd/compile: delay transformAssign if lhs/rhs have typeparam
This also requires that we sometimes delay transformSelect(), if the assignments in the Comm part of the select have not been transformed. Fixes #48137 Change-Id: I163aa1f999d1e63616280dca807561b12b2aa779 Reviewed-on: https://go-review.googlesource.com/c/go/+/347915 Trust: Dan Scales <[email protected]> Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent c10b980 commit 07b30a4

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/cmd/compile/internal/noder/stencil.go

+3
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@ func (subst *subster) node(n ir.Node) ir.Node {
995995
case ir.OSEND:
996996
transformSend(m.(*ir.SendStmt))
997997

998+
case ir.OSELECT:
999+
transformSelect(m.(*ir.SelectStmt))
1000+
9981001
}
9991002
}
10001003

src/cmd/compile/internal/noder/stmt.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
8484
// to know the types of the left and right sides in various cases.
8585
delay := false
8686
for _, e := range lhs {
87-
if e.Typecheck() == 3 {
87+
if e.Type().HasTParam() || e.Typecheck() == 3 {
8888
delay = true
8989
break
9090
}
9191
}
9292
for _, e := range rhs {
93-
if e.Typecheck() == 3 {
93+
if e.Type().HasTParam() || e.Typecheck() == 3 {
9494
delay = true
9595
break
9696
}
@@ -145,8 +145,20 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
145145
return g.forStmt(stmt)
146146
case *syntax.SelectStmt:
147147
n := g.selectStmt(stmt)
148-
transformSelect(n.(*ir.SelectStmt))
149-
n.SetTypecheck(1)
148+
149+
delay := false
150+
for _, ncase := range n.(*ir.SelectStmt).Cases {
151+
if ncase.Comm != nil && ncase.Comm.Typecheck() == 3 {
152+
delay = true
153+
break
154+
}
155+
}
156+
if delay {
157+
n.SetTypecheck(3)
158+
} else {
159+
transformSelect(n.(*ir.SelectStmt))
160+
n.SetTypecheck(1)
161+
}
150162
return n
151163
case *syntax.SwitchStmt:
152164
return g.switchStmt(stmt)

test/typeparam/issue48137.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run -gcflags=-G=3
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
type Constraint[T any] interface {
10+
~func() T
11+
}
12+
13+
func Foo[T Constraint[T]]() T {
14+
var t T
15+
16+
t = func() T {
17+
return t
18+
}
19+
return t
20+
}
21+
22+
func main() {
23+
type Bar func() Bar
24+
Foo[Bar]()
25+
}

0 commit comments

Comments
 (0)