aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2020-10-29 01:13:16 -0700
committerMatthew Dempsky <mdempsky@google.com>2020-11-06 20:49:11 +0000
commit5736eb0013cb8c9b67432c98b08f68e9f370810c (patch)
tree3f3c0d5e330409476bf37cf3a7707d75e28911fc /test
parent362d25f2c82980860cb4eb5bfd0648116504788d (diff)
downloadgo-5736eb0013cb8c9b67432c98b08f68e9f370810c.tar.gz
go-5736eb0013cb8c9b67432c98b08f68e9f370810c.zip
cmd/compile: support inlining of type switches
This CL adds support for inlining type switches, including exporting and importing them. Type switches are represented mostly the same as expression switches. However, if the type switch guard includes a short variable declaration, then there are two differences: (1) there's an ONONAME (in the OTYPESW's Left) to represent the overall pseudo declaration; and (2) there's an ONAME (in each OCASE's Rlist) to represent the per-case variables. For simplicity, this CL simply writes out each variable separately using iimport/iiexport's normal Vargen mechanism for disambiguating identically named variables within a function. This could be improved somewhat, but inlinable type switches are probably too uncommon to merit the complexity. While here, remove "case OCASE" from typecheck1. We only type check "case" clauses as part of a "select" or "switch" statement, never as standalone statements. Fixes #37837 Change-Id: I8f42f6c9afdd821d6202af4a6bf1dbcbba0ef424 Reviewed-on: https://go-review.googlesource.com/c/go/+/266203 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Trust: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'test')
-rw-r--r--test/fixedbugs/issue37837.dir/a.go33
-rw-r--r--test/fixedbugs/issue37837.dir/b.go32
-rw-r--r--test/fixedbugs/issue37837.go7
-rw-r--r--test/inline.go3
4 files changed, 73 insertions, 2 deletions
diff --git a/test/fixedbugs/issue37837.dir/a.go b/test/fixedbugs/issue37837.dir/a.go
new file mode 100644
index 0000000000..49d830ffbc
--- /dev/null
+++ b/test/fixedbugs/issue37837.dir/a.go
@@ -0,0 +1,33 @@
+// Copyright 2020 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 a
+
+func F(i interface{}) int { // ERROR "can inline F" "i does not escape"
+ switch i.(type) {
+ case nil:
+ return 0
+ case int:
+ return 1
+ case float64:
+ return 2
+ default:
+ return 3
+ }
+}
+
+func G(i interface{}) interface{} { // ERROR "can inline G" "leaking param: i"
+ switch i := i.(type) {
+ case nil: // ERROR "moved to heap: i"
+ return &i
+ case int: // ERROR "moved to heap: i"
+ return &i
+ case float64: // ERROR "moved to heap: i"
+ return &i
+ case string, []byte: // ERROR "moved to heap: i"
+ return &i
+ default: // ERROR "moved to heap: i"
+ return &i
+ }
+}
diff --git a/test/fixedbugs/issue37837.dir/b.go b/test/fixedbugs/issue37837.dir/b.go
new file mode 100644
index 0000000000..461f5c7a55
--- /dev/null
+++ b/test/fixedbugs/issue37837.dir/b.go
@@ -0,0 +1,32 @@
+// Copyright 2020 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 "./a"
+
+func main() {
+ // Test that inlined type switches without short variable
+ // declarations work correctly.
+ check(0, a.F(nil)) // ERROR "inlining call to a.F"
+ check(1, a.F(0)) // ERROR "inlining call to a.F" "does not escape"
+ check(2, a.F(0.0)) // ERROR "inlining call to a.F" "does not escape"
+ check(3, a.F("")) // ERROR "inlining call to a.F" "does not escape"
+
+ // Test that inlined type switches with short variable
+ // declarations work correctly.
+ _ = a.G(nil).(*interface{}) // ERROR "inlining call to a.G"
+ _ = a.G(1).(*int) // ERROR "inlining call to a.G" "does not escape"
+ _ = a.G(2.0).(*float64) // ERROR "inlining call to a.G" "does not escape"
+ _ = (*a.G("").(*interface{})).(string) // ERROR "inlining call to a.G" "does not escape"
+ _ = (*a.G(([]byte)(nil)).(*interface{})).([]byte) // ERROR "inlining call to a.G" "does not escape"
+ _ = (*a.G(true).(*interface{})).(bool) // ERROR "inlining call to a.G" "does not escape"
+}
+
+//go:noinline
+func check(want, got int) {
+ if want != got {
+ println("want", want, "but got", got)
+ }
+}
diff --git a/test/fixedbugs/issue37837.go b/test/fixedbugs/issue37837.go
new file mode 100644
index 0000000000..2e8abc5f05
--- /dev/null
+++ b/test/fixedbugs/issue37837.go
@@ -0,0 +1,7 @@
+// errorcheckandrundir -0 -m
+
+// Copyright 2020 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 ignored
diff --git a/test/inline.go b/test/inline.go
index 470414f883..d754f06e03 100644
--- a/test/inline.go
+++ b/test/inline.go
@@ -152,8 +152,7 @@ func switchBreak(x, y int) int {
return n
}
-// can't currently inline functions with a type switch
-func switchType(x interface{}) int { // ERROR "x does not escape"
+func switchType(x interface{}) int { // ERROR "can inline switchType" "x does not escape"
switch x.(type) {
case int:
return x.(int)