mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
go/parser: parse an ast.IndexExpr for a[]
To be consistent with Go 1.16, and to preserve as much information in the AST as possible, parse an ast.IndexExpr with BadExpr Index for the invalid expression a[]. A go/types test had to be adjusted to account for an additional error resulting from this change. We don't have a lot of test coverage for parser error recovery, so rather than write an ad-hoc test for this issue, add a new go/types test that checks that the indexed operand is used. Updates #46403 Change-Id: I21e6ff4179746aaa50e530d4091fded450e69824 Reviewed-on: https://go-review.googlesource.com/c/go/+/329791 Trust: Robert Findley <rfindley@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
1bd5a20e3c
commit
9afd158eb2
3 changed files with 18 additions and 2 deletions
|
|
@ -1302,7 +1302,12 @@ func (p *parser) parseIndexOrSliceOrInstance(x ast.Expr) ast.Expr {
|
||||||
p.errorExpected(p.pos, "operand")
|
p.errorExpected(p.pos, "operand")
|
||||||
rbrack := p.pos
|
rbrack := p.pos
|
||||||
p.next()
|
p.next()
|
||||||
return &ast.BadExpr{From: x.Pos(), To: rbrack}
|
return &ast.IndexExpr{
|
||||||
|
X: x,
|
||||||
|
Lbrack: lbrack,
|
||||||
|
Index: &ast.BadExpr{From: rbrack, To: rbrack},
|
||||||
|
Rbrack: rbrack,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p.exprLev++
|
p.exprLev++
|
||||||
|
|
||||||
|
|
|
||||||
2
src/go/types/testdata/examples/functions.go2
vendored
2
src/go/types/testdata/examples/functions.go2
vendored
|
|
@ -210,5 +210,5 @@ func _() {
|
||||||
func h[] /* ERROR empty type parameter list */ ()
|
func h[] /* ERROR empty type parameter list */ ()
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
h[] /* ERROR operand */ ()
|
h /* ERROR cannot index */ [] /* ERROR operand */ ()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
src/go/types/testdata/fixedbugs/issue46403.src
vendored
Normal file
11
src/go/types/testdata/fixedbugs/issue46403.src
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package issue46403
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
// a should be used, despite the parser error below.
|
||||||
|
var a []int
|
||||||
|
var _ = a[] // ERROR expected operand
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue