]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: add tests for literals in type parameter lists
authorRobert Findley <rfindley@google.com>
Mon, 14 Feb 2022 21:33:39 +0000 (16:33 -0500)
committerRobert Findley <rfindley@google.com>
Tue, 15 Feb 2022 14:54:27 +0000 (14:54 +0000)
Add tests that verify consistent behavior of go/types and types2 with
respect to potentially ambiguous type parameter lists.

For #49482

Change-Id: I3386d4fa3eb91f2a8ea0987372ca40a6962de886
Reviewed-on: https://go-review.googlesource.com/c/go/+/385756
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/types2/testdata/fixedbugs/issue49482.go2 [new file with mode: 0644]
src/go/types/testdata/fixedbugs/issue49482.go2 [new file with mode: 0644]

diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49482.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49482.go2
new file mode 100644 (file)
index 0000000..f289d2e
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2022 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.
+
+// This file is tested when running "go test -run Manual"
+// without source arguments. Use for one-off debugging.
+
+package p
+
+// The following is OK, per the special handling for type literals discussed in issue #49482.
+type _[P *struct{}] struct{}
+type _[P *int,] int
+type _[P (*int),] int
+
+const P = 2 // declare P to avoid noisy 'undeclared name' errors below.
+
+// The following parse as invalid array types.
+type _[P *int /* ERROR "int \(type\) is not an expression" */ ] int
+type _[P /* ERROR non-function P */ (*int)] int
+
+// The following should be parsed as a generic type, but is instead parsed as an array type.
+type _[P *struct /* ERROR "not an expression" */ {}| int /* ERROR "not an expression" */ ] struct{}
+
+// The following fails to parse, due to the '~'
+type _[P *struct /* ERROR "not an expression" */ {}|~ /* ERROR "unexpected ~" */ int] struct{}
diff --git a/src/go/types/testdata/fixedbugs/issue49482.go2 b/src/go/types/testdata/fixedbugs/issue49482.go2
new file mode 100644 (file)
index 0000000..4c6579e
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2022 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.
+
+// This file is tested when running "go test -run Manual"
+// without source arguments. Use for one-off debugging.
+
+package p
+
+// The following is OK, per the special handling for type literals discussed in issue #49482.
+type _[P *struct{}] struct{}
+type _[P *int,] int
+type _[P (*int),] int
+
+const P = 2 // declare P to avoid noisy 'undeclared name' errors below.
+
+// The following parse as invalid array types.
+type _[P *int /* ERROR "int \(type\) is not an expression" */ ] int
+type _[P /* ERROR non-function P */ (*int)] int
+
+// The following should be parsed as a generic type, but is instead parsed as an array type.
+type _[P *struct /* ERROR "expected expression" */ {}| int /* ERROR "not an expression" */ ] struct{}
+
+// The following fails to parse, due to the '~'
+type _[P *struct /* ERROR "expected expression" */ {}|~ /* ERROR "expected operand" */ int] struct{}
+
+// This is fragile: 'var' synchronizes the parser, and we absorb the rest of the errors.
+var /* ERROR "expected ']'" */ _ /* ERROR "value or type" */