aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2021-12-15 13:04:54 -0800
committerKeith Randall <khr@golang.org>2021-12-16 00:33:58 +0000
commitd107aa2cd1fdc596b9275a127e6c35cc5f8d32bb (patch)
treecea2411f97ea73d29d3eddc4c17a6e49a8319cd4 /test/typeparam
parent6e7c6912186b6c91fff332ef473409a8e960c519 (diff)
downloadgo-d107aa2cd1fdc596b9275a127e6c35cc5f8d32bb.tar.xz
cmd/compile: upgrade ssa to do (int or float) -> complex
Generic instantiations can produce conversions from constant literal ints or floats to complex values. We could constant literals during instantiation, but it is just as easy to upgrade the code generator to do the conversions. Fixes #50193 Change-Id: I24bdc09226c8e868f6282e0e4057ba6c3ad5c41a Reviewed-on: https://go-review.googlesource.com/c/go/+/372514 Trust: Keith Randall <khr@golang.org> Run-TryBot: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Reviewed-by: Dan Scales <danscales@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/issue50193.go32
-rw-r--r--test/typeparam/issue50193.out6
2 files changed, 38 insertions, 0 deletions
diff --git a/test/typeparam/issue50193.go b/test/typeparam/issue50193.go
new file mode 100644
index 0000000000..8dc488244e
--- /dev/null
+++ b/test/typeparam/issue50193.go
@@ -0,0 +1,32 @@
+// run -gcflags=-G=3
+
+// Copyright 2021 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 main
+
+import (
+ "constraints"
+ "fmt"
+)
+
+func zero[T constraints.Complex]() T {
+ return T(0)
+}
+func pi[T constraints.Complex]() T {
+ return T(3.14)
+}
+func sqrtN1[T constraints.Complex]() T {
+ return T(-1i)
+}
+
+func main() {
+ fmt.Println(zero[complex128]())
+ fmt.Println(pi[complex128]())
+ fmt.Println(sqrtN1[complex128]())
+ fmt.Println(zero[complex64]())
+ fmt.Println(pi[complex64]())
+ fmt.Println(sqrtN1[complex64]())
+}
+
diff --git a/test/typeparam/issue50193.out b/test/typeparam/issue50193.out
new file mode 100644
index 0000000000..68186222c7
--- /dev/null
+++ b/test/typeparam/issue50193.out
@@ -0,0 +1,6 @@
+(0+0i)
+(3.14+0i)
+(0-1i)
+(0+0i)
+(3.14+0i)
+(0-1i)