From: Robert Findley Date: Mon, 14 Feb 2022 21:33:39 +0000 (-0500) Subject: go/types, types2: add tests for literals in type parameter lists X-Git-Tag: go1.18rc1~16 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=76bd8ea9e133293d2a8253f01f66c1ca9add64e6;p=gostls13.git go/types, types2: add tests for literals in type parameter lists 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 Run-TryBot: Robert Findley TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky --- 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 index 0000000000..f289d2e52d --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49482.go2 @@ -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 index 0000000000..4c6579ed68 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue49482.go2 @@ -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" */