]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: handle missing index like in go/parser
authorRobert Griesemer <gri@golang.org>
Wed, 17 Aug 2022 23:48:07 +0000 (16:48 -0700)
committerRobert Griesemer <gri@google.com>
Thu, 18 Aug 2022 21:46:39 +0000 (21:46 +0000)
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>
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/types2/testdata/examples/functions.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go
src/go/types/testdata/fixedbugs/issue39634.go

index 22b1816307710d9579002faa4351fdf4c4b96398..3bf9a5cb3b1799bccb0697378bf3f607017f7aa1 100644 (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
index d50f79d11f82f78f5849c5287a56be5493f82264..0a308936285114c2580180d3be2c9ff2767c57dc 100644 (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.
index b7d99f96c2f5fc19a566f39f5877e7a6034f634e..5ae647c59616dedb0f71f5d701b6097026fa0777 100644 (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) }
index ce84299a6159bc3934401211f6d7ef0a880f65a4..9df72f990ee9c2714e35331556e812a8e4bc402a 100644 (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