]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: deal with blank nodes with typeparam type during stenciling
authorDan Scales <danscales@google.com>
Fri, 24 Sep 2021 15:57:06 +0000 (08:57 -0700)
committerDan Scales <danscales@google.com>
Sat, 25 Sep 2021 01:24:46 +0000 (01:24 +0000)
Deal correctly with a blank local variable with type param type. This is
a special case, because a blank local variable is not in the fn.Dcl
list. In this case, we must explicitly create a new blank node with the
correct substituted type, so we have correct types if the blank local
variable has an initializing assignment.

Fixes #48602

Change-Id: I903ea44b29934e180404e32800773b7309bf297b
Reviewed-on: https://go-review.googlesource.com/c/go/+/352117
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>

src/cmd/compile/internal/noder/stencil.go
test/typeparam/issue48602.go [new file with mode: 0644]

index cf8641d60e6806fc7728ed0f366558b617e2e80f..23f53bac0469b2655886d460f49b79497a6efc1e 100644 (file)
@@ -912,6 +912,12 @@ func (subst *subster) node(n ir.Node) ir.Node {
                        if v := subst.ts.Vars[x.(*ir.Name)]; v != nil {
                                return v
                        }
+                       if ir.IsBlank(x) {
+                               // Special case, because a blank local variable is
+                               // not in the fn.Dcl list.
+                               m := ir.NewNameAt(x.Pos(), ir.BlankNode.Sym())
+                               return typed(subst.ts.Typ(x.Type()), m)
+                       }
                        return x
                case ir.ONONAME:
                        // This handles the identifier in a type switch guard
diff --git a/test/typeparam/issue48602.go b/test/typeparam/issue48602.go
new file mode 100644 (file)
index 0000000..53ce20e
--- /dev/null
@@ -0,0 +1,25 @@
+// run -gcflags=-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
+
+type Iterator[T any] interface {
+       Iterate(fn T)
+}
+
+type IteratorFunc[T any] func(fn T)
+
+func (f IteratorFunc[T]) Iterate(fn T) {
+       f(fn)
+}
+
+func Foo[R any]() {
+       var _ Iterator[R] = IteratorFunc[R](nil)
+}
+
+func main() {
+       Foo[int]()
+}