Source file
src/go/parser/short_test.go
1
2
3
4
5
6
7 package parser
8
9 import (
10 "go/internal/typeparams"
11 "testing"
12 )
13
14 var valids = []string{
15 "package p\n",
16 `package p;`,
17 `package p; import "fmt"; func f() { fmt.Println("Hello, World!") };`,
18 `package p; func f() { if f(T{}) {} };`,
19 `package p; func f() { _ = <-chan int(nil) };`,
20 `package p; func f() { _ = (<-chan int)(nil) };`,
21 `package p; func f() { _ = (<-chan <-chan int)(nil) };`,
22 `package p; func f() { _ = <-chan <-chan <-chan <-chan <-int(nil) };`,
23 `package p; func f(func() func() func());`,
24 `package p; func f(...T);`,
25 `package p; func f(float, ...int);`,
26 `package p; func f(x int, a ...int) { f(0, a...); f(1, a...,) };`,
27 `package p; func f(int,) {};`,
28 `package p; func f(...int,) {};`,
29 `package p; func f(x ...int,) {};`,
30 `package p; type T []int; var a []bool; func f() { if a[T{42}[0]] {} };`,
31 `package p; type T []int; func g(int) bool { return true }; func f() { if g(T{42}[0]) {} };`,
32 `package p; type T []int; func f() { for _ = range []int{T{42}[0]} {} };`,
33 `package p; var a = T{{1, 2}, {3, 4}}`,
34 `package p; func f() { select { case <- c: case c <- d: case c <- <- d: case <-c <- d: } };`,
35 `package p; func f() { select { case x := (<-c): } };`,
36 `package p; func f() { if ; true {} };`,
37 `package p; func f() { switch ; {} };`,
38 `package p; func f() { for _ = range "foo" + "bar" {} };`,
39 `package p; func f() { var s []int; g(s[:], s[i:], s[:j], s[i:j], s[i:j:k], s[:j:k]) };`,
40 `package p; var ( _ = (struct {*T}).m; _ = (interface {T}).m )`,
41 `package p; func ((T),) m() {}`,
42 `package p; func ((*T),) m() {}`,
43 `package p; func (*(T),) m() {}`,
44 `package p; func _(x []int) { for range x {} }`,
45 `package p; func _() { if [T{}.n]int{} {} }`,
46 `package p; func _() { map[int]int{}[0]++; map[int]int{}[0] += 1 }`,
47 `package p; func _(x interface{f()}) { interface{f()}(x).f() }`,
48 `package p; func _(x chan int) { chan int(x) <- 0 }`,
49 `package p; const (x = 0; y; z)`,
50 `package p; var _ = map[P]int{P{}:0, {}:1}`,
51 `package p; var _ = map[*P]int{&P{}:0, {}:1}`,
52 `package p; type T = int`,
53 `package p; type (T = p.T; _ = struct{}; x = *T)`,
54 `package p; type T (*int)`,
55 `package p; type _ struct{ ((int)) }`,
56 `package p; type _ struct{ (*(int)) }`,
57 `package p; type _ struct{ ([]byte) }`,
58 `package p; var _ = func()T(nil)`,
59 `package p; func _(T (P))`,
60 `package p; func _(T []E)`,
61 `package p; func _(T [P]E)`,
62 `package p; type _ [A+B]struct{}`,
63 `package p; func (R) _()`,
64 `package p; type _ struct{ f [n]E }`,
65 `package p; type _ struct{ f [a+b+c+d]E }`,
66 `package p; type I1 interface{}; type I2 interface{ I1 }`,
67 }
68
69
70
71
72 var validWithTParamsOnly = []string{
73 `package p; type _ []T[ /* ERROR "expected ';', found '\['" */ int]`,
74 `package p; type T[P any /* ERROR "expected ']', found any" */ ] struct { P }`,
75 `package p; type T[P comparable /* ERROR "expected ']', found comparable" */ ] struct { P }`,
76 `package p; type T[P comparable /* ERROR "expected ']', found comparable" */ [P]] struct { P }`,
77 `package p; type T[P1, /* ERROR "unexpected comma" */ P2 any] struct { P1; f []P2 }`,
78 `package p; func _[ /* ERROR "expected '\(', found '\['" */ T any]()()`,
79 `package p; func _(T (P))`,
80 `package p; func f[ /* ERROR "expected '\(', found '\['" */ A, B any](); func _() { _ = f[int, int] }`,
81 `package p; func _(x /* ERROR "mixed named and unnamed parameters" */ T[P1, P2, P3])`,
82 `package p; func _(x /* ERROR "mixed named and unnamed parameters" */ p.T[Q])`,
83 `package p; func _(p.T[ /* ERROR "missing ',' in parameter list" */ Q])`,
84 `package p; type _[A interface /* ERROR "expected ']', found 'interface'" */ {},] struct{}`,
85 `package p; type _[A interface /* ERROR "expected ']', found 'interface'" */ {}] struct{}`,
86 `package p; type _[A, /* ERROR "unexpected comma" */ B any,] struct{}`,
87 `package p; type _[A, /* ERROR "unexpected comma" */ B any] struct{}`,
88 `package p; type _[A any /* ERROR "expected ']', found any" */,] struct{}`,
89 `package p; type _[A any /* ERROR "expected ']', found any" */ ]struct{}`,
90 `package p; type _[A any /* ERROR "expected ']', found any" */ ] struct{ A }`,
91 `package p; func _[ /* ERROR "expected '\(', found '\['" */ T any]()`,
92 `package p; func _[ /* ERROR "expected '\(', found '\['" */ T any](x T)`,
93 `package p; func _[ /* ERROR "expected '\(', found '\['" */ T1, T2 any](x T)`,
94 `package p; func _[ /* ERROR "expected '\(', found '\['" */ A, B any](a A) B`,
95 `package p; func _[ /* ERROR "expected '\(', found '\['" */ A, B C](a A) B`,
96 `package p; func _[ /* ERROR "expected '\(', found '\['" */ A, B C[A, B]](a A) B`,
97
98 `package p; type _[A, /* ERROR "unexpected comma" */ B any] interface { _(a A) B }`,
99 `package p; type _[A, /* ERROR "unexpected comma" */ B C[A, B]] interface { _(a A) B }`,
100 `package p; func _[ /* ERROR "expected '\(', found '\['" */ T1, T2 interface{}](x T1) T2`,
101 `package p; func _[ /* ERROR "expected '\(', found '\['" */ T1 interface{ m() }, T2, T3 interface{}](x T1, y T3) T2`,
102 `package p; var _ = [ /* ERROR "expected expression" */ ]T[int]{}`,
103 `package p; var _ = [ /* ERROR "expected expression" */ 10]T[int]{}`,
104 `package p; var _ = func /* ERROR "expected expression" */ ()T[int]{}`,
105 `package p; var _ = map /* ERROR "expected expression" */ [T[int]]T[int]{}`,
106 `package p; var _ = chan /* ERROR "expected expression" */ T[int](x)`,
107 `package p; func _(_ T[ /* ERROR "missing ',' in parameter list" */ P], T P) T[P]`,
108 `package p; var _ T[ /* ERROR "expected ';', found '\['" */ chan int]`,
109
110
111 `package p; func (_ /* ERROR "mixed named and unnamed parameters" */ R[P]) _(x T)`,
112 `package p; func (_ /* ERROR "mixed named and unnamed parameters" */ R[ P, Q]) _(x T)`,
113
114 `package p; func (R[P] /* ERROR "missing element type" */ ) _()`,
115 `package p; func _(T[P] /* ERROR "missing element type" */ )`,
116 `package p; func _(T[P1, /* ERROR "expected ']', found ','" */ P2, P3 ])`,
117 `package p; func _(T[P] /* ERROR "missing element type" */ ) T[P]`,
118 `package p; type _ struct{ T[P] /* ERROR "missing element type" */ }`,
119 `package p; type _ struct{ T[struct /* ERROR "expected expression" */ {a, b, c int}] }`,
120 `package p; type _ interface{int| /* ERROR "expected ';'" */ float32; bool; m(); string;}`,
121 `package p; type I1[T any /* ERROR "expected ']', found any" */ ] interface{}; type I2 interface{ I1[int] }`,
122 `package p; type I1[T any /* ERROR "expected ']', found any" */ ] interface{}; type I2[T any] interface{ I1[T] }`,
123 `package p; type _ interface { N[ /* ERROR "expected ';', found '\['" */ T] }`,
124 `package p; type T[P any /* ERROR "expected ']'" */ ] = T0`,
125 }
126
127 func TestValid(t *testing.T) {
128 t.Run("no tparams", func(t *testing.T) {
129 for _, src := range valids {
130 checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
131 }
132 })
133 t.Run("tparams", func(t *testing.T) {
134 for _, src := range valids {
135 checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
136 }
137 for _, src := range validWithTParamsOnly {
138 checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
139 }
140 })
141 }
142
143
144 func TestSingle(t *testing.T) {
145 const src = `package p; var _ = T{}`
146 checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
147 }
148
149 var invalids = []string{
150 `foo /* ERROR "expected 'package'" */ !`,
151 `package p; func f() { if { /* ERROR "missing condition" */ } };`,
152 `package p; func f() { if ; /* ERROR "missing condition" */ {} };`,
153 `package p; func f() { if f(); /* ERROR "missing condition" */ {} };`,
154 `package p; func f() { if _ = range /* ERROR "expected operand" */ x; true {} };`,
155 `package p; func f() { switch _ /* ERROR "expected switch expression" */ = range x; true {} };`,
156 `package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`,
157 `package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`,
158 `package p; func f() { for ; _ /* ERROR "expected boolean or range expression" */ = range x ; {} };`,
159 `package p; func f() { switch t = /* ERROR "expected ':=', found '='" */ t.(type) {} };`,
160 `package p; func f() { switch t /* ERROR "expected switch expression" */ , t = t.(type) {} };`,
161 `package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`,
162 `package p; var a = [ /* ERROR "expected expression" */ 1]int;`,
163 `package p; var a = [ /* ERROR "expected expression" */ ...]int;`,
164 `package p; var a = struct /* ERROR "expected expression" */ {}`,
165 `package p; var a = func /* ERROR "expected expression" */ ();`,
166 `package p; var a = interface /* ERROR "expected expression" */ {}`,
167 `package p; var a = [ /* ERROR "expected expression" */ ]int`,
168 `package p; var a = map /* ERROR "expected expression" */ [int]int`,
169 `package p; var a = chan /* ERROR "expected expression" */ int;`,
170 `package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`,
171 `package p; var a = ( /* ERROR "expected expression" */ []int);`,
172 `package p; var a = <- /* ERROR "expected expression" */ chan int;`,
173 `package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`,
174 `package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
175 `package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
176 `package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`,
177 `package p; func f() { if x := g(); x /* ERROR "expected boolean expression" */ = 0 {}};`,
178 `package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
179 `package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
180 `package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
181 `package p; func f() { var s []int; _ = s[i:j: /* ERROR "3rd index required" */ ] };`,
182 `package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :k] };`,
183 `package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :] };`,
184 `package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ :] };`,
185 `package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ ::] };`,
186 `package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
187 `package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`,
188 `package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`,
189 `package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`,
190 `package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
191 `package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`,
192 `package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`,
193 `package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`,
194 `package p; func _() (type /* ERROR "found 'type'" */ T)(T)`,
195 `package p; func (type /* ERROR "found 'type'" */ T)(T) _()`,
196 `package p; type _[A+B, /* ERROR "unexpected comma" */ ] int`,
197
198
199 `package p; var a = a[[]int:[ /* ERROR "expected expression" */ ]int];`,
200
201
202
203
204
205
206 `package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`,
207
208
209 `package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`,
210 `package p; const x /* ERROR "missing constant value" */ ;`,
211 `package p; const x /* ERROR "missing constant value" */ int;`,
212 `package p; const (x = 0; y; z /* ERROR "missing constant value" */ int);`,
213
214
215 `package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ }{};`,
216 `package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ y float }{};`,
217
218
219 `package p; type _ struct { int, } /* ERROR "expected 'IDENT', found '}'" */ ;`,
220 `package p; type _ struct { int, float } /* ERROR "expected type, found '}'" */ ;`,
221
222
223 `package p; func f() { if true {} else ; /* ERROR "expected if statement or block" */ }`,
224 `package p; func f() { if true {} else defer /* ERROR "expected if statement or block" */ f() }`,
225 }
226
227
228
229 var invalidNoTParamErrs = []string{
230 `package p; type _[_ any /* ERROR "expected ']', found any" */ ] int; var _ = T[]{}`,
231 `package p; type T[P any /* ERROR "expected ']', found any" */ ] = T0`,
232 `package p; var _ func[ /* ERROR "expected '\(', found '\['" */ T any](T)`,
233 `package p; func _[ /* ERROR "expected '\(', found '\['" */ ]()`,
234 `package p; type _[A, /* ERROR "unexpected comma" */] struct{ A }`,
235 `package p; func _[ /* ERROR "expected '\(', found '\['" */ type P, *Q interface{}]()`,
236
237 `package p; func (T) _[ /* ERROR "expected '\(', found '\['" */ A, B any](a A) B`,
238 `package p; func (T) _[ /* ERROR "expected '\(', found '\['" */ A, B C](a A) B`,
239 `package p; func (T) _[ /* ERROR "expected '\(', found '\['" */ A, B C[A, B]](a A) B`,
240
241 `package p; func(*T[ /* ERROR "missing ',' in parameter list" */ e, e]) _()`,
242 }
243
244
245
246 var invalidTParamErrs = []string{
247 `package p; type _[_ any] int; var _ = T[] /* ERROR "expected operand" */ {}`,
248 `package p; var _ func[ /* ERROR "must have no type parameters" */ T any](T)`,
249 `package p; func _[]/* ERROR "empty type parameter list" */()`,
250
251
252 `package p; type _[A /* ERROR "all type parameters must be named" */ ,] struct{ A }`,
253
254
255 `package p; func _[type /* ERROR "all type parameters must be named" */ P, *Q interface{}]()`,
256
257 `package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B any](a A) B`,
258 `package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B C](a A) B`,
259 `package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B C[A, B]](a A) B`,
260
261 `package p; func(*T[e, e /* ERROR "e redeclared" */ ]) _()`,
262 }
263
264 func TestInvalid(t *testing.T) {
265 t.Run("no tparams", func(t *testing.T) {
266 for _, src := range invalids {
267 checkErrors(t, src, src, DeclarationErrors|AllErrors|typeparams.DisallowParsing, true)
268 }
269 for _, src := range validWithTParamsOnly {
270 checkErrors(t, src, src, DeclarationErrors|AllErrors|typeparams.DisallowParsing, true)
271 }
272 for _, src := range invalidNoTParamErrs {
273 checkErrors(t, src, src, DeclarationErrors|AllErrors|typeparams.DisallowParsing, true)
274 }
275 })
276 t.Run("tparams", func(t *testing.T) {
277 for _, src := range invalids {
278 checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
279 }
280 for _, src := range invalidTParamErrs {
281 checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
282 }
283 })
284 }
285
View as plain text