1
0
mirror of https://github.com/golang/go synced 2024-09-29 20:14:29 -06:00

cmd/compile/internal/syntax: handle missing index like in go/parser

Instead of simply reporting an error but otherwise dropping the
index expression from the parse tree when an index is missing
(as in: x[]), create an index expression with a "bad expression"
as index. This matches the behavior of go/parser and permits the
use of the same test case for both parsers.

(It would be simpler to adjust the go/parser to match the syntax
parser's behavior, but that would break backward-compatibility
of the go/parser.)

Adjust the affected test files.

For #54511.

Change-Id: If7668973794604593e869a24b560da92e100b812
Reviewed-on: https://go-review.googlesource.com/c/go/+/424654
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2022-08-17 16:48:07 -07:00 committed by Robert Griesemer
parent 39ec97da15
commit 9485d4c1bd
4 changed files with 10 additions and 14 deletions

View File

@ -1111,20 +1111,19 @@ loop:
case _Lbrack:
p.next()
if p.tok == _Rbrack {
// invalid empty instance, slice or index expression; accept but complain
p.syntaxError("expecting operand")
p.next()
break
}
var i Expr
if p.tok != _Colon {
var comma bool
i, comma = p.typeList()
if p.tok == _Rbrack {
// invalid empty instance, slice or index expression; accept but complain
p.syntaxError("expecting operand")
i = p.badExpr()
} else {
i, comma = p.typeList()
}
if comma || p.tok == _Rbrack {
p.want(_Rbrack)
// x[i,] or x[i, j, ...]
// x[], x[i,] or x[i, j, ...]
t := new(IndexExpr)
t.pos = pos
t.X = x

View File

@ -211,7 +211,7 @@ func _() {
func h[] /* ERROR empty type parameter list */ () {}
func _() {
h[] /* ERROR operand */ ()
h[ /* ERROR cannot index */ ] /* ERROR operand */ ()
}
// Parameterized functions must have a function body.

View File

@ -83,7 +83,7 @@ var x T25 /* ERROR without instantiation */ .m1
// crash 26
type T26 = interface{ F26[ /* ERROR interface method must have no type parameters */ Z any]() }
func F26[Z any]() T26 { return F26 /* ERROR without instantiation */ [] /* ERROR operand */ }
func F26[Z any]() T26 { return F26[] /* ERROR operand */ }
// crash 27
func e27[T any]() interface{ x27 /* ERROR not a type */ } { panic(0) }

View File

@ -83,9 +83,6 @@ var x T25 /* ERROR without instantiation */ .m1
// crash 26
type T26 = interface{ F26[ /* ERROR interface method must have no type parameters */ Z any]() }
// The error messages on the line below differ from types2 because for backward
// compatibility go/parser must produce an IndexExpr with BadExpr index for the
// expression F26[].
func F26[Z any]() T26 { return F26[] /* ERROR operand */ }
// crash 27