1// Copyright 2020 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// Examples from the issue adjusted to match new [T any] syntax for type parameters. 6// Also, previously permitted empty type parameter lists and instantiations 7// are now syntax errors. 8// 9// The primary concern here is that these tests shouldn't crash the type checker. 10// The quality of the error messages is secondary as these are all pretty esoteric 11// or artificial test cases. 12 13package p 14 15// crash 1 16type nt1[_ any]interface{g /* ERROR "undefined" */ } 17type ph1[e nt1[e],g(d /* ERROR "undefined" */ )]s /* ERROR "undefined" */ 18func(*ph1[e,e /* ERROR "redeclared" */ ])h(d /* ERROR "undefined" */ ) 19 20// crash 2 21// Disabled: empty []'s are now syntax errors. This example leads to too many follow-on errors. 22// type Numeric2 interface{t2 /* ERROR "not a type" */ } 23// func t2[T Numeric2](s[]T){0 /* ERROR "not a type */ []{s /* ERROR cannot index" */ [0][0]}} 24 25// crash 3 26type t3 *interface{ t3.p /* ERROR "t3.p is not a type" */ } 27 28// crash 4 29type Numeric4 interface{t4 /* ERROR "not a type" */ } 30func t4[T Numeric4](s[]T){if( /* ERROR "non-boolean" */ 0){*s /* ERROR "cannot indirect" */ [0]}} 31 32// crash 7 33type foo7 interface { bar() } 34type x7[A any] struct{ foo7 } 35func main7() { var _ foo7 = x7[int]{} } 36 37// crash 8 38type foo8[A any] interface { ~A /* ERROR "cannot be a type parameter" */ } 39func bar8[A foo8[A]](a A) {} 40 41// crash 9 42type foo9[A any] interface { foo9 /* ERROR "invalid recursive type" */ [A] } 43func _() { var _ = new(foo9[int]) } 44 45// crash 12 46var u, i [func /* ERROR "used as value" */ /* ERROR "used as value" */ (u /* ERROR "u is not a type" */ /* ERROR "u is not a type" */ , c /* ERROR "undefined" */ /* ERROR "undefined" */ ) {}(0, len /* ERROR "must be called" */ /* ERROR "must be called" */ )]c /* ERROR "undefined" */ /* ERROR "undefined" */ 47 48// crash 15 49func y15() { var a /* ERROR "declared and not used" */ interface{ p() } = G15[string]{} } 50type G15[X any] s /* ERROR "undefined" */ 51func (G15 /* ERRORx `generic type .* without instantiation` */ ) p() 52 53// crash 16 54type Foo16[T any] r16 /* ERROR "not a type" */ 55func r16[T any]() Foo16[Foo16[T]] { panic(0) } 56 57// crash 17 58type Y17 interface{ c() } 59type Z17 interface { 60 c() Y17 61 Y17 /* ERROR "duplicate method" */ 62} 63func F17[T Z17](T) {} 64 65// crash 18 66type o18[T any] []func(_ o18[[]_ /* ERROR "cannot use _" */ ]) 67 68// crash 19 69type Z19 [][[]Z19{}[0][0]]c19 /* ERROR "undefined" */ 70 71// crash 20 72type Z20 /* ERROR "invalid recursive type" */ interface{ Z20 } 73func F20[t Z20]() { F20(t /* ERROR "invalid composite literal type" */ /* ERROR "too many arguments in call to F20\n\thave (unknown type)\n\twant ()" */ {}) } 74 75// crash 21 76type Z21 /* ERROR "invalid recursive type" */ interface{ Z21 } 77func F21[T Z21]() { ( /* ERROR "not used" */ F21[Z21]) } 78 79// crash 24 80type T24[P any] P // ERROR "cannot use a type parameter as RHS in type declaration" 81func (r T24[P]) m() { T24 /* ERROR "without instantiation" */ .m() } 82 83// crash 25 84type T25[A any] int 85func (t T25[A]) m1() {} 86var x T25 /* ERROR "without instantiation" */ .m1 87 88// crash 26 89type T26 = interface{ F26[ /* ERROR "interface method must have no type parameters" */ Z any]() } 90func F26[Z any]() T26 { return F26[] /* ERROR "operand" */ } 91 92// crash 27 93func e27[T any]() interface{ x27 /* ERROR "not a type" */ } { panic(0) } 94func x27() { e27 /* ERROR "cannot infer T" */ () } 95