From 717056da1a0086c5d1d1b27231a385f77ebc61d7 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 29 Mar 2022 16:58:49 -0700 Subject: [PATCH] cmd/compile/internal/syntax: use .go suffix for test files (cleanup) - Use .go suffix for test files as go fmt doesn't descend into testdata directories. - Move test files from testdata/go2 into testdata directory. - Delete some test files that contained type-checker ERROR markers that were ignored by the TestParseGo2 test but would be considered by the TestSyntaxErrors test if the files were moved unchanged into the testdata directory. - Remove one (type checker) ERROR marker in testdata/slices.go to make it pass the syntax error tests. - Delete TestParseGo2 test. There's enough coverage with the existing TestSyntaxErrors test. - Add missing copyright notice to testdata/chans.go and gofmt the file. Change-Id: I449913fe1bd2119987ba33f7152e5e4ba5f3fe31 Reviewed-on: https://go-review.googlesource.com/c/go/+/396518 Trust: Robert Griesemer Reviewed-by: Ian Lance Taylor --- src/cmd/compile/internal/syntax/error_test.go | 2 +- .../compile/internal/syntax/parser_test.go | 14 - .../testdata/{go2/chans.go2 => chans.go} | 8 +- .../internal/syntax/testdata/go2/typeinst.go2 | 60 --- .../syntax/testdata/go2/typeinst2.go2 | 232 --------- .../syntax/testdata/go2/typeparams.go2 | 451 ------------------ .../testdata/{interface.go2 => interface.go} | 0 .../{issue20789.src => issue20789.go} | 0 .../{issue23385.src => issue23385.go} | 0 .../{issue23434.src => issue23434.go} | 0 .../{issue31092.src => issue31092.go} | 0 .../{issue43527.go2 => issue43527.go} | 0 .../{issue43674.src => issue43674.go} | 0 .../{issue46558.src => issue46558.go} | 0 .../{issue47704.go2 => issue47704.go} | 0 .../{issue47704.src => issue47704.go117} | 0 .../{issue48382.go2 => issue48382.go} | 0 .../{issue49482.go2 => issue49482.go} | 0 .../testdata/{go2/linalg.go2 => linalg.go} | 0 .../syntax/testdata/{go2/map.go2 => map.go} | 3 +- .../syntax/testdata/{go2/map2.go2 => map2.go} | 0 .../syntax/testdata/{sample.src => sample.go} | 0 .../testdata/{go2/slices.go2 => slices.go} | 2 +- .../{go2/smoketest.go2 => smoketest.go} | 6 - .../testdata/{tparams.go2 => tparams.go} | 0 .../testdata/{typeset.go2 => typeset.go} | 0 26 files changed, 9 insertions(+), 769 deletions(-) rename src/cmd/compile/internal/syntax/testdata/{go2/chans.go2 => chans.go} (89%) delete mode 100644 src/cmd/compile/internal/syntax/testdata/go2/typeinst.go2 delete mode 100644 src/cmd/compile/internal/syntax/testdata/go2/typeinst2.go2 delete mode 100644 src/cmd/compile/internal/syntax/testdata/go2/typeparams.go2 rename src/cmd/compile/internal/syntax/testdata/{interface.go2 => interface.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue20789.src => issue20789.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue23385.src => issue23385.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue23434.src => issue23434.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue31092.src => issue31092.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue43527.go2 => issue43527.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue43674.src => issue43674.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue46558.src => issue46558.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue47704.go2 => issue47704.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue47704.src => issue47704.go117} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue48382.go2 => issue48382.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{issue49482.go2 => issue49482.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{go2/linalg.go2 => linalg.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{go2/map.go2 => map.go} (97%) rename src/cmd/compile/internal/syntax/testdata/{go2/map2.go2 => map2.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{sample.src => sample.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{go2/slices.go2 => slices.go} (94%) rename src/cmd/compile/internal/syntax/testdata/{go2/smoketest.go2 => smoketest.go} (92%) rename src/cmd/compile/internal/syntax/testdata/{tparams.go2 => tparams.go} (100%) rename src/cmd/compile/internal/syntax/testdata/{typeset.go2 => typeset.go} (100%) diff --git a/src/cmd/compile/internal/syntax/error_test.go b/src/cmd/compile/internal/syntax/error_test.go index d87e8eaee3..0900bec6ec 100644 --- a/src/cmd/compile/internal/syntax/error_test.go +++ b/src/cmd/compile/internal/syntax/error_test.go @@ -129,7 +129,7 @@ func testSyntaxErrors(t *testing.T, filename string) { defer f.Close() var mode Mode - if strings.HasSuffix(filename, ".go2") { + if !strings.HasSuffix(filename, ".go117") { mode = AllowGenerics } ParseFile(filename, func(err error) { diff --git a/src/cmd/compile/internal/syntax/parser_test.go b/src/cmd/compile/internal/syntax/parser_test.go index 66690a527a..ddddedc5ec 100644 --- a/src/cmd/compile/internal/syntax/parser_test.go +++ b/src/cmd/compile/internal/syntax/parser_test.go @@ -38,20 +38,6 @@ func TestVerify(t *testing.T) { verifyPrint(t, *src_, ast) } -func TestParseGo2(t *testing.T) { - dir := filepath.Join(testdata, "go2") - list, err := ioutil.ReadDir(dir) - if err != nil { - t.Fatal(err) - } - for _, fi := range list { - name := fi.Name() - if !fi.IsDir() && !strings.HasPrefix(name, ".") { - ParseFile(filepath.Join(dir, name), func(err error) { t.Error(err) }, nil, AllowGenerics|AllowMethodTypeParams) - } - } -} - func TestStdLib(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") diff --git a/src/cmd/compile/internal/syntax/testdata/go2/chans.go2 b/src/cmd/compile/internal/syntax/testdata/chans.go similarity index 89% rename from src/cmd/compile/internal/syntax/testdata/go2/chans.go2 rename to src/cmd/compile/internal/syntax/testdata/chans.go index fad2bcec9d..d4c4207a4c 100644 --- a/src/cmd/compile/internal/syntax/testdata/go2/chans.go2 +++ b/src/cmd/compile/internal/syntax/testdata/chans.go @@ -1,3 +1,7 @@ +// 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. + package chans import "runtime" @@ -22,7 +26,7 @@ func Ranger[T any]() (*Sender[T], *Receiver[T]) { // A sender is used to send values to a Receiver. type Sender[T any] struct { values chan<- T - done <-chan bool + done <-chan bool } // Send sends a value to the receiver. It returns whether any more @@ -45,7 +49,7 @@ func (s *Sender[T]) Close() { // A Receiver receives values from a Sender. type Receiver[T any] struct { values <-chan T - done chan<- bool + done chan<- bool } // Next returns the next value from the channel. The bool result diff --git a/src/cmd/compile/internal/syntax/testdata/go2/typeinst.go2 b/src/cmd/compile/internal/syntax/testdata/go2/typeinst.go2 deleted file mode 100644 index a422d5e568..0000000000 --- a/src/cmd/compile/internal/syntax/testdata/go2/typeinst.go2 +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2019 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. - -package p - -type myInt int - -// Parameterized type declarations - -type T1[P any] P - -type T2[P any] struct { - f P - g int // int should still be in scope chain -} - -type List[P any] []P - -// Alias type declarations cannot have type parameters. Syntax error. -// TODO(gri) Disabled for now as we don't check syntax error here. -// type A1[P any] = /* ERROR cannot be alias */ P - -// But an alias may refer to a generic, uninstantiated type. -type A2 = List -var _ A2[int] -var _ A2 /* ERROR without instantiation */ - -type A3 = List[int] -var _ A3 - -// Parameterized type instantiations - -var x int -type _ x /* ERROR not a type */ [int] - -type _ int /* ERROR not a generic type */ [int] -type _ myInt /* ERROR not a generic type */ [int] - -// TODO(gri) better error messages -type _ T1[int] -type _ T1[x /* ERROR not a type */ ] -type _ T1 /* ERROR got 2 arguments but 1 type parameters */ [int, float32] - -var _ T2[int] = T2[int]{} - -var _ List[int] = []int{1, 2, 3} -var _ List[[]int] = [][]int{{1, 2, 3}} -var _ List[List[List[int]]] - -// Parameterized types containing parameterized types - -type T3[P any] List[P] - -var _ T3[int] = T3[int](List[int]{1, 2, 3}) - -// Self-recursive generic types are not permitted - -type self1[P any] self1 /* ERROR illegal cycle */ [P] -type self2[P any] *self2[P] // this is ok diff --git a/src/cmd/compile/internal/syntax/testdata/go2/typeinst2.go2 b/src/cmd/compile/internal/syntax/testdata/go2/typeinst2.go2 deleted file mode 100644 index 76b8d5591f..0000000000 --- a/src/cmd/compile/internal/syntax/testdata/go2/typeinst2.go2 +++ /dev/null @@ -1,232 +0,0 @@ -// Copyright 2019 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. - -package p - -type List[E any] []E -var _ List[List[List[int]]] -var _ List[List[List[int]]] = []List[List[int]]{} - -type ( - T1[P1 any] struct { - f1 T2[P1, float32] - } - - T2[P2, P3 any] struct { - f2 P2 - f3 P3 - } -) - -func _() { - var x1 T1[int] - var x2 T2[int, float32] - - x1.f1.f2 = 0 - x1.f1 = x2 -} - -type T3[P any] T1[T2[P, P]] - -func _() { - var x1 T3[int] - var x2 T2[int, int] - x1.f1.f2 = x2 -} - -func f[P any] (x P) List[P] { - return List[P]{x} -} - -var ( - _ []int = f(0) - _ []float32 = f[float32](10) - _ List[complex128] = f(1i) - _ []List[int] = f(List[int]{}) - _ List[List[int]] = []List[int]{} - _ = []List[int]{} -) - -// Parameterized types with methods - -func (l List[E]) Head() (_ E, _ bool) { - if len(l) > 0 { - return l[0], true - } - return -} - -// A test case for instantiating types with other types (extracted from map.go2) - -type Pair[K any] struct { - key K -} - -type Receiver[T any] struct { - values T -} - -type Iterator[K any] struct { - r Receiver[Pair[K]] -} - -func Values [T any] (r Receiver[T]) T { - return r.values -} - -func (it Iterator[K]) Next() K { - return Values[Pair[K]](it.r).key -} - -// A more complex test case testing type bounds (extracted from linalg.go2 and reduced to essence) - -type NumericAbs[T any] interface { - Abs() T -} - -func AbsDifference[T NumericAbs[T]](x T) - -type OrderedAbs[T any] T - -func (a OrderedAbs[T]) Abs() OrderedAbs[T] - -func OrderedAbsDifference[T any](x T) { - AbsDifference(OrderedAbs[T](x)) -} - -// same code, reduced to essence - -func g[P interface{ m() P }](x P) - -type T4[P any] P - -func (_ T4[P]) m() T4[P] - -func _[Q any](x Q) { - g(T4[Q](x)) -} - -// Another test case that caused problems in the past - -type T5[_ interface { a() }, _ interface{}] struct{} - -type A[P any] struct{ x P } - -func (_ A[P]) a() {} - -var _ T5[A[int], int] - -// Invoking methods with parameterized receiver types uses -// type inference to determine the actual type arguments matching -// the receiver type parameters from the actual receiver argument. -// Go does implicit address-taking and dereferenciation depending -// on the actual receiver and the method's receiver type. To make -// type inference work, the type-checker matches "pointer-ness" -// of the actual receiver and the method's receiver type. -// The following code tests this mechanism. - -type R1[A any] struct{} -func (_ R1[A]) vm() -func (_ *R1[A]) pm() - -func _[T any](r R1[T], p *R1[T]) { - r.vm() - r.pm() - p.vm() - p.pm() -} - -type R2[A, B any] struct{} -func (_ R2[A, B]) vm() -func (_ *R2[A, B]) pm() - -func _[T any](r R2[T, int], p *R2[string, T]) { - r.vm() - r.pm() - p.vm() - p.pm() -} - -// Interface type constraints can contain any type, incl. *Named types. -// Verify that we use the underlying type to compute the operational type. -type MyInt int -func add1[T interface{ ~MyInt }](x T) T { - return x + 1 -} - -type MyString string -func double[T interface{ ~MyInt | ~MyString }](x T) T { - return x + x -} - -// Embedding of interfaces with type constraints leads to interfaces -// with type constraints that are the intersection of the embedded -// type constraints. - -type E0 interface { - ~int | ~bool | ~string -} - -type E1 interface { - ~int | ~float64 | ~string -} - -type E2 interface { - ~float64 -} - -type I0 interface { - E0 -} - -func f0[T I0]() -var _ = f0[int] -var _ = f0[bool] -var _ = f0[string] -var _ = f0[float64 /* ERROR does not satisfy I0 */ ] - -type I01 interface { - E0 - E1 -} - -func f01[T I01]() -var _ = f01[int] -var _ = f01[bool /* ERROR does not satisfy I0 */ ] -var _ = f01[string] -var _ = f01[float64 /* ERROR does not satisfy I0 */ ] - -type I012 interface { - E0 - E1 - E2 -} - -func f012[T I012]() -var _ = f012[int /* ERROR does not satisfy I012 */ ] -var _ = f012[bool /* ERROR does not satisfy I012 */ ] -var _ = f012[string /* ERROR does not satisfy I012 */ ] -var _ = f012[float64 /* ERROR does not satisfy I012 */ ] - -type I12 interface { - E1 - E2 -} - -func f12[T I12]() -var _ = f12[int /* ERROR does not satisfy I12 */ ] -var _ = f12[bool /* ERROR does not satisfy I12 */ ] -var _ = f12[string /* ERROR does not satisfy I12 */ ] -var _ = f12[float64] - -type I0_ interface { - E0 - ~int -} - -func f0_[T I0_]() -var _ = f0_[int] -var _ = f0_[bool /* ERROR does not satisfy I0_ */ ] -var _ = f0_[string /* ERROR does not satisfy I0_ */ ] -var _ = f0_[float64 /* ERROR does not satisfy I0_ */ ] diff --git a/src/cmd/compile/internal/syntax/testdata/go2/typeparams.go2 b/src/cmd/compile/internal/syntax/testdata/go2/typeparams.go2 deleted file mode 100644 index 111f7c1004..0000000000 --- a/src/cmd/compile/internal/syntax/testdata/go2/typeparams.go2 +++ /dev/null @@ -1,451 +0,0 @@ -// Copyright 2018 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. - -package p - -// import "io" // for type assertion tests - -// The predeclared identifier "any" is only visible as a constraint -// in a type parameter list. -var _ any // ERROR undeclared -func _[_ any /* ok here */ , _ interface{any /* ERROR undeclared */ }](any /* ERROR undeclared */ ) { - var _ any /* ERROR undeclared */ -} - -func identity[T any](x T) T { return x } - -func _[_ any](x int) int -func _[T any](T /* ERROR redeclared */ T)() -func _[T, T /* ERROR redeclared */ any]() - -func reverse[T any](list []T) []T { - rlist := make([]T, len(list)) - i := len(list) - for _, x := range list { - i-- - rlist[i] = x - } - return rlist -} - -var _ = reverse /* ERROR cannot use generic function reverse */ -var _ = reverse[int, float32 /* ERROR got 2 type arguments */ ] ([]int{1, 2, 3}) -var _ = reverse[int]([ /* ERROR cannot use */ ]float32{1, 2, 3}) -var f = reverse[chan int] -var _ = f(0 /* ERROR cannot convert 0 .* to \[\]chan int */ ) - -func swap[A, B any](a A, b B) (B, A) { return b, a } - -var _ = swap /* ERROR single value is expected */ [int, float32](1, 2) -var f32, i = swap[int, float32](swap(float32, int)(1, 2)) -var _ float32 = f32 -var _ int = i - -func swapswap[A, B any](a A, b B) (A, B) { - return swap[B, A](b, a) -} - -type F[A, B any] func(A, B) (B, A) - -func min[T interface{ ~int }](x, y T) T { - if x < y { - return x - } - return y -} - -func _[T interface{ ~int | ~float32 }](x, y T) bool { return x < y } -func _[T any](x, y T) bool { return x /* ERROR cannot compare */ < y } -func _[T interface{ ~int | ~float32 | ~bool }](x, y T) bool { return x /* ERROR cannot compare */ < y } - -func _[T C1[T]](x, y T) bool { return x /* ERROR cannot compare */ < y } -func _[T C2[T]](x, y T) bool { return x < y } - -type C1[T any] interface{} -type C2[T any] interface{ ~int | ~float32 } - -func new[T any]() *T { - var x T - return &x -} - -var _ = new /* ERROR cannot use generic function new */ -var _ *int = new[int]() - -func _[T any](map[T /* ERROR invalid map key type T \(missing comparable constraint\) */]int) // w/o constraint we don't know if T is comparable - -func f1[T1 any](struct{T1}) int -var _ = f1(int)(struct{T1}{}) -type T1 = int - -func f2[t1 any](struct{t1; x float32}) int -var _ = f2(t1)(struct{t1; x float32}{}) -type t1 = int - - -func f3[A, B, C any](A, struct{x B}, func(A, struct{x B}, *C)) int - -var _ = f3[int, rune, bool](1, struct{x rune}{}, nil) - -// indexing - -func _[T any] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] } -func _[T interface{ ~int }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] } -func _[T interface{ ~string }] (x T, i int) { _ = x[i] } -func _[T interface{ ~[]int }] (x T, i int) { _ = x[i] } -func _[T interface{ ~[10]int | ~*[20]int | ~map[string]int }] (x T, i int) { _ = x[i] } -func _[T interface{ ~string | ~[]byte }] (x T, i int) { _ = x[i] } -func _[T interface{ ~[]int | ~[1]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] } -func _[T interface{ ~string | ~[]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] } - -// slicing -// TODO(gri) implement this - -func _[T interface{ ~string }] (x T, i, j, k int) { _ = x /* ERROR invalid operation */ [i:j:k] } - -// len/cap built-ins - -func _[T any](x T) { _ = len(x /* ERROR invalid argument */ ) } -func _[T interface{ ~int }](x T) { _ = len(x /* ERROR invalid argument */ ) } -func _[T interface{ ~string | ~[]byte | ~int }](x T) { _ = len(x /* ERROR invalid argument */ ) } -func _[T interface{ ~string }](x T) { _ = len(x) } -func _[T interface{ ~[10]int }](x T) { _ = len(x) } -func _[T interface{ ~[]byte }](x T) { _ = len(x) } -func _[T interface{ ~map[int]int }](x T) { _ = len(x) } -func _[T interface{ ~chan int }](x T) { _ = len(x) } -func _[T interface{ ~string | ~[]byte | ~chan int }](x T) { _ = len(x) } - -func _[T any](x T) { _ = cap(x /* ERROR invalid argument */ ) } -func _[T interface{ ~int }](x T) { _ = cap(x /* ERROR invalid argument */ ) } -func _[T interface{ ~string | ~[]byte | ~int }](x T) { _ = cap(x /* ERROR invalid argument */ ) } -func _[T interface{ ~string }](x T) { _ = cap(x /* ERROR invalid argument */ ) } -func _[T interface{ ~[10]int }](x T) { _ = cap(x) } -func _[T interface{ ~[]byte }](x T) { _ = cap(x) } -func _[T interface{ ~map[int]int }](x T) { _ = cap(x /* ERROR invalid argument */ ) } -func _[T interface{ ~chan int }](x T) { _ = cap(x) } -func _[T interface{ ~[]byte | ~chan int }](x T) { _ = cap(x) } - -// range iteration - -func _[T interface{}](x T) { - for range x /* ERROR cannot range */ {} -} - -func _[T interface{ ~string | ~[]string }](x T) { - for range x {} - for i := range x { _ = i } - for i, _ := range x { _ = i } - for i, e := range x /* ERROR must have the same element type */ { _ = i } - for _, e := range x /* ERROR must have the same element type */ {} - var e rune - _ = e - for _, (e) = range x /* ERROR must have the same element type */ {} -} - - -func _[T interface{ ~string | ~[]rune | ~map[int]rune }](x T) { - for _, e := range x { _ = e } - for i, e := range x { _ = i; _ = e } -} - -func _[T interface{ ~string | ~[]rune | ~map[string]rune }](x T) { - for _, e := range x { _ = e } - for i, e := range x /* ERROR must have the same key type */ { _ = e } -} - -func _[T interface{ ~string | ~chan int }](x T) { - for range x {} - for i := range x { _ = i } - for i, _ := range x { _ = i } // TODO(gri) should get an error here: channels only return one value -} - -func _[T interface{ ~string | ~chan<-int }](x T) { - for i := range x /* ERROR send-only channel */ { _ = i } -} - -// type inference checks - -var _ = new() /* ERROR cannot infer T */ - -func f4[A, B, C any](A, B) C - -var _ = f4(1, 2) /* ERROR cannot infer C */ -var _ = f4[int, float32, complex128](1, 2) - -func f5[A, B, C any](A, []*B, struct{f []C}) int - -var _ = f5[int, float32, complex128](0, nil, struct{f []complex128}{}) -var _ = f5(0, nil, struct{f []complex128}{}) // ERROR cannot infer -var _ = f5(0, []*float32{new[float32]()}, struct{f []complex128}{}) - -func f6[A any](A, []A) int - -var _ = f6(0, nil) - -func f6nil[A any](A) int - -var _ = f6nil(nil) // ERROR cannot infer - -// type inference with variadic functions - -func f7[T any](...T) T - -var _ int = f7() /* ERROR cannot infer T */ -var _ int = f7(1) -var _ int = f7(1, 2) -var _ int = f7([]int{}...) -var _ int = f7 /* ERROR cannot use */ ([]float64{}...) -var _ float64 = f7([]float64{}...) -var _ = f7[float64](1, 2.3) -var _ = f7(float64(1), 2.3) -var _ = f7(1, 2.3 /* ERROR does not match */ ) -var _ = f7(1.2, 3 /* ERROR does not match */ ) - -func f8[A, B any](A, B, ...B) int - -var _ = f8(1) /* ERROR not enough arguments */ -var _ = f8(1, 2.3) -var _ = f8(1, 2.3, 3.4, 4.5) -var _ = f8(1, 2.3, 3.4, 4 /* ERROR does not match */ ) -var _ = f8(int, float64)(1, 2.3, 3.4, 4) - -var _ = f8(int, float64)(0, 0, nil...) // test case for #18268 - -// init functions cannot have type parameters - -func init() {} -func init[/* ERROR func init must have no type parameters */ _ any]() {} -func init[/* ERROR func init must have no type parameters */ P any]() {} - -type T struct {} - -func (T) m1() {} -// The type checker accepts method type parameters if configured accordingly. -func (T) m2[_ any]() {} -func (T) m3[P any]() {} - -// type inference across parameterized types - -type S1[P any] struct { f P } - -func f9[P any](x S1[P]) - -func _() { - f9[int](S1[int]{42}) - f9(S1[int]{42}) -} - -type S2[A, B, C any] struct{} - -func f10[X, Y, Z any](a S2[X, int, Z], b S2[X, Y, bool]) - -func _[P any]() { - f10[int, float32, string](S2[int, int, string]{}, S2[int, float32, bool]{}) - f10(S2[int, int, string]{}, S2[int, float32, bool]{}) - f10(S2[P, int, P]{}, S2[P, float32, bool]{}) -} - -// corner case for type inference -// (was bug: after instanting f11, the type-checker didn't mark f11 as non-generic) - -func f11[T any]() - -func _() { - f11[int]() -} - -// the previous example was extracted from - -func f12[T interface{m() T}]() - -type A[T any] T - -func (a A[T]) m() A[T] - -func _[T any]() { - f12(A[T])() -} - -// method expressions - -func (_ S1[P]) m() - -func _() { - m := S1[int].m - m(struct { f int }{42}) -} - -func _[T any] (x T) { - m := S1[T].m - m(S1[T]{x}) -} - -// type parameters in methods (generalization) - -type R0 struct{} - -func (R0) _[T any](x T) -func (R0 /* ERROR invalid receiver */ ) _[R0 any]() // scope of type parameters starts at "func" - -type R1[A, B any] struct{} - -func (_ R1[A, B]) m0(A, B) -func (_ R1[A, B]) m1[T any](A, B, T) T -func (_ R1 /* ERROR not a generic type */ [R1, _]) _() -func (_ R1[A, B]) _[A /* ERROR redeclared */ any](B) - -func _() { - var r R1[int, string] - r.m1[rune](42, "foo", 'a') - r.m1[rune](42, "foo", 1.2 /* ERROR truncated to rune */) - r.m1(42, "foo", 1.2) // using type inference - var _ float64 = r.m1(42, "foo", 1.2) -} - -type I1[A any] interface { - m1(A) -} - -var _ I1[int] = r1[int]{} - -type r1[T any] struct{} - -func (_ r1[T]) m1(T) - -type I2[A, B any] interface { - m1(A) - m2(A) B -} - -var _ I2[int, float32] = R2[int, float32]{} - -type R2[P, Q any] struct{} - -func (_ R2[X, Y]) m1(X) -func (_ R2[X, Y]) m2(X) Y - -// type assertions and type switches over generic types -// NOTE: These are currently disabled because it's unclear what the correct -// approach is, and one can always work around by assigning the variable to -// an interface first. - -// // ReadByte1 corresponds to the ReadByte example in the draft design. -// func ReadByte1[T io.Reader](r T) (byte, error) { -// if br, ok := r.(io.ByteReader); ok { -// return br.ReadByte() -// } -// var b [1]byte -// _, err := r.Read(b[:]) -// return b[0], err -// } -// -// // ReadBytes2 is like ReadByte1 but uses a type switch instead. -// func ReadByte2[T io.Reader](r T) (byte, error) { -// switch br := r.(type) { -// case io.ByteReader: -// return br.ReadByte() -// } -// var b [1]byte -// _, err := r.Read(b[:]) -// return b[0], err -// } -// -// // type assertions and type switches over generic types are strict -// type I3 interface { -// m(int) -// } -// -// type I4 interface { -// m() int // different signature from I3.m -// } -// -// func _[T I3](x I3, p T) { -// // type assertions and type switches over interfaces are not strict -// _ = x.(I4) -// switch x.(type) { -// case I4: -// } -// -// // type assertions and type switches over generic types are strict -// _ = p /* ERROR cannot have dynamic type I4 */.(I4) -// switch p.(type) { -// case I4 /* ERROR cannot have dynamic type I4 */ : -// } -// } - -// type assertions and type switches over generic types lead to errors for now - -func _[T any](x T) { - _ = x /* ERROR not an interface */ .(int) - switch x /* ERROR not an interface */ .(type) { - } - - // work-around - var t interface{} = x - _ = t.(int) - switch t.(type) { - } -} - -func _[T interface{ ~int }](x T) { - _ = x /* ERROR not an interface */ .(int) - switch x /* ERROR not an interface */ .(type) { - } - - // work-around - var t interface{} = x - _ = t.(int) - switch t.(type) { - } -} - -// error messages related to type bounds mention those bounds -type C[P any] interface{} - -func _[P C[P]] (x P) { - x.m /* ERROR x.m undefined */ () -} - -type I interface {} - -func _[P I] (x P) { - x.m /* ERROR interface I has no method m */ () -} - -func _[P interface{}] (x P) { - x.m /* ERROR type bound for P has no method m */ () -} - -func _[P any] (x P) { - x.m /* ERROR type bound for P has no method m */ () -} - -// automatic distinguishing between array and generic types -// NOTE: Disabled when using unified parameter list syntax. -/* -const P = 10 -type A1 [P]byte -func _(a A1) { - assert(len(a) == 10) -} - -type A2 [P]struct{ - f [P]byte -} -func _(a A2) { - assert(len(a) == 10) - assert(len(a[0].f) == 10) -} - -type A3 [P]func(x [P]A3) -func _(a A3) { - assert(len(a) == 10) -} - -type T2[P] struct{ P } -var _ T2[int] - -type T3[P] func(P) -var _ T3[int] -*/ \ No newline at end of file diff --git a/src/cmd/compile/internal/syntax/testdata/interface.go2 b/src/cmd/compile/internal/syntax/testdata/interface.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/interface.go2 rename to src/cmd/compile/internal/syntax/testdata/interface.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue20789.src b/src/cmd/compile/internal/syntax/testdata/issue20789.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue20789.src rename to src/cmd/compile/internal/syntax/testdata/issue20789.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue23385.src b/src/cmd/compile/internal/syntax/testdata/issue23385.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue23385.src rename to src/cmd/compile/internal/syntax/testdata/issue23385.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue23434.src b/src/cmd/compile/internal/syntax/testdata/issue23434.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue23434.src rename to src/cmd/compile/internal/syntax/testdata/issue23434.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue31092.src b/src/cmd/compile/internal/syntax/testdata/issue31092.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue31092.src rename to src/cmd/compile/internal/syntax/testdata/issue31092.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue43527.go2 b/src/cmd/compile/internal/syntax/testdata/issue43527.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue43527.go2 rename to src/cmd/compile/internal/syntax/testdata/issue43527.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue43674.src b/src/cmd/compile/internal/syntax/testdata/issue43674.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue43674.src rename to src/cmd/compile/internal/syntax/testdata/issue43674.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue46558.src b/src/cmd/compile/internal/syntax/testdata/issue46558.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue46558.src rename to src/cmd/compile/internal/syntax/testdata/issue46558.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue47704.go2 b/src/cmd/compile/internal/syntax/testdata/issue47704.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue47704.go2 rename to src/cmd/compile/internal/syntax/testdata/issue47704.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue47704.src b/src/cmd/compile/internal/syntax/testdata/issue47704.go117 similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue47704.src rename to src/cmd/compile/internal/syntax/testdata/issue47704.go117 diff --git a/src/cmd/compile/internal/syntax/testdata/issue48382.go2 b/src/cmd/compile/internal/syntax/testdata/issue48382.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue48382.go2 rename to src/cmd/compile/internal/syntax/testdata/issue48382.go diff --git a/src/cmd/compile/internal/syntax/testdata/issue49482.go2 b/src/cmd/compile/internal/syntax/testdata/issue49482.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/issue49482.go2 rename to src/cmd/compile/internal/syntax/testdata/issue49482.go diff --git a/src/cmd/compile/internal/syntax/testdata/go2/linalg.go2 b/src/cmd/compile/internal/syntax/testdata/linalg.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/go2/linalg.go2 rename to src/cmd/compile/internal/syntax/testdata/linalg.go diff --git a/src/cmd/compile/internal/syntax/testdata/go2/map.go2 b/src/cmd/compile/internal/syntax/testdata/map.go similarity index 97% rename from src/cmd/compile/internal/syntax/testdata/go2/map.go2 rename to src/cmd/compile/internal/syntax/testdata/map.go index 814d9539fd..a508d214b8 100644 --- a/src/cmd/compile/internal/syntax/testdata/go2/map.go2 +++ b/src/cmd/compile/internal/syntax/testdata/map.go @@ -5,8 +5,7 @@ // Package orderedmap provides an ordered map, implemented as a binary tree. package orderedmap -// TODO(gri) fix imports for tests -import "chans" // ERROR could not import +import "chans" // Map is an ordered map. type Map[K, V any] struct { diff --git a/src/cmd/compile/internal/syntax/testdata/go2/map2.go2 b/src/cmd/compile/internal/syntax/testdata/map2.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/go2/map2.go2 rename to src/cmd/compile/internal/syntax/testdata/map2.go diff --git a/src/cmd/compile/internal/syntax/testdata/sample.src b/src/cmd/compile/internal/syntax/testdata/sample.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/sample.src rename to src/cmd/compile/internal/syntax/testdata/sample.go diff --git a/src/cmd/compile/internal/syntax/testdata/go2/slices.go2 b/src/cmd/compile/internal/syntax/testdata/slices.go similarity index 94% rename from src/cmd/compile/internal/syntax/testdata/go2/slices.go2 rename to src/cmd/compile/internal/syntax/testdata/slices.go index 2bacd1c2aa..9265109556 100644 --- a/src/cmd/compile/internal/syntax/testdata/go2/slices.go2 +++ b/src/cmd/compile/internal/syntax/testdata/slices.go @@ -56,7 +56,7 @@ func reducer(x float64, y int) float64 { } var reduced1 = Reduce[int, float64](input, 0, reducer) -var reduced2 = Reduce(input, 1i /* ERROR overflows */, reducer) // using type inference +var reduced2 = Reduce(input, 1i, reducer) // using type inference var reduced3 = Reduce(input, 1, reducer) // using type inference func filter(x int) bool { diff --git a/src/cmd/compile/internal/syntax/testdata/go2/smoketest.go2 b/src/cmd/compile/internal/syntax/testdata/smoketest.go similarity index 92% rename from src/cmd/compile/internal/syntax/testdata/go2/smoketest.go2 rename to src/cmd/compile/internal/syntax/testdata/smoketest.go index 42efb42527..1eb04e3ff7 100644 --- a/src/cmd/compile/internal/syntax/testdata/go2/smoketest.go2 +++ b/src/cmd/compile/internal/syntax/testdata/smoketest.go @@ -28,12 +28,6 @@ func _[P interface{}]() func _[P B]() func _[P B[P]]() -// in methods -func (T) _[P any]() -func (T) _[P interface{}]() -func (T) _[P B]() -func (T) _[P B[P]]() - // type instantiations type _ T[int] diff --git a/src/cmd/compile/internal/syntax/testdata/tparams.go2 b/src/cmd/compile/internal/syntax/testdata/tparams.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/tparams.go2 rename to src/cmd/compile/internal/syntax/testdata/tparams.go diff --git a/src/cmd/compile/internal/syntax/testdata/typeset.go2 b/src/cmd/compile/internal/syntax/testdata/typeset.go similarity index 100% rename from src/cmd/compile/internal/syntax/testdata/typeset.go2 rename to src/cmd/compile/internal/syntax/testdata/typeset.go -- 2.50.0