]> Cypherpunks repositories - gostls13.git/commit
cmd/compile/internal/syntax: fix parsing of type parameter lists
authorRobert Griesemer <gri@golang.org>
Thu, 9 Dec 2021 04:32:29 +0000 (20:32 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 17 Dec 2021 00:14:49 +0000 (00:14 +0000)
commit33a1a93a92804205eca89e2bb113ca68c1de5a4f
treead64468f900b3e4deac34a609c8b1cac6b20ecd4
parent0c24038d22a83c0da5feb3d700b13445f47b24c9
cmd/compile/internal/syntax: fix parsing of type parameter lists

The parser cannot distinguish a type parameter list of the form

[P *T ] or
[P (T)]

where T is not a type literal from an array length specification
P*T (product) or P(T) (constant-valued function call) and thus
interprets these forms as the start of array types.

This ambiguity must be resolved explicitly by placing *T inside
an interface, adding a trailing comma, or by leaving parentheses
away where possible.

This CL adjusts the parser such that these forms are
interpreted as (the beginning) of type parameter lists
if the token after P*T or P(T) is a comma, or if T is
a type literal.

This CL also adjusts the printer to print a comma if
necessary to avoid this ambiguity, and adds additional
printer tests.

Fixes #49482

Change-Id: I36328e2a7d9439c39ba0349837c445542549e84e
Reviewed-on: https://go-review.googlesource.com/c/go/+/370774
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/printer.go
src/cmd/compile/internal/syntax/printer_test.go
src/cmd/compile/internal/syntax/testdata/issue49482.go2 [new file with mode: 0644]