From 9485d4c1bd871be792d03c29b7902f6ac284ed27 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 17 Aug 2022 16:48:07 -0700 Subject: [PATCH] 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 TryBot-Result: Gopher Robot Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/syntax/parser.go | 17 ++++++++--------- .../types2/testdata/examples/functions.go | 2 +- .../types2/testdata/fixedbugs/issue39634.go | 2 +- src/go/types/testdata/fixedbugs/issue39634.go | 3 --- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go index 22b1816307..3bf9a5cb3b 100644 --- a/src/cmd/compile/internal/syntax/parser.go +++ b/src/cmd/compile/internal/syntax/parser.go @@ -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 diff --git a/src/cmd/compile/internal/types2/testdata/examples/functions.go b/src/cmd/compile/internal/types2/testdata/examples/functions.go index d50f79d11f..0a30893628 100644 --- a/src/cmd/compile/internal/types2/testdata/examples/functions.go +++ b/src/cmd/compile/internal/types2/testdata/examples/functions.go @@ -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. diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go index b7d99f96c2..5ae647c596 100644 --- a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go @@ -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) } diff --git a/src/go/types/testdata/fixedbugs/issue39634.go b/src/go/types/testdata/fixedbugs/issue39634.go index ce84299a61..9df72f990e 100644 --- a/src/go/types/testdata/fixedbugs/issue39634.go +++ b/src/go/types/testdata/fixedbugs/issue39634.go @@ -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 -- 2.50.0