]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: return ast.BadExpr for missing index operands
authorRob Findley <rfindley@google.com>
Mon, 22 Feb 2021 20:39:00 +0000 (15:39 -0500)
committerRobert Findley <rfindley@google.com>
Mon, 22 Feb 2021 23:43:15 +0000 (23:43 +0000)
The parser was returning the indexed operand when a slice or index or
instance expression was missing any index arguments (as in the
expression `a[]`). This can result in returning an *ast.Ident for the
LHS of the (invalid) assignment `a[] = ...` -- in this case parsing the
LHS as just `a`. Unfortunately, as the indexed operand `a` has already
been resolved, this results in a panic for duplicate resolution.

Fix this by instead returning an ast.BadExpr. This can suppress some
subsequent errors from the typechecker, but those errors may or may not
be correct anyway. Other interpretations, such as an *ast.IndexExpr with
bad or missing X, run into potential misinterpretations downstream (both
caused errors in go/types and/or gopls).

Fixes #44504

Change-Id: I5ca8bed4a1861bcc7db8898770b08937110981d4
Reviewed-on: https://go-review.googlesource.com/c/go/+/295151
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/parser/parser.go
src/go/parser/testdata/issue44504.src [new file with mode: 0644]
src/go/types/fixedbugs/issue39634.go2

index 41c3f2943e12c083a39e6fe7057a38f1a7286748..5c4cea8638d32a8a5266501ee62efbe2fd26b0c7 100644 (file)
@@ -1485,8 +1485,9 @@ func (p *parser) parseIndexOrSliceOrInstance(x ast.Expr) ast.Expr {
                // empty index, slice or index expressions are not permitted;
                // accept them for parsing tolerance, but complain
                p.errorExpected(p.pos, "operand")
+               rbrack := p.pos
                p.next()
-               return x
+               return &ast.BadExpr{From: x.Pos(), To: rbrack}
        }
        p.exprLev++
 
diff --git a/src/go/parser/testdata/issue44504.src b/src/go/parser/testdata/issue44504.src
new file mode 100644 (file)
index 0000000..7791f4a
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test case for issue 44504: panic due to duplicate resolution of slice/index
+// operands. We should not try to resolve a LHS expression with invalid syntax.
+
+package p
+
+func _() {
+  var items []bool
+  items[] /* ERROR "operand" */ = false
+}
index 249542d541190cd2a74bdcd56dc949ffc61480b8..78dee00383b4616fa7b61de8be204d6640086a21 100644 (file)
@@ -84,7 +84,7 @@ var x T25 /* ERROR without instantiation */ .m1
 
 // crash 26
 type T26 = interface{ F26[ /* ERROR methods cannot have type parameters */ Z any]() }
-func F26[Z any]() T26 { return F26 /* ERROR without instantiation */ /* ERROR missing method */ [] /* ERROR operand */ }
+func F26[Z any]() T26 { return F26[] /* ERROR operand */ }
 
 // crash 27
 func e27[T any]() interface{ x27 /* ERROR not a type */ }