Black Lives Matter. Support the Equal Justice Initiative.

Source file src/go/parser/short_test.go

Documentation: go/parser

     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 (
    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)`, // issue 9639
    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) }`, // disallowed by type-checker
    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  // validWithTParamsOnly holds source code examples that are valid if
    70  // parseTypeParams is set, but invalid if not. When checking with the
    71  // parseTypeParams set, errors are ignored.
    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 "expected ']', found ','" */ 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 "expected ']', found ','" */  B any,] struct{}`,
    87  	`package p; type _[A, /* ERROR "expected ']', found ','" */ 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  	`package p; func (T) _[ /* ERROR "expected '\(', found '\['" */ A, B any](a A) B`,
    98  	`package p; func (T) _[ /* ERROR "expected '\(', found '\['" */ A, B C](a A) B`,
    99  	`package p; func (T) _[ /* ERROR "expected '\(', found '\['" */ A, B C[A, B]](a A) B`,
   100  	`package p; type _[A, /* ERROR "expected ']', found ','" */ B any] interface { _(a A) B }`,
   101  	`package p; type _[A, /* ERROR "expected ']', found ','" */ B C[A, B]] interface { _(a A) B }`,
   102  	`package p; func _[ /* ERROR "expected '\(', found '\['" */ T1, T2 interface{}](x T1) T2`,
   103  	`package p; func _[ /* ERROR "expected '\(', found '\['" */ T1 interface{ m() }, T2, T3 interface{}](x T1, y T3) T2`,
   104  	`package p; var _ = [ /* ERROR "expected expression" */ ]T[int]{}`,
   105  	`package p; var _ = [ /* ERROR "expected expression" */ 10]T[int]{}`,
   106  	`package p; var _ = func /* ERROR "expected expression" */ ()T[int]{}`,
   107  	`package p; var _ = map /* ERROR "expected expression" */ [T[int]]T[int]{}`,
   108  	`package p; var _ = chan /* ERROR "expected expression" */ T[int](x)`,
   109  	`package p; func _(_ T[ /* ERROR "missing ',' in parameter list" */ P], T P) T[P]`,
   110  	`package p; var _ T[ /* ERROR "expected ';', found '\['" */ chan int]`,
   111  
   112  	// TODO(rfindley) this error message could be improved.
   113  	`package p; func (_ /* ERROR "mixed named and unnamed parameters" */ R[P]) _[T any](x T)`,
   114  	`package p; func (_ /* ERROR "mixed named and unnamed parameters" */ R[ P, Q]) _[T1, T2 any](x T)`,
   115  
   116  	`package p; func (R[P] /* ERROR "missing element type" */ ) _[T any]()`,
   117  	`package p; func _(T[P] /* ERROR "missing element type" */ )`,
   118  	`package p; func _(T[P1, /* ERROR "expected ']', found ','" */ P2, P3 ])`,
   119  	`package p; func _(T[P] /* ERROR "missing element type" */ ) T[P]`,
   120  	`package p; type _ struct{ T[P] /* ERROR "missing element type" */ }`,
   121  	`package p; type _ struct{ T[struct /* ERROR "expected expression" */ {a, b, c int}] }`,
   122  	`package p; type _ interface{type /* ERROR "expected '}', found 'type'" */ int}`,
   123  	`package p; type _ interface{type /* ERROR "expected '}', found 'type'" */ int, float32; type bool; m(); type string;}`,
   124  	`package p; type I1[T any /* ERROR "expected ']', found any" */ ] interface{}; type I2 interface{ I1[int] }`,
   125  	`package p; type I1[T any /* ERROR "expected ']', found any" */ ] interface{}; type I2[T any] interface{ I1[T] }`,
   126  	`package p; type _ interface { f[ /* ERROR "expected ';', found '\['" */ T any]() }`,
   127  }
   128  
   129  func TestValid(t *testing.T) {
   130  	t.Run("no tparams", func(t *testing.T) {
   131  		for _, src := range valids {
   132  			checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
   133  		}
   134  	})
   135  	t.Run("tparams", func(t *testing.T) {
   136  		if !typeparams.Enabled {
   137  			t.Skip("type params are not enabled")
   138  		}
   139  		for _, src := range valids {
   140  			checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
   141  		}
   142  		for _, src := range validWithTParamsOnly {
   143  			checkErrors(t, src, src, DeclarationErrors|AllErrors, false)
   144  		}
   145  	})
   146  }
   147  
   148  // TestSingle is useful to track down a problem with a single short test program.
   149  func TestSingle(t *testing.T) {
   150  	const src = `package p; var _ = T{}`
   151  	checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
   152  }
   153  
   154  var invalids = []string{
   155  	`foo /* ERROR "expected 'package'" */ !`,
   156  	`package p; func f() { if { /* ERROR "missing condition" */ } };`,
   157  	`package p; func f() { if ; /* ERROR "missing condition" */ {} };`,
   158  	`package p; func f() { if f(); /* ERROR "missing condition" */ {} };`,
   159  	`package p; func f() { if _ = range /* ERROR "expected operand" */ x; true {} };`,
   160  	`package p; func f() { switch _ /* ERROR "expected switch expression" */ = range x; true {} };`,
   161  	`package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`,
   162  	`package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`,
   163  	`package p; func f() { for ; _ /* ERROR "expected boolean or range expression" */ = range x ; {} };`,
   164  	`package p; func f() { switch t = /* ERROR "expected ':=', found '='" */ t.(type) {} };`,
   165  	`package p; func f() { switch t /* ERROR "expected switch expression" */ , t = t.(type) {} };`,
   166  	`package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`,
   167  	`package p; var a = [ /* ERROR "expected expression" */ 1]int;`,
   168  	`package p; var a = [ /* ERROR "expected expression" */ ...]int;`,
   169  	`package p; var a = struct /* ERROR "expected expression" */ {}`,
   170  	`package p; var a = func /* ERROR "expected expression" */ ();`,
   171  	`package p; var a = interface /* ERROR "expected expression" */ {}`,
   172  	`package p; var a = [ /* ERROR "expected expression" */ ]int`,
   173  	`package p; var a = map /* ERROR "expected expression" */ [int]int`,
   174  	`package p; var a = chan /* ERROR "expected expression" */ int;`,
   175  	`package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`,
   176  	`package p; var a = ( /* ERROR "expected expression" */ []int);`,
   177  	`package p; var a = <- /* ERROR "expected expression" */ chan int;`,
   178  	`package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`,
   179  	`package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
   180  	`package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
   181  	`package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`,
   182  	`package p; func f() { if x := g(); x /* ERROR "expected boolean expression" */ = 0 {}};`,
   183  	`package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
   184  	`package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
   185  	`package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
   186  	`package p; func f() { var s []int; _ = s[i:j: /* ERROR "3rd index required" */ ] };`,
   187  	`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :k] };`,
   188  	`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :] };`,
   189  	`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ :] };`,
   190  	`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ ::] };`,
   191  	`package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
   192  	`package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`,
   193  	`package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`,
   194  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`,
   195  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
   196  	`package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`,
   197  	`package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`,
   198  	`package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`,
   199  	`package p; func _() (type /* ERROR "found 'type'" */ T)(T)`,
   200  	`package p; func (type /* ERROR "found 'type'" */ T)(T) _()`,
   201  	`package p; type _[A+B, /* ERROR "expected ']'" */ ] int`,
   202  
   203  	// TODO: this error should be positioned on the ':'
   204  	`package p; var a = a[[]int:[ /* ERROR "expected expression" */ ]int];`,
   205  	// TODO: the compiler error is better here: "cannot parenthesize embedded type"
   206  	`package p; type I1 interface{}; type I2 interface{ (/* ERROR "expected '}', found '\('" */ I1) }`,
   207  
   208  	// issue 8656
   209  	`package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`,
   210  
   211  	// issue 9639
   212  	`package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`,
   213  	`package p; const x /* ERROR "missing constant value" */ ;`,
   214  	`package p; const x /* ERROR "missing constant value" */ int;`,
   215  	`package p; const (x = 0; y; z /* ERROR "missing constant value" */ int);`,
   216  
   217  	// issue 12437
   218  	`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ }{};`,
   219  	`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ y float }{};`,
   220  
   221  	// issue 11611
   222  	`package p; type _ struct { int, } /* ERROR "expected 'IDENT', found '}'" */ ;`,
   223  	`package p; type _ struct { int, float } /* ERROR "expected type, found '}'" */ ;`,
   224  
   225  	// issue 13475
   226  	`package p; func f() { if true {} else ; /* ERROR "expected if statement or block" */ }`,
   227  	`package p; func f() { if true {} else defer /* ERROR "expected if statement or block" */ f() }`,
   228  }
   229  
   230  // invalidNoTParamErrs holds invalid source code examples annotated with the
   231  // error messages produced when ParseTypeParams is not set.
   232  var invalidNoTParamErrs = []string{
   233  	`package p; type _[_ any /* ERROR "expected ']', found any" */ ] int; var _ = T[]{}`,
   234  	`package p; type T[P any /* ERROR "expected ']', found any" */ ] = T0`,
   235  	`package p; var _ func[ /* ERROR "expected '\(', found '\['" */ T any](T)`,
   236  	`package p; func _[ /* ERROR "expected '\(', found '\['" */ ]()`,
   237  	`package p; type _[A, /* ERROR "expected ']', found ','" */] struct{ A }`,
   238  	`package p; func _[ /* ERROR "expected '\(', found '\['" */ type P, *Q interface{}]()`,
   239  }
   240  
   241  // invalidTParamErrs holds invalid source code examples annotated with the
   242  // error messages produced when ParseTypeParams is set.
   243  var invalidTParamErrs = []string{
   244  	`package p; type _[_ any] int; var _ = T[] /* ERROR "expected operand" */ {}`,
   245  	`package p; type T[P any] = /* ERROR "cannot be alias" */ T0`,
   246  	`package p; var _ func[ /* ERROR "cannot have type parameters" */ T any](T)`,
   247  	`package p; func _[]/* ERROR "empty type parameter list" */()`,
   248  
   249  	// TODO(rfindley) a better location would be after the ']'
   250  	`package p; type _[A/* ERROR "all type parameters must be named" */,] struct{ A }`,
   251  
   252  	// TODO(rfindley) this error is confusing.
   253  	`package p; func _[type /* ERROR "all type parameters must be named" */P, *Q interface{}]()`,
   254  }
   255  
   256  func TestInvalid(t *testing.T) {
   257  	t.Run("no tparams", func(t *testing.T) {
   258  		for _, src := range invalids {
   259  			checkErrors(t, src, src, DeclarationErrors|AllErrors|typeparams.DisallowParsing, true)
   260  		}
   261  		for _, src := range validWithTParamsOnly {
   262  			checkErrors(t, src, src, DeclarationErrors|AllErrors|typeparams.DisallowParsing, true)
   263  		}
   264  		for _, src := range invalidNoTParamErrs {
   265  			checkErrors(t, src, src, DeclarationErrors|AllErrors|typeparams.DisallowParsing, true)
   266  		}
   267  	})
   268  	t.Run("tparams", func(t *testing.T) {
   269  		if !typeparams.Enabled {
   270  			t.Skip("type params are not enabled")
   271  		}
   272  		for _, src := range invalids {
   273  			checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
   274  		}
   275  		for _, src := range invalidTParamErrs {
   276  			checkErrors(t, src, src, DeclarationErrors|AllErrors, true)
   277  		}
   278  	})
   279  }
   280  

View as plain text