aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-01-31 15:12:53 -0800
committerRobert Griesemer <gri@golang.org>2022-02-01 01:07:25 +0000
commit4fea5935f5f03b5037c792f8d5f5aa4cba90f1d6 (patch)
tree96e235693e9f37ed9ff4567ce0fa26d03952c7af /test/typeparam
parenteab9a77a60f5b6aaba978b61fbb260f2fbb307fb (diff)
downloadgo-4fea5935f5f03b5037c792f8d5f5aa4cba90f1d6.tar.xz
go/types, types2: disallow real, imag, complex on type parameters
We can type-check these fine but the API implications are unclear. Fixes #50912. For #50937. Change-Id: If29bbb4a257ff6a85e3bfcd4755fd8f90c80fb87 Reviewed-on: https://go-review.googlesource.com/c/go/+/382116 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/absdiff2.go20
-rw-r--r--test/typeparam/absdiff3.go20
-rw-r--r--test/typeparam/absdiffimp2.dir/a.go20
3 files changed, 54 insertions, 6 deletions
diff --git a/test/typeparam/absdiff2.go b/test/typeparam/absdiff2.go
index 2d82c4721c..36de8ff2c9 100644
--- a/test/typeparam/absdiff2.go
+++ b/test/typeparam/absdiff2.go
@@ -66,9 +66,25 @@ type complexAbs[T Complex] struct {
Value T
}
+func realimag(x any) (re, im float64) {
+ switch z := x.(type) {
+ case complex64:
+ re = float64(real(z))
+ im = float64(imag(z))
+ case complex128:
+ re = real(z)
+ im = imag(z)
+ default:
+ panic("unknown complex type")
+ }
+ return
+}
+
func (a complexAbs[T]) Abs() T {
- r := float64(real(a.Value))
- i := float64(imag(a.Value))
+ // TODO use direct conversion instead of realimag once #50937 is fixed
+ r, i := realimag(a.Value)
+ // r := float64(real(a.Value))
+ // i := float64(imag(a.Value))
d := math.Sqrt(r*r + i*i)
return T(complex(d, 0))
}
diff --git a/test/typeparam/absdiff3.go b/test/typeparam/absdiff3.go
index 3ca03fe26f..99fa6f11ab 100644
--- a/test/typeparam/absdiff3.go
+++ b/test/typeparam/absdiff3.go
@@ -43,9 +43,25 @@ type Complex interface {
~complex64 | ~complex128
}
+func realimag(x any) (re, im float64) {
+ switch z := x.(type) {
+ case complex64:
+ re = float64(real(z))
+ im = float64(imag(z))
+ case complex128:
+ re = real(z)
+ im = imag(z)
+ default:
+ panic("unknown complex type")
+ }
+ return
+}
+
func ComplexAbs[T Complex](a T) T {
- r := float64(real(a))
- i := float64(imag(a))
+ // TODO use direct conversion instead of realimag once #50937 is fixed
+ r, i := realimag(a)
+ // r := float64(real(a))
+ // i := float64(imag(a))
d := math.Sqrt(r*r + i*i)
return T(complex(d, 0))
}
diff --git a/test/typeparam/absdiffimp2.dir/a.go b/test/typeparam/absdiffimp2.dir/a.go
index 302b69b976..43493e1430 100644
--- a/test/typeparam/absdiffimp2.dir/a.go
+++ b/test/typeparam/absdiffimp2.dir/a.go
@@ -60,9 +60,25 @@ type complexAbs[T Complex] struct {
Value T
}
+func realimag(x any) (re, im float64) {
+ switch z := x.(type) {
+ case complex64:
+ re = float64(real(z))
+ im = float64(imag(z))
+ case complex128:
+ re = real(z)
+ im = imag(z)
+ default:
+ panic("unknown complex type")
+ }
+ return
+}
+
func (a complexAbs[T]) Abs() T {
- r := float64(real(a.Value))
- i := float64(imag(a.Value))
+ // TODO use direct conversion instead of realimag once #50937 is fixed
+ r, i := realimag(a.Value)
+ // r := float64(real(a.Value))
+ // i := float64(imag(a.Value))
d := math.Sqrt(r*r + i*i)
return T(complex(d, 0))
}