1
0
mirror of https://github.com/golang/go synced 2024-11-17 07:24:53 -07:00
go/test/typeparam/issue50193.go
Keith Randall d107aa2cd1 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>
2021-12-16 00:33:58 +00:00

33 lines
610 B
Go

// 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]())
}