]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: error when using internal type declarations in generic functions
authorKeith Randall <khr@golang.org>
Mon, 15 Nov 2021 18:01:43 +0000 (10:01 -0800)
committerKeith Randall <khr@golang.org>
Mon, 15 Nov 2021 21:21:51 +0000 (21:21 +0000)
We hope to support this feature one day, but it doesn't work currently.
Issue a nice error message instead of having the compiler crash.

Update #47631

Change-Id: I0359411410acbaf9a5b9dbb988cd933de1bb8438
Reviewed-on: https://go-review.googlesource.com/c/go/+/364054
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
src/cmd/compile/internal/noder/stmt.go
test/run.go
test/typeparam/builtins.go
test/typeparam/issue47631.go [new file with mode: 0644]
test/typeparam/typelist.go

index aedb09e21ee74474385db3407e77f5dcba70291e..e329a591566ce4657a7bea6cc0d4dfc441d6c1b6 100644 (file)
@@ -46,6 +46,10 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
                n.SetTypecheck(1)
                return n
        case *syntax.DeclStmt:
+               if _, ok := stmt.DeclList[0].(*syntax.TypeDecl); ok && g.topFuncIsGeneric {
+                       // TODO: remove this restriction. See issue 47631.
+                       base.ErrorfAt(g.pos(stmt), "type declarations inside generic functions are not currently supported")
+               }
                n := ir.NewBlockStmt(g.pos(stmt), nil)
                g.decls(&n.List, stmt.DeclList)
                return n
index ad64304ec8aecf944de1c0fe2ebeacd7442b2365..bdc2f0a27776a4121d4dd35b2982ca90e7f7686c 100644 (file)
@@ -2186,6 +2186,7 @@ var unifiedFailures = setOf(
        "fixedbugs/issue42284.go", // prints "T(0) does not escape", but test expects "a.I(a.T(0)) does not escape"
        "fixedbugs/issue7921.go",  // prints "… escapes to heap", but test expects "string(…) escapes to heap"
        "typeparam/issue48538.go", // assertion failure, interprets struct key as closure variable
+       "typeparam/issue47631.go", // unified IR can handle local type declarations
 )
 
 func setOf(keys ...string) map[string]bool {
index 844cdae8ab3c7c590e035f907916cfb3529cdae4..73dda77e0ecb54d83fcd3a1b28a75f7042954a2f 100644 (file)
@@ -69,24 +69,25 @@ func m1[
        C1 interface{ chan int },
        C2 interface{ chan int | chan string },
 ]() {
-       type S0 []int
        _ = make([]int, 10)
-       _ = make(S0, 10)
+       _ = make(m1S0, 10)
        _ = make(S1, 10)
        _ = make(S1, 10, 20)
 
-       type M0 map[string]int
        _ = make(map[string]int)
-       _ = make(M0)
+       _ = make(m1M0)
        _ = make(M1)
        _ = make(M1, 10)
 
-       type C0 chan int
        _ = make(chan int)
-       _ = make(C0)
+       _ = make(m1C0)
        _ = make(C1)
        _ = make(C1, 10)
 }
+// TODO: put these type declarations back inside m1 when issue 47631 is fixed.
+type m1S0 []int
+type m1M0 map[string]int
+type m1C0 chan int
 
 // len/cap
 
diff --git a/test/typeparam/issue47631.go b/test/typeparam/issue47631.go
new file mode 100644 (file)
index 0000000..7f7cfa6
--- /dev/null
@@ -0,0 +1,34 @@
+// errorcheck -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.
+
+// TODO: one day we will support internal type declarations, at which time this test will be removed.
+
+package p
+
+func g[T any]() {
+       type U []T // ERROR "type declarations inside generic functions are not currently supported"
+       type V []int // ERROR "type declarations inside generic functions are not currently supported"
+}
+
+type S[T any] struct {
+}
+
+func (s S[T]) m() {
+       type U []T // ERROR "type declarations inside generic functions are not currently supported"
+       type V []int // ERROR "type declarations inside generic functions are not currently supported"
+}
+
+
+func f() {
+       type U []int // ok
+}
+
+type X struct {
+}
+
+func (x X) m() {
+       type U []int // ok
+}
index 8d6a228de555d6ae2dcfe773056130095c5f459a..34ea4b8aa99219199d13da19524dcb0994014180 100644 (file)
@@ -26,11 +26,12 @@ func at[T interface{ ~[]E }, E any](x T, i int) E {
 // type is itself, its "operational type" is defined by the type list in
 // the tybe bound, if any.
 func _[T interface{ ~int }](x T) {
-       type myint int
        var _ int = int(x)
        var _ T = 42
        var _ T = T(myint(42))
 }
+// TODO: put this type declaration back inside the above function when issue 47631 is fixed.
+type myint int
 
 // Indexing a generic type which has a structural contraints to be an array.
 func _[T interface{ ~[10]int }](x T) {