aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-10-27 13:55:15 -0700
committerRobert Griesemer <gri@golang.org>2021-10-28 00:11:24 +0000
commita91d0b649c9ee8c64328fb124eff94dfca300d39 (patch)
treea0cb94476cfb1bc09c2a806f71aaf36d1c0cb4cb /test/typeparam
parent79ff663754f4238bd1fe2e56f460c2f603c71b80 (diff)
downloadgo-a91d0b649c9ee8c64328fb124eff94dfca300d39.tar.xz
cmd/compile/internal/types2: disallow lone type parameter on RHS of type declaration
We may revisit this decision in a future release. By disallowing this for Go 1.18 we are ensuring that we don't lock in the generics design in a place that may need to change later. (Type declarations are the primary construct where it crucially matters what the underlying type of a type parameter is.) Comment out all tests that rely on this feature; add comments referring to issue so we can find all places easily should we change our minds. Fixes #45639. Change-Id: I730510e4da66d3716d455a9071c7778a1e4a1152 Reviewed-on: https://go-review.googlesource.com/c/go/+/359177 Trust: Robert Griesemer <gri@golang.org> Trust: Dan Scales <danscales@google.com> Reviewed-by: Dan Scales <danscales@google.com>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/absdiff.go105
-rw-r--r--test/typeparam/absdiffimp.dir/a.go71
-rw-r--r--test/typeparam/absdiffimp.dir/main.go38
-rw-r--r--test/typeparam/boundmethod.go34
-rw-r--r--test/typeparam/issue47708.go45
-rw-r--r--test/typeparam/issue47740.go23
-rw-r--r--test/typeparam/issue47740.out2
7 files changed, 154 insertions, 164 deletions
diff --git a/test/typeparam/absdiff.go b/test/typeparam/absdiff.go
index cad6e84c4e..f1822831b2 100644
--- a/test/typeparam/absdiff.go
+++ b/test/typeparam/absdiff.go
@@ -6,11 +6,6 @@
package main
-import (
- "fmt"
- "math"
-)
-
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
@@ -43,55 +38,57 @@ type Complex interface {
~complex64 | ~complex128
}
-// orderedAbs is a helper type that defines an Abs method for
-// ordered numeric types.
-type orderedAbs[T orderedNumeric] T
-
-func (a orderedAbs[T]) Abs() orderedAbs[T] {
- if a < 0 {
- return -a
- }
- return a
-}
-
-// complexAbs is a helper type that defines an Abs method for
-// complex types.
-type complexAbs[T Complex] T
-
-func (a complexAbs[T]) Abs() complexAbs[T] {
- r := float64(real(a))
- i := float64(imag(a))
- d := math.Sqrt(r*r + i*i)
- return complexAbs[T](complex(d, 0))
-}
-
-// OrderedAbsDifference returns the absolute value of the difference
-// between a and b, where a and b are of an ordered type.
-func orderedAbsDifference[T orderedNumeric](a, b T) T {
- return T(absDifference(orderedAbs[T](a), orderedAbs[T](b)))
-}
-
-// ComplexAbsDifference returns the absolute value of the difference
-// between a and b, where a and b are of a complex type.
-func complexAbsDifference[T Complex](a, b T) T {
- return T(absDifference(complexAbs[T](a), complexAbs[T](b)))
-}
+// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+// // orderedAbs is a helper type that defines an Abs method for
+// // ordered numeric types.
+// type orderedAbs[T orderedNumeric] T
+//
+// func (a orderedAbs[T]) Abs() orderedAbs[T] {
+// if a < 0 {
+// return -a
+// }
+// return a
+// }
+//
+// // complexAbs is a helper type that defines an Abs method for
+// // complex types.
+// type complexAbs[T Complex] T
+//
+// func (a complexAbs[T]) Abs() complexAbs[T] {
+// r := float64(real(a))
+// i := float64(imag(a))
+// d := math.Sqrt(r*r + i*i)
+// return complexAbs[T](complex(d, 0))
+// }
+//
+// // OrderedAbsDifference returns the absolute value of the difference
+// // between a and b, where a and b are of an ordered type.
+// func orderedAbsDifference[T orderedNumeric](a, b T) T {
+// return T(absDifference(orderedAbs[T](a), orderedAbs[T](b)))
+// }
+//
+// // ComplexAbsDifference returns the absolute value of the difference
+// // between a and b, where a and b are of a complex type.
+// func complexAbsDifference[T Complex](a, b T) T {
+// return T(absDifference(complexAbs[T](a), complexAbs[T](b)))
+// }
func main() {
- if got, want := orderedAbsDifference(1.0, -2.0), 3.0; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
- if got, want := orderedAbsDifference(-1.0, 2.0), 3.0; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
- if got, want := orderedAbsDifference(-20, 15), 35; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
-
- if got, want := complexAbsDifference(5.0+2.0i, 2.0-2.0i), 5+0i; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
- if got, want := complexAbsDifference(2.0-2.0i, 5.0+2.0i), 5+0i; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
+ // // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+ // if got, want := orderedAbsDifference(1.0, -2.0), 3.0; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ // if got, want := orderedAbsDifference(-1.0, 2.0), 3.0; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ // if got, want := orderedAbsDifference(-20, 15), 35; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ //
+ // if got, want := complexAbsDifference(5.0+2.0i, 2.0-2.0i), 5+0i; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ // if got, want := complexAbsDifference(2.0-2.0i, 5.0+2.0i), 5+0i; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
}
diff --git a/test/typeparam/absdiffimp.dir/a.go b/test/typeparam/absdiffimp.dir/a.go
index 7b5bfbe2ac..60822fdb8b 100644
--- a/test/typeparam/absdiffimp.dir/a.go
+++ b/test/typeparam/absdiffimp.dir/a.go
@@ -4,10 +4,6 @@
package a
-import (
- "math"
-)
-
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
@@ -40,36 +36,37 @@ type Complex interface {
~complex64 | ~complex128
}
-// orderedAbs is a helper type that defines an Abs method for
-// ordered numeric types.
-type orderedAbs[T orderedNumeric] T
-
-func (a orderedAbs[T]) Abs() orderedAbs[T] {
- if a < 0 {
- return -a
- }
- return a
-}
-
-// complexAbs is a helper type that defines an Abs method for
-// complex types.
-type complexAbs[T Complex] T
-
-func (a complexAbs[T]) Abs() complexAbs[T] {
- r := float64(real(a))
- i := float64(imag(a))
- d := math.Sqrt(r*r + i*i)
- return complexAbs[T](complex(d, 0))
-}
-
-// OrderedAbsDifference returns the absolute value of the difference
-// between a and b, where a and b are of an ordered type.
-func OrderedAbsDifference[T orderedNumeric](a, b T) T {
- return T(absDifference(orderedAbs[T](a), orderedAbs[T](b)))
-}
-
-// ComplexAbsDifference returns the absolute value of the difference
-// between a and b, where a and b are of a complex type.
-func ComplexAbsDifference[T Complex](a, b T) T {
- return T(absDifference(complexAbs[T](a), complexAbs[T](b)))
-}
+// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+// // orderedAbs is a helper type that defines an Abs method for
+// // ordered numeric types.
+// type orderedAbs[T orderedNumeric] T
+//
+// func (a orderedAbs[T]) Abs() orderedAbs[T] {
+// if a < 0 {
+// return -a
+// }
+// return a
+// }
+//
+// // complexAbs is a helper type that defines an Abs method for
+// // complex types.
+// type complexAbs[T Complex] T
+//
+// func (a complexAbs[T]) Abs() complexAbs[T] {
+// r := float64(real(a))
+// i := float64(imag(a))
+// d := math.Sqrt(r*r + i*i)
+// return complexAbs[T](complex(d, 0))
+// }
+//
+// // OrderedAbsDifference returns the absolute value of the difference
+// // between a and b, where a and b are of an ordered type.
+// func OrderedAbsDifference[T orderedNumeric](a, b T) T {
+// return T(absDifference(orderedAbs[T](a), orderedAbs[T](b)))
+// }
+//
+// // ComplexAbsDifference returns the absolute value of the difference
+// // between a and b, where a and b are of a complex type.
+// func ComplexAbsDifference[T Complex](a, b T) T {
+// return T(absDifference(complexAbs[T](a), complexAbs[T](b)))
+// }
diff --git a/test/typeparam/absdiffimp.dir/main.go b/test/typeparam/absdiffimp.dir/main.go
index 8eefdbdf38..c648013327 100644
--- a/test/typeparam/absdiffimp.dir/main.go
+++ b/test/typeparam/absdiffimp.dir/main.go
@@ -4,26 +4,22 @@
package main
-import (
- "a"
- "fmt"
-)
-
func main() {
- if got, want := a.OrderedAbsDifference(1.0, -2.0), 3.0; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
- if got, want := a.OrderedAbsDifference(-1.0, 2.0), 3.0; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
- if got, want := a.OrderedAbsDifference(-20, 15), 35; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
-
- if got, want := a.ComplexAbsDifference(5.0+2.0i, 2.0-2.0i), 5+0i; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
- if got, want := a.ComplexAbsDifference(2.0-2.0i, 5.0+2.0i), 5+0i; got != want {
- panic(fmt.Sprintf("got = %v, want = %v", got, want))
- }
+ // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+ // if got, want := a.OrderedAbsDifference(1.0, -2.0), 3.0; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ // if got, want := a.OrderedAbsDifference(-1.0, 2.0), 3.0; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ // if got, want := a.OrderedAbsDifference(-20, 15), 35; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ //
+ // if got, want := a.ComplexAbsDifference(5.0+2.0i, 2.0-2.0i), 5+0i; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
+ // if got, want := a.ComplexAbsDifference(2.0-2.0i, 5.0+2.0i), 5+0i; got != want {
+ // panic(fmt.Sprintf("got = %v, want = %v", got, want))
+ // }
}
diff --git a/test/typeparam/boundmethod.go b/test/typeparam/boundmethod.go
index 22f416422d..a14eb544ce 100644
--- a/test/typeparam/boundmethod.go
+++ b/test/typeparam/boundmethod.go
@@ -59,12 +59,13 @@ type Ints interface {
~int32 | ~int
}
-type StringInt[T Ints] T
-
-//go:noinline
-func (m StringInt[T]) String() string {
- return strconv.Itoa(int(m))
-}
+// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+// type StringInt[T Ints] T
+//
+// //go:noinline
+// func (m StringInt[T]) String() string {
+// return strconv.Itoa(int(m))
+// }
type StringStruct[T Ints] struct {
f T
@@ -84,22 +85,23 @@ func main() {
panic(fmt.Sprintf("got %s, want %s", got, want))
}
- x2 := []StringInt[myint]{StringInt[myint](5), StringInt[myint](7), StringInt[myint](6)}
-
- // stringify on an instantiated type, whose bound method is associated with
- // the generic type StringInt[T], which maps directly to T.
- got2 := stringify(x2)
- want2 := []string{ "5", "7", "6" }
- if !reflect.DeepEqual(got2, want2) {
- panic(fmt.Sprintf("got %s, want %s", got2, want2))
- }
+ // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+ // x2 := []StringInt[myint]{StringInt[myint](5), StringInt[myint](7), StringInt[myint](6)}
+ //
+ // // stringify on an instantiated type, whose bound method is associated with
+ // // the generic type StringInt[T], which maps directly to T.
+ // got2 := stringify(x2)
+ // want2 := []string{"5", "7", "6"}
+ // if !reflect.DeepEqual(got2, want2) {
+ // panic(fmt.Sprintf("got %s, want %s", got2, want2))
+ // }
// stringify on an instantiated type, whose bound method is associated with
// the generic type StringStruct[T], which maps to a struct containing T.
x3 := []StringStruct[myint]{StringStruct[myint]{f: 11}, StringStruct[myint]{f: 10}, StringStruct[myint]{f: 9}}
got3 := stringify(x3)
- want3 := []string{ "11", "10", "9" }
+ want3 := []string{"11", "10", "9"}
if !reflect.DeepEqual(got3, want3) {
panic(fmt.Sprintf("got %s, want %s", got3, want3))
}
diff --git a/test/typeparam/issue47708.go b/test/typeparam/issue47708.go
index 261d6efb61..35d57c8a64 100644
--- a/test/typeparam/issue47708.go
+++ b/test/typeparam/issue47708.go
@@ -6,35 +6,32 @@
package main
-import (
- "fmt"
-)
-
type FooType[T any] interface {
- Foo(BarType[T])string
+ Foo(BarType[T]) string
}
type BarType[T any] interface {
- Bar(FooType[T])string
-}
-
-type Baz[T any] T
-func (l Baz[T]) Foo(v BarType[T]) string {
- return v.Bar(l)
-}
-type Bob[T any] T
-func (l Bob[T]) Bar(v FooType[T]) string {
- if v,ok := v.(Baz[T]);ok{
- return fmt.Sprintf("%v%v",v,l)
- }
- return ""
+ Bar(FooType[T]) string
}
+// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+// type Baz[T any] T
+// func (l Baz[T]) Foo(v BarType[T]) string {
+// return v.Bar(l)
+// }
+// type Bob[T any] T
+// func (l Bob[T]) Bar(v FooType[T]) string {
+// if v,ok := v.(Baz[T]);ok{
+// return fmt.Sprintf("%v%v",v,l)
+// }
+// return ""
+// }
func main() {
- var baz Baz[int] = 123
- var bob Bob[int] = 456
-
- if got, want := baz.Foo(bob), "123456"; got != want {
- panic(fmt.Sprintf("got %s want %s", got, want))
- }
+ // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+ // var baz Baz[int] = 123
+ // var bob Bob[int] = 456
+ //
+ // if got, want := baz.Foo(bob), "123456"; got != want {
+ // panic(fmt.Sprintf("got %s want %s", got, want))
+ // }
}
diff --git a/test/typeparam/issue47740.go b/test/typeparam/issue47740.go
index a8c6839de3..ea1168f4e6 100644
--- a/test/typeparam/issue47740.go
+++ b/test/typeparam/issue47740.go
@@ -12,10 +12,11 @@ type Exp[Ty any] interface {
Eval() Ty
}
-type Lit[Ty any] Ty
-
-func (lit Lit[Ty]) Eval() Ty { return Ty(lit) }
-func (lit Lit[Ty]) String() string { return fmt.Sprintf("(lit %v)", Ty(lit)) }
+// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+// type Lit[Ty any] Ty
+//
+// func (lit Lit[Ty]) Eval() Ty { return Ty(lit) }
+// func (lit Lit[Ty]) String() string { return fmt.Sprintf("(lit %v)", Ty(lit)) }
type Eq[Ty any] struct {
a Exp[Ty]
@@ -26,12 +27,14 @@ func (e Eq[Ty]) String() string {
return fmt.Sprintf("(eq %v %v)", e.a, e.b)
}
-var (
- e0 = Eq[int]{Lit[int](128), Lit[int](64)}
- e1 = Eq[bool]{Lit[bool](true), Lit[bool](true)}
-)
+// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+// var (
+// e0 = Eq[int]{Lit[int](128), Lit[int](64)}
+// e1 = Eq[bool]{Lit[bool](true), Lit[bool](true)}
+// )
func main() {
- fmt.Printf("%v\n", e0)
- fmt.Printf("%v\n", e1)
+ // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
+ // fmt.Printf("%v\n", e0)
+ // fmt.Printf("%v\n", e1)
}
diff --git a/test/typeparam/issue47740.out b/test/typeparam/issue47740.out
index f23c310f66..e69de29bb2 100644
--- a/test/typeparam/issue47740.out
+++ b/test/typeparam/issue47740.out
@@ -1,2 +0,0 @@
-(eq (lit 128) (lit 64))
-(eq (lit true) (lit true))