aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam/min.go
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-02-03 15:45:26 -0800
committerDan Scales <danscales@google.com>2021-02-05 16:40:12 +0000
commitdcb5e0392e73c900db0f7260b392c91611e33540 (patch)
tree6601fb5362d9493eaed016c1888de53373507a53 /test/typeparam/min.go
parentf37b0c6c12072edef19569c7f0b456ab7e570385 (diff)
downloadgo-dcb5e0392e73c900db0f7260b392c91611e33540.tar.gz
go-dcb5e0392e73c900db0f7260b392c91611e33540.zip
[dev.typeparams] cmd/compile: add stenciling of simple generic functions
Allow full compilation and running of simple programs with generic functions by stenciling on the fly the needed generic functions. Deal with some simple derived types based on type params. Include a few new typeparam tests min.go and add.go which involve fully compiling and running simple generic code. Change-Id: Ifc2a64ecacdbd860faaeee800e2ef49ffef9df5e Reviewed-on: https://go-review.googlesource.com/c/go/+/289630 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/min.go')
-rw-r--r--test/typeparam/min.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/typeparam/min.go b/test/typeparam/min.go
new file mode 100644
index 0000000000..3bd92c5f3e
--- /dev/null
+++ b/test/typeparam/min.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 (
+ "fmt"
+)
+
+
+func min[T interface{ type int }](x, y T) T {
+ if x < y {
+ return x
+ }
+ return y
+}
+
+func main() {
+ want := 2
+ got := min[int](2, 3)
+ if want != got {
+ panic(fmt.Sprintf("Want %d, got %d", want, got))
+ }
+
+ got = min(2, 3)
+ if want != got {
+ panic(fmt.Sprintf("Want %d, got %d", want, got))
+ }
+}