Source file src/go/parser/short_test.go

     1  // Copyright 2009 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  // This file contains test cases for short valid and invalid programs.
     6  
     7  package parser
     8  
     9  import "testing"
    10  
    11  var valids = []string{
    12  	"package p\n",
    13  	`package p;`,
    14  	`package p; import "fmt"; func f() { fmt.Println("Hello, World!") };`,
    15  	`package p; func f() { if f(T{}) {} };`,
    16  	`package p; func f() { _ = <-chan int(nil) };`,
    17  	`package p; func f() { _ = (<-chan int)(nil) };`,
    18  	`package p; func f() { _ = (<-chan <-chan int)(nil) };`,
    19  	`package p; func f() { _ = <-chan <-chan <-chan <-chan <-int(nil) };`,
    20  	`package p; func f(func() func() func());`,
    21  	`package p; func f(...T);`,
    22  	`package p; func f(float, ...int);`,
    23  	`package p; func f(x int, a ...int) { f(0, a...); f(1, a...,) };`,
    24  	`package p; func f(int,) {};`,
    25  	`package p; func f(...int,) {};`,
    26  	`package p; func f(x ...int,) {};`,
    27  	`package p; type T []int; var a []bool; func f() { if a[T{42}[0]] {} };`,
    28  	`package p; type T []int; func g(int) bool { return true }; func f() { if g(T{42}[0]) {} };`,
    29  	`package p; type T []int; func f() { for _ = range []int{T{42}[0]} {} };`,
    30  	`package p; var a = T{{1, 2}, {3, 4}}`,
    31  	`package p; func f() { select { case <- c: case c <- d: case c <- <- d: case <-c <- d: } };`,
    32  	`package p; func f() { select { case x := (<-c): } };`,
    33  	`package p; func f() { if ; true {} };`,
    34  	`package p; func f() { switch ; {} };`,
    35  	`package p; func f() { for _ = range "foo" + "bar" {} };`,
    36  	`package p; func f() { var s []int; g(s[:], s[i:], s[:j], s[i:j], s[i:j:k], s[:j:k]) };`,
    37  	`package p; var ( _ = (struct {*T}).m; _ = (interface {T}).m )`,
    38  	`package p; func ((T),) m() {}`,
    39  	`package p; func ((*T),) m() {}`,
    40  	`package p; func (*(T),) m() {}`,
    41  	`package p; func _(x []int) { for range x {} }`,
    42  	`package p; func _() { if [T{}.n]int{} {} }`,
    43  	`package p; func _() { map[int]int{}[0]++; map[int]int{}[0] += 1 }`,
    44  	`package p; func _(x interface{f()}) { interface{f()}(x).f() }`,
    45  	`package p; func _(x chan int) { chan int(x) <- 0 }`,
    46  	`package p; const (x = 0; y; z)`, // go.dev/issue/9639
    47  	`package p; var _ = map[P]int{P{}:0, {}:1}`,
    48  	`package p; var _ = map[*P]int{&P{}:0, {}:1}`,
    49  	`package p; type T = int`,
    50  	`package p; type (T = p.T; _ = struct{}; x = *T)`,
    51  	`package p; type T (*int)`,
    52  	`package p; type _ struct{ int }`,
    53  	`package p; type _ struct{ pkg.T }`,
    54  	`package p; type _ struct{ *pkg.T }`,
    55  	`package p; var _ = func()T(nil)`,
    56  	`package p; func _(T (P))`,
    57  	`package p; func _(T []E)`,
    58  	`package p; func _(T [P]E)`,
    59  	`package p; type _ [A+B]struct{}`,
    60  	`package p; func (R) _()`,
    61  	`package p; type _ struct{ f [n]E }`,
    62  	`package p; type _ struct{ f [a+b+c+d]E }`,
    63  	`package p; type I1 interface{}; type I2 interface{ I1 }`,
    64  
    65  	// generic code
    66  	`package p; type _ []T[int]`,
    67  	`package p; type T[P any] struct { P }`,
    68  	`package p; type T[P comparable] struct { P }`,
    69  	`package p; type T[P comparable[P]] struct { P }`,
    70  	`package p; type T[P1, P2 any] struct { P1; f []P2 }`,
    71  	`package p; func _[T any]()()`,
    72  	`package p; func _(T (P))`,
    73  	`package p; func f[A, B any](); func _() { _ = f[int, int] }`,
    74  	`package p; func _(x T[P1, P2, P3])`,
    75  	`package p; func _(x p.T[Q])`,
    76  	`package p; func _(p.T[Q])`,
    77  	`package p; type _[A interface{},] struct{}`,
    78  	`package p; type _[A interface{}] struct{}`,
    79  	`package p; type _[A,  B any,] struct{}`,
    80  	`package p; type _[A, B any] struct{}`,
    81  	`package p; type _[A any,] struct{}`,
    82  	`package p; type _[A any]struct{}`,
    83  	`package p; type _[A any] struct{ A }`,
    84  	`package p; func _[T any]()`,
    85  	`package p; func _[T any](x T)`,
    86  	`package p; func _[T1, T2 any](x T)`,
    87  	`package p; func _[A, B any](a A) B`,
    88  	`package p; func _[A, B C](a A) B`,
    89  	`package p; func _[A, B C[A, B]](a A) B`,
    90  
    91  	`package p; type _[A, B any] interface { _(a A) B }`,
    92  	`package p; type _[A, B C[A, B]] interface { _(a A) B }`,
    93  	`package p; func _[T1, T2 interface{}](x T1) T2`,
    94  	`package p; func _[T1 interface{ m() }, T2, T3 interface{}](x T1, y T3) T2`,
    95  	`package p; var _ = []T[int]{}`,
    96  	`package p; var _ = [10]T[int]{}`,
    97  	`package p; var _ = func()T[int]{}`,
    98  	`package p; var _ = map[T[int]]T[int]{}`,
    99  	`package p; var _ = chan T[int](x)`,
   100  	`package p; func _(_ T[P], T P) T[P]`,
   101  	`package p; var _ T[chan int]`,
   102  
   103  	`package p; func (_ R[P]) _(x T)`,
   104  	`package p; func (_ R[ P, Q]) _(x T)`,
   105  
   106  	`package p; func (R[P]) _()`,
   107  	`package p; func _(T[P])`,
   108  	`package p; func _(T[P1, P2, P3 ])`,
   109  	`package p; func _(T[P]) T[P]`,
   110  	`package p; type _ struct{ T[P]}`,
   111  	`package p; type _ struct{ T[struct{a, b, c int}] }`,
   112  	`package p; type _ interface{int|float32; bool; m(); string;}`,
   113  	`package p; type I1[T any] interface{}; type I2 interface{ I1[int] }`,
   114  	`package p; type I1[T any] interface{}; type I2[T any] interface{ I1[T] }`,
   115  	`package p; type _ interface { N[T] }`,
   116  	`package p; type T[P any] = T0`,
   117  }
   118  
   119  func TestValid(t *testing.T) {
   120  	for _, src := range valids {
   121  		checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
   122  	}
   123  }
   124  
   125  // TestSingle is useful to track down a problem with a single short test program.
   126  func TestSingle(t *testing.T) {
   127  	const src = `package p; var _ = T{}`
   128  	checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
   129  }
   130  
   131  var invalids = []string{
   132  	`foo /* ERROR "expected 'package'" */ !`,
   133  	`package p; func f() { if { /* ERROR "missing condition" */ } };`,
   134  	`package p; func f() { if ; /* ERROR "missing condition" */ {} };`,
   135  	`package p; func f() { if f(); /* ERROR "missing condition" */ {} };`,
   136  	`package p; func f() { if _ = range /* ERROR "expected operand" */ x; true {} };`,
   137  	`package p; func f() { switch _ /* ERROR "expected switch expression" */ = range x; true {} };`,
   138  	`package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`,
   139  	`package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`,
   140  	`package p; func f() { for ; _ /* ERROR "expected boolean or range expression" */ = range x ; {} };`,
   141  	`package p; func f() { switch t = /* ERROR "expected ':=', found '='" */ t.(type) {} };`,
   142  	`package p; func f() { switch t /* ERROR "expected switch expression" */ , t = t.(type) {} };`,
   143  	`package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`,
   144  	`package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
   145  	`package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
   146  	`package p; func f() { if x := g(); x /* ERROR "expected boolean expression" */ = 0 {}};`,
   147  	`package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
   148  	`package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
   149  	`package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
   150  	`package p; func f() { var s []int; _ = s[i:j: /* ERROR "final index required" */ ] };`,
   151  	`package p; func f() { var s []int; _ = s[i: /* ERROR "middle index required" */ :k] };`,
   152  	`package p; func f() { var s []int; _ = s[i: /* ERROR "middle index required" */ :] };`,
   153  	`package p; func f() { var s []int; _ = s[: /* ERROR "middle index required" */ :] };`,
   154  	`package p; func f() { var s []int; _ = s[: /* ERROR "middle index required" */ ::] };`,
   155  	`package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
   156  	`package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`,
   157  	`package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`,
   158  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`,
   159  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
   160  	`package p; func f() { go f /* ERROR HERE "must be function call" */ }`,
   161  	`package p; func f() { go ( /* ERROR "must not be parenthesized" */ f()) }`,
   162  	`package p; func f() { defer func() {} /* ERROR HERE "must be function call" */ }`,
   163  	`package p; func f() { defer ( /* ERROR "must not be parenthesized" */ f()) }`,
   164  	`package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`,
   165  	`package p; func _() (type /* ERROR "found 'type'" */ T)(T)`,
   166  	`package p; func (type /* ERROR "found 'type'" */ T)(T) _()`,
   167  	`package p; type _[A+B, /* ERROR "unexpected comma" */ ] int`,
   168  
   169  	`package p; type _ struct{ [ /* ERROR "expected '}', found '\['" */ ]byte }`,
   170  	`package p; type _ struct{ ( /* ERROR "cannot parenthesize embedded type" */ int) }`,
   171  	`package p; type _ struct{ ( /* ERROR "cannot parenthesize embedded type" */ []byte) }`,
   172  	`package p; type _ struct{ *( /* ERROR "cannot parenthesize embedded type" */ int) }`,
   173  	`package p; type _ struct{ *( /* ERROR "cannot parenthesize embedded type" */ []byte) }`,
   174  
   175  	// go.dev/issue/8656
   176  	`package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`,
   177  
   178  	// go.dev/issue/9639
   179  	`package p; var x, y, z; /* ERROR "expected type" */`,
   180  
   181  	// go.dev/issue/12437
   182  	`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ }{};`,
   183  	`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ y float }{};`,
   184  
   185  	// go.dev/issue/11611
   186  	`package p; type _ struct { int, } /* ERROR "expected 'IDENT', found '}'" */ ;`,
   187  	`package p; type _ struct { int, float } /* ERROR "expected type, found '}'" */ ;`,
   188  
   189  	// go.dev/issue/13475
   190  	`package p; func f() { if true {} else ; /* ERROR "expected if statement or block" */ }`,
   191  	`package p; func f() { if true {} else defer /* ERROR "expected if statement or block" */ f() }`,
   192  
   193  	// generic code
   194  	`package p; type _[_ any] int; var _ = T[] /* ERROR "expected operand" */ {}`,
   195  	`package p; var _ func[ /* ERROR "must have no type parameters" */ T any](T)`,
   196  	`package p; func _[]/* ERROR "empty type parameter list" */()`,
   197  
   198  	`package p; type _[A,] /* ERROR "missing type constraint" */ struct{ A }`,
   199  
   200  	`package p; func _[type /* ERROR "found 'type'" */ P, *Q interface{}]()`,
   201  
   202  	`package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B any](a A) B`,
   203  	`package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B C](a A) B`,
   204  	`package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B C[A, B]](a A) B`,
   205  
   206  	`package p; func(*T[e, e /* ERROR "e redeclared" */ ]) _()`,
   207  }
   208  
   209  func TestInvalid(t *testing.T) {
   210  	for _, src := range invalids {
   211  		checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
   212  	}
   213  }
   214  

View as plain text