| 1 |
// Copyright 2019 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package p |
| 6 |
|
| 7 |
type myInt int |
| 8 |
|
| 9 |
// Parameterized type declarations |
| 10 |
|
| 11 |
// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639). |
| 12 |
type T1[P any] P // ERROR "cannot use a type parameter as RHS in type declaration" |
| 13 |
|
| 14 |
type T2[P any] struct { |
| 15 |
f P |
| 16 |
g int // int should still be in scope chain |
| 17 |
} |
| 18 |
|
| 19 |
type List[P any] []P |
| 20 |
|
| 21 |
// Pending clarification of #46477 we disallow aliases |
| 22 |
// of generic types. |
| 23 |
type A2 = List // ERROR "cannot use generic type" |
| 24 |
var _ A2[int] |
| 25 |
var _ A2 |
| 26 |
|
| 27 |
type A3 = List[int] |
| 28 |
var _ A3 |
| 29 |
|
| 30 |
// Parameterized type instantiations |
| 31 |
|
| 32 |
var x int |
| 33 |
type _ x /* ERROR "not a type" */ [int] |
| 34 |
|
| 35 |
type _ int /* ERROR "not a generic type" */ [] // ERROR "expected type argument list" |
| 36 |
type _ myInt /* ERROR "not a generic type" */ [] // ERROR "expected type argument list" |
| 37 |
|
| 38 |
// TODO(gri) better error messages |
| 39 |
type _ T1[] // ERROR "expected type argument list" |
| 40 |
type _ T1[x /* ERROR "not a type" */ ] |
| 41 |
type _ T1 /* ERROR "too many type arguments for type T1: have 2, want 1" */ [int, float32] |
| 42 |
|
| 43 |
var _ T2[int] = T2[int]{} |
| 44 |
|
| 45 |
var _ List[int] = []int{1, 2, 3} |
| 46 |
var _ List[[]int] = [][]int{{1, 2, 3}} |
| 47 |
var _ List[List[List[int]]] |
| 48 |
|
| 49 |
// Parameterized types containing parameterized types |
| 50 |
|
| 51 |
type T3[P any] List[P] |
| 52 |
|
| 53 |
var _ T3[int] = T3[int](List[int]{1, 2, 3}) |
| 54 |
|
| 55 |
// Self-recursive generic types are not permitted |
| 56 |
|
| 57 |
type self1[P any] self1 /* ERROR "invalid recursive type" */ [P] |
| 58 |
type self2[P any] *self2[P] // this is ok |