mirror of
https://github.com/golang/go
synced 2024-11-23 00:00:07 -07:00
cmd/compile: error when using internal type declarations in generic functions
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>
This commit is contained in:
parent
92655582d0
commit
560dc9712d
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
||||
|
34
test/typeparam/issue47631.go
Normal file
34
test/typeparam/issue47631.go
Normal file
@ -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
|
||||
}
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user