1
0
mirror of https://github.com/golang/go synced 2024-11-26 05:17:58 -07:00

go/parser: resolve the type name when parsing a composite lit value

parsePrimaryExpr has to be careful to resolve identifiers used in
composite expressions when parsing in LHS mode. It missed the literal
type name.

Fixes #45136

Change-Id: I3e12f91e3ef5fdb43faa436cdf1240eb3293fe1a
Reviewed-on: https://go-review.googlesource.com/c/go/+/304451
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>
This commit is contained in:
Rob Findley 2021-03-22 22:27:25 -04:00 committed by Robert Findley
parent 0fdd371e6b
commit c3ec79bca9
2 changed files with 9 additions and 6 deletions

View File

@ -1804,6 +1804,11 @@ func (p *parser) parsePrimaryExpr(lhs bool) (x ast.Expr) {
p.error(t.Pos(), "cannot parenthesize type in composite literal")
// already progressed, no need to advance
}
if lhs {
// An error has already been reported above, but try to resolve the 'T'
// in (T){...} anyway.
p.resolve(t)
}
x = p.parseLiteralValue(x)
default:
return

View File

@ -12,17 +12,15 @@ func _ /* =@blank */ () {
var foo /* =@foo */ = "foo"
obj /* @obj */ ["foo"]
obj /* @obj */ .run()
// TODO(#45136): the next two statements are missing objects.
obj{
obj /* @obj */ {
name: foo /* @foo */,
}
obj{
obj /* @obj */ {
name: "bar",
}.run()
var _ /* @=blank4 */ = File{key: obj /* @obj */{}}
var _ /* @=blank3 */ = File{obj{}}
var _ /* @=blank4 */ = File{key: obj /* @obj */ {}}
var _ /* @=blank3 */ = File{obj /* @obj */ {}}
[]obj /* @obj */ {foo /* @foo */}
x /* =@x1 */ := obj /* @obj */{}