aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam/double.go
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-03-18 11:56:46 -0700
committerDan Scales <danscales@google.com>2021-03-18 22:18:32 +0000
commit90b1ed1602ad8db08a5b1bde4aae5f8569b21915 (patch)
tree4a3356a6a99812a1961c05ce018bf8dc19c3575d /test/typeparam/double.go
parent095ba225973152734b0722e7c5758accb2639c15 (diff)
downloadgo-90b1ed1602ad8db08a5b1bde4aae5f8569b21915.tar.gz
go-90b1ed1602ad8db08a5b1bde4aae5f8569b21915.zip
cmd/compile: get untyped constants working in generic functions
types2 will give us a constant with a type T, if an untyped constant is used with another operand of type T (in a provably correct way). When we substitute in the type args during stenciling, we now know the real type of the constant. We may then need to change the BasicLit.val to be the correct type (e.g. convert an int64Val constant to a floatVal constant). Otherwise, later parts of the compiler will be confused. Updated tests list.go and double.go with uses of untyped constants. Change-Id: I9966bbb0dea3a7de1c5a6420f8ad8af9ca84a33e Reviewed-on: https://go-review.googlesource.com/c/go/+/303089 Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dan Scales <danscales@google.com> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'test/typeparam/double.go')
-rw-r--r--test/typeparam/double.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/typeparam/double.go b/test/typeparam/double.go
index 1f7a26c7f4..ce78ec9748 100644
--- a/test/typeparam/double.go
+++ b/test/typeparam/double.go
@@ -16,6 +16,7 @@ type Number interface {
}
type MySlice []int
+type MyFloatSlice []float64
type _SliceOf[E any] interface {
type []E
@@ -29,6 +30,15 @@ func _DoubleElems[S _SliceOf[E], E Number](s S) S {
return r
}
+// Test use of untyped constant in an expression with a generically-typed parameter
+func _DoubleElems2[S _SliceOf[E], E Number](s S) S {
+ r := make(S, len(s))
+ for i, v := range s {
+ r[i] = v * 2
+ }
+ return r
+}
+
func main() {
arg := MySlice{1, 2, 3}
want := MySlice{2, 4, 6}
@@ -47,4 +57,16 @@ func main() {
if !reflect.DeepEqual(got, want) {
panic(fmt.Sprintf("got %s, want %s", got, want))
}
+
+ farg := MyFloatSlice{1.2, 2.0, 3.5}
+ fwant := MyFloatSlice{2.4, 4.0, 7.0}
+ fgot := _DoubleElems(farg)
+ if !reflect.DeepEqual(fgot, fwant) {
+ panic(fmt.Sprintf("got %s, want %s", fgot, fwant))
+ }
+
+ fgot = _DoubleElems2(farg)
+ if !reflect.DeepEqual(fgot, fwant) {
+ panic(fmt.Sprintf("got %s, want %s", fgot, fwant))
+ }
}