]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: consolidate testdata/examples test files
authorRobert Griesemer <gri@golang.org>
Thu, 18 Aug 2022 01:09:55 +0000 (18:09 -0700)
committerRobert Griesemer <gri@google.com>
Thu, 18 Aug 2022 21:54:10 +0000 (21:54 +0000)
Besides applying gofmt (which doesn't damage the tests in this case),
the ERROR comments in the types2 files now match the go/types files.
But because types2 still reports some errors are different positions,
the checking code now allows for some position discrepancy (similar
to what we do for other tests).

Except for an outstanding TODO in go/types/testdata/examples/types.go
the test files are now identical.

For #54511.

Change-Id: I5748e0f678d11c5c0bdf4fdf28bd04f0b11b5b23
Reviewed-on: https://go-review.googlesource.com/c/go/+/424674
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/check_test.go
src/cmd/compile/internal/types2/testdata/examples/functions.go
src/cmd/compile/internal/types2/testdata/examples/inference.go
src/cmd/compile/internal/types2/testdata/examples/types.go
src/cmd/compile/internal/types2/testdata/examples/typesets.go
src/go/types/testdata/examples/functions.go
src/go/types/testdata/examples/inference.go
src/go/types/testdata/examples/types.go
src/go/types/testdata/examples/typesets.go

index 845dcb6aa9e6c51af6d0f28c846a91fdba85b493..2b79f39d0a8c01acab2189c428a1ab2793be5703 100644 (file)
@@ -299,7 +299,7 @@ func TestManual(t *testing.T) {
 
 func TestCheck(t *testing.T)     { DefPredeclaredTestFuncs(); testDirFiles(t, "testdata/check", 55, false) } // TODO(gri) narrow column tolerance
 func TestSpec(t *testing.T)      { testDirFiles(t, "testdata/spec", 0, false) }
-func TestExamples(t *testing.T)  { testDirFiles(t, "testdata/examples", 0, false) }
+func TestExamples(t *testing.T)  { testDirFiles(t, "testdata/examples", 45, false) }
 func TestFixedbugs(t *testing.T) { testDirFiles(t, "testdata/fixedbugs", 0, false) }
 
 func testDirFiles(t *testing.T, dir string, colDelta uint, manual bool) {
index 0a308936285114c2580180d3be2c9ff2767c57dc..244c9dd22839986d551037ca27541f9765122dfc 100644 (file)
@@ -211,7 +211,7 @@ func _() {
 func h[] /* ERROR empty type parameter list */ () {}
 
 func _() {
-       h[ /* ERROR cannot index */ ] /* ERROR operand */ ()
+       h /* ERROR cannot index */ [] /* ERROR operand */ ()
 }
 
 // Parameterized functions must have a function body.
index e3d6bfb2124e23b63b9ba3b3d1f8c23a8d5cda79..23a3d81f3deb207bae719659014e0e8e00f0c7d3 100644 (file)
@@ -7,7 +7,7 @@
 package p
 
 type Ordered interface {
-       ~int|~float64|~string
+       ~int | ~float64 | ~string
 }
 
 func min[T Ordered](x, y T) T { panic(0) }
@@ -24,13 +24,13 @@ func _() {
        _ = min(x, 1)
        _ = min(x, 1.0)
        _ = min(1, 2)
-       _ = min(1, 2.3 /* ERROR default type float64 .* does not match */ )
+       _ = min(1, 2.3 /* ERROR default type float64 .* does not match */)
 
        var y float64
        _ = min(1, y)
        _ = min(1.2, y)
        _ = min(1.2, 3.4)
-       _ = min(1.2, 3 /* ERROR default type int .* does not match */ )
+       _ = min(1.2, 3 /* ERROR default type int .* does not match */)
 
        var s string
        _ = min(s, "foo")
@@ -51,10 +51,10 @@ func _() {
 
        // Provided type arguments always take precedence over
        // inferred types.
-       mixed[int, string](1.1 /* ERROR cannot use 1.1 */ , "", false)
+       mixed[int, string](1.1 /* ERROR cannot use 1.1 */, "", false)
 }
 
-func related1[Slice interface{~[]Elem}, Elem any](s Slice, e Elem) {}
+func related1[Slice interface{ ~[]Elem }, Elem any](s Slice, e Elem) {}
 
 func _() {
        // related1 can be called with explicit instantiation.
@@ -69,16 +69,16 @@ func _() {
 
        // A type argument inferred from another explicitly provided
        // type argument overrides whatever value argument type is given.
-       related1[[]string](ss, 0 /* ERROR cannot use 0 */ )
+       related1[[]string](ss, 0 /* ERROR cannot use 0 */)
 
        // A type argument may be inferred from a value argument
        // and then help infer another type argument via constraint
        // type inference.
        related1(si, 0)
-       related1(si, "foo" /* ERROR cannot use "foo" */ )
+       related1(si, "foo" /* ERROR cannot use "foo" */)
 }
 
-func related2[Elem any, Slice interface{[]Elem}](e Elem, s Slice) {}
+func related2[Elem any, Slice interface{ []Elem }](e Elem, s Slice) {}
 
 func _() {
        // related2 can be called with explicit instantiation.
@@ -97,7 +97,7 @@ func _() {
        // last.
        related2(1.2, []float64{})
        related2(1.0, []int{})
-       related2( /* ERROR does not implement */ float64(1.0), []int{}) // TODO(gri) fix error position
+       related2 /* ERROR does not implement */ (float64(1.0), []int{}) // TODO(gri) fix error position
 }
 
 type List[P any] []P
@@ -112,5 +112,5 @@ func _() {
        // The 2nd type argument cannot be inferred from the first
        // one because there's two possible choices: []Elem and
        // List[Elem].
-       related3[int]( /* ERROR cannot infer Slice */ )
+       related3 /* ERROR cannot infer Slice */ [int]()
 }
index ae9c0151d136ab76595d1ed6604cac2938449a75..052d168fc6fcc961bea24caa33f29ac4d158433c 100644 (file)
@@ -106,7 +106,7 @@ var _ = T /* ERROR cannot use generic type T */ (0)
 
 // In type context, generic (parameterized) types cannot be parenthesized before
 // being instantiated. See also NOTES entry from 12/4/2019.
-var _ (T /* ERROR cannot use generic type T */ )[ /* ERROR unexpected \[ */ int]
+var _ (T /* ERROR cannot use generic type T */ )[ /* ERROR unexpected \[|expected ';' */ int]
 
 // All types may be parameterized, including interfaces.
 type I1[T any] interface{
index 55ef02284b9829fd516af8a9d2995c944b6d7b70..a50beb9745328b9d3e4624fa719958c4377826b6 100644 (file)
@@ -9,13 +9,13 @@ package p
 
 // Constraint type sets of the form T, ~T, or A|B may omit the interface.
 type (
-       _[T int] struct{}
-       _[T ~int] struct{}
-       _[T int|string] struct{}
-       _[T ~int|~string] struct{}
+       _[T int]            struct{}
+       _[T ~int]           struct{}
+       _[T int | string]   struct{}
+       _[T ~int | ~string] struct{}
 )
 
-func min[T int|string](x, y T) T {
+func min[T int | string](x, y T) T {
        if x < y {
                return x
        }
@@ -45,9 +45,8 @@ func _() *int {
 
 // A type parameter may not be embedded in an interface;
 // so it can also not be used as a constraint.
-func _[A any, B A /* ERROR cannot use a type parameter as constraint */ ]() {}
-func _[A any, B, C A /* ERROR cannot use a type parameter as constraint */ ]() {}
-
+func _[A any, B A /* ERROR cannot use a type parameter as constraint */]()    {}
+func _[A any, B, C A /* ERROR cannot use a type parameter as constraint */]() {}
 
 // Error messages refer to the type constraint as it appears in the source.
 // (No implicit interface should be exposed.)
@@ -55,6 +54,6 @@ func _[T string](x T) T {
        return x /* ERROR constrained by string */ * x
 }
 
-func _[T int|string](x T) T {
+func _[T int | string](x T) T {
        return x /* ERROR constrained by int|string */ * x
 }
index 1d30075c7c72dfdc876ae0d296897226a5fedfca..244c9dd22839986d551037ca27541f9765122dfc 100644 (file)
@@ -183,6 +183,7 @@ func _() {
        var s1 string
        g3(nil, "1", myString("2"), "3")
        g3(& /* ERROR does not match */ s1, "1", myString("2"), "3")
+       _ = s1
 
        type myStruct struct{x int}
        var s2 myStruct
index e59a544660d15cec583cddc556f56bf8f2156712..23a3d81f3deb207bae719659014e0e8e00f0c7d3 100644 (file)
@@ -7,7 +7,7 @@
 package p
 
 type Ordered interface {
-       ~int|~float64|~string
+       ~int | ~float64 | ~string
 }
 
 func min[T Ordered](x, y T) T { panic(0) }
@@ -24,13 +24,13 @@ func _() {
        _ = min(x, 1)
        _ = min(x, 1.0)
        _ = min(1, 2)
-       _ = min(1, 2.3 /* ERROR default type float64 .* does not match */ )
+       _ = min(1, 2.3 /* ERROR default type float64 .* does not match */)
 
        var y float64
        _ = min(1, y)
        _ = min(1.2, y)
        _ = min(1.2, 3.4)
-       _ = min(1.2, 3 /* ERROR default type int .* does not match */ )
+       _ = min(1.2, 3 /* ERROR default type int .* does not match */)
 
        var s string
        _ = min(s, "foo")
@@ -51,10 +51,10 @@ func _() {
 
        // Provided type arguments always take precedence over
        // inferred types.
-       mixed[int, string](1.1 /* ERROR cannot use 1.1 */ , "", false)
+       mixed[int, string](1.1 /* ERROR cannot use 1.1 */, "", false)
 }
 
-func related1[Slice interface{~[]Elem}, Elem any](s Slice, e Elem) {}
+func related1[Slice interface{ ~[]Elem }, Elem any](s Slice, e Elem) {}
 
 func _() {
        // related1 can be called with explicit instantiation.
@@ -69,16 +69,16 @@ func _() {
 
        // A type argument inferred from another explicitly provided
        // type argument overrides whatever value argument type is given.
-       related1[[]string](ss, 0 /* ERROR cannot use 0 */ )
+       related1[[]string](ss, 0 /* ERROR cannot use 0 */)
 
        // A type argument may be inferred from a value argument
        // and then help infer another type argument via constraint
        // type inference.
        related1(si, 0)
-       related1(si, "foo" /* ERROR cannot use "foo" */ )
+       related1(si, "foo" /* ERROR cannot use "foo" */)
 }
 
-func related2[Elem any, Slice interface{[]Elem}](e Elem, s Slice) {}
+func related2[Elem any, Slice interface{ []Elem }](e Elem, s Slice) {}
 
 func _() {
        // related2 can be called with explicit instantiation.
index 1e83f898836f094f00310a7bfee29141d954becc..97e84993d548c1bb4ed957ebf8f5f325843b46f7 100644 (file)
@@ -106,7 +106,7 @@ var _ = T /* ERROR cannot use generic type T */ (0)
 
 // In type context, generic (parameterized) types cannot be parenthesized before
 // being instantiated. See also NOTES entry from 12/4/2019.
-var _ (T /* ERROR cannot use generic type T */ )[ /* ERROR expected ';' */ int]
+var _ (T /* ERROR cannot use generic type T */ )[ /* ERROR unexpected \[|expected ';' */ int]
 
 // All types may be parameterized, including interfaces.
 type I1[T any] interface{
@@ -145,21 +145,17 @@ func _() {
        x.m("foo")
 }
 
-// We accept parenthesized embedded struct fields so we can distinguish between
-// a named field with a parenthesized type foo (T) and an embedded parameterized
-// type (foo(T)), similarly to interface embedding.
-// They still need to be valid embedded types after the parentheses are stripped
-// (i.e., in contrast to interfaces, we cannot embed a struct literal). The name
-// of the embedded field is derived as before, after stripping parentheses.
-// (7/14/2020: See comment above. We probably will revert this generalized ability
-// if we go with [] for type parameters.)
 type _ struct {
+       // TODO(gri) The next 3 lines need to be adjusted to match
+       //           the corresponding types2 tests. This requires
+       //           a go/parser fix (issue #51655).
        int8
        *int16
-       *List[int]
+       List[int]
 
        int8 /* ERROR int8 redeclared */
-       * /* ERROR List redeclared */ List[int]
+       * /* ERROR int16 redeclared */ int16
+       List /* ERROR List redeclared */ [int]
 }
 
 // Issue #45639: We don't allow this anymore. Keep this code
@@ -169,27 +165,27 @@ type _ struct {
 // are type parameters. As with ordinary type definitions, the
 // types underlying properties are "inherited" but the methods
 // are not.
-//func _[T interface{ m(); ~int }]() {
-//     type L T
-//     var x L
-//
-//     // m is not defined on L (it is not "inherited" from
-//     // its underlying type).
-//     x.m /* ERROR x.m undefined */ ()
-//
-//     // But the properties of T, such that as that it supports
-//     // the operations of the types given by its type bound,
-//     // are also the properties of L.
-//     x++
-//     _ = x - x
-//
-//     // On the other hand, if we define a local alias for T,
-//     // that alias stands for T as expected.
-//     type A = T
-//     var y A
-//     y.m()
-//     _ = y < 0
-//}
+// func _[T interface{ m(); ~int }]() {
+//     type L T
+//     var x L
+// 
+//     // m is not defined on L (it is not "inherited" from
+//     // its underlying type).
+//     x.m /* ERROR x.m undefined */ ()
+// 
+//     // But the properties of T, such that as that it supports
+//     // the operations of the types given by its type bound,
+//     // are also the properties of L.
+//     x++
+//     _ = x - x
+// 
+//     // On the other hand, if we define a local alias for T,
+//     // that alias stands for T as expected.
+//     type A = T
+//     var y A
+//     y.m()
+//     _ = y < 0
+// }
 
 // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
 // // It is not permitted to declare a local type whose underlying
@@ -285,7 +281,7 @@ func _() {
 // (If a type set contains just a single const type, we could
 // allow it, but such type sets don't make much sense in the
 // first place.)
-func _[T interface {~int|~float64}]() {
+func _[T interface{~int|~float64}]() {
        // not valid
        const _ = T /* ERROR not constant */ (0)
        const _ T /* ERROR invalid constant type T */ = 1
index fcddf1f1a575599c6b7eeaf3fb68a020ea00175e..a50beb9745328b9d3e4624fa719958c4377826b6 100644 (file)
@@ -9,13 +9,13 @@ package p
 
 // Constraint type sets of the form T, ~T, or A|B may omit the interface.
 type (
-       _[T int] struct{}
-       _[T ~int] struct{}
-       _[T int|string] struct{}
-       _[T ~int|~string] struct{}
+       _[T int]            struct{}
+       _[T ~int]           struct{}
+       _[T int | string]   struct{}
+       _[T ~int | ~string] struct{}
 )
 
-func min[T int|string](x, y T) T {
+func min[T int | string](x, y T) T {
        if x < y {
                return x
        }
@@ -45,7 +45,8 @@ func _() *int {
 
 // A type parameter may not be embedded in an interface;
 // so it can also not be used as a constraint.
-func _[A any, B A /* ERROR cannot use a type parameter as constraint */ ]() {}
+func _[A any, B A /* ERROR cannot use a type parameter as constraint */]()    {}
+func _[A any, B, C A /* ERROR cannot use a type parameter as constraint */]() {}
 
 // Error messages refer to the type constraint as it appears in the source.
 // (No implicit interface should be exposed.)
@@ -53,6 +54,6 @@ func _[T string](x T) T {
        return x /* ERROR constrained by string */ * x
 }
 
-func _[T int|string](x T) T {
+func _[T int | string](x T) T {
        return x /* ERROR constrained by int|string */ * x
 }