aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax/printer_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-03-03 18:33:45 -0800
committerRobert Griesemer <gri@golang.org>2021-03-04 22:20:29 +0000
commita99ff24a26939f30440dd0f06dce426ed5e638ee (patch)
tree130202788ccb64a05f53351f985780cc03d71b3c /src/cmd/compile/internal/syntax/printer_test.go
parent9d3718e834fcf5b602b84539364606445cfc8a1a (diff)
downloadgo-a99ff24a26939f30440dd0f06dce426ed5e638ee.tar.gz
go-a99ff24a26939f30440dd0f06dce426ed5e638ee.zip
cmd/compile/internal/syntax: print type parameters and type lists
types2 uses the syntax printer to print expressions (for tracing or error messages), so we need to (at least) print type lists in interfaces. While at it, also implement the printing of type parameter lists. Fixes #44766. Change-Id: I36a4a7152d9bef7251af264b5c7890aca88d8dc3 Reviewed-on: https://go-review.googlesource.com/c/go/+/298549 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax/printer_test.go')
-rw-r--r--src/cmd/compile/internal/syntax/printer_test.go37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/syntax/printer_test.go b/src/cmd/compile/internal/syntax/printer_test.go
index bcae815a46..4890327595 100644
--- a/src/cmd/compile/internal/syntax/printer_test.go
+++ b/src/cmd/compile/internal/syntax/printer_test.go
@@ -61,6 +61,21 @@ var stringTests = []string{
"package p",
"package p; type _ int; type T1 = struct{}; type ( _ *struct{}; T2 = float32 )",
+ // generic type declarations
+ "package p; type _[T any] struct{}",
+ "package p; type _[A, B, C interface{m()}] struct{}",
+ "package p; type _[T any, A, B, C interface{m()}, X, Y, Z interface{type int}] struct{}",
+
+ // generic function declarations
+ "package p; func _[T any]()",
+ "package p; func _[A, B, C interface{m()}]()",
+ "package p; func _[T any, A, B, C interface{m()}, X, Y, Z interface{type int}]()",
+
+ // methods with generic receiver types
+ "package p; func (R[T]) _()",
+ "package p; func (*R[A, B, C]) _()",
+ "package p; func (_ *R[A, B, C]) _()",
+
// channels
"package p; type _ chan chan int",
"package p; type _ chan (<-chan int)",
@@ -79,7 +94,7 @@ var stringTests = []string{
func TestPrintString(t *testing.T) {
for _, want := range stringTests {
- ast, err := Parse(nil, strings.NewReader(want), nil, nil, 0)
+ ast, err := Parse(nil, strings.NewReader(want), nil, nil, AllowGenerics)
if err != nil {
t.Error(err)
continue
@@ -116,6 +131,24 @@ var exprTests = [][2]string{
{"func(x int) complex128 { return 0 }", "func(x int) complex128 {…}"},
{"[]int{1, 2, 3}", "[]int{…}"},
+ // type expressions
+ dup("[1 << 10]byte"),
+ dup("[]int"),
+ dup("*int"),
+ dup("struct{x int}"),
+ dup("func()"),
+ dup("func(int, float32) string"),
+ dup("interface{m()}"),
+ dup("interface{m() string; n(x int)}"),
+ dup("interface{type int}"),
+ dup("interface{type int, float64, string}"),
+ dup("interface{type int; m()}"),
+ dup("interface{type int, float64, string; m() string; n(x int)}"),
+ dup("map[string]int"),
+ dup("chan E"),
+ dup("<-chan E"),
+ dup("chan<- E"),
+
// non-type expressions
dup("(x)"),
dup("x.f"),
@@ -172,7 +205,7 @@ var exprTests = [][2]string{
func TestShortString(t *testing.T) {
for _, test := range exprTests {
src := "package p; var _ = " + test[0]
- ast, err := Parse(nil, strings.NewReader(src), nil, nil, 0)
+ ast, err := Parse(nil, strings.NewReader(src), nil, nil, AllowGenerics)
if err != nil {
t.Errorf("%s: %s", test[0], err)
continue