aboutsummaryrefslogtreecommitdiff
path: root/test/codegen
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2024-10-01 02:30:39 +0700
committerGopher Robot <gobot@golang.org>2024-10-07 19:12:01 +0000
commit7e2487cf65f749528c17adf95ad2a82196f48de2 (patch)
treeb462c4e17332df68960840185895145233f74cc3 /test/codegen
parent6a3f39a9b856c7fd063d9bc10c1e43f074fa8867 (diff)
downloadgo-7e2487cf65f749528c17adf95ad2a82196f48de2.tar.xz
cmd/compile: avoid dynamic type when possible
If the expression type is a single compile-time known type, use that type instead of the dynamic one, so the later passes of the compiler could skip un-necessary runtime calls. Thanks Youlin Feng for writing the original test case. Change-Id: I3f65ab90f041474a9731338a82136c1d394c1773 Reviewed-on: https://go-review.googlesource.com/c/go/+/616975 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'test/codegen')
-rw-r--r--test/codegen/typeswitch.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/codegen/typeswitch.go b/test/codegen/typeswitch.go
new file mode 100644
index 0000000000..495853ed3c
--- /dev/null
+++ b/test/codegen/typeswitch.go
@@ -0,0 +1,47 @@
+// asmcheck
+
+// Copyright 2024 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 codegen
+
+type Ix interface {
+ X()
+}
+
+type Iy interface {
+ Y()
+}
+
+type Iz interface {
+ Z()
+}
+
+func swXYZ(a Ix) {
+ switch t := a.(type) {
+ case Iy: // amd64:-".*typeAssert"
+ t.Y()
+ case Iz: // amd64:-".*typeAssert"
+ t.Z()
+ }
+}
+
+type Ig[T any] interface {
+ G() T
+}
+
+func swGYZ[T any](a Ig[T]) {
+ switch t := a.(type) {
+ case Iy: // amd64:-".*typeAssert"
+ t.Y()
+ case Iz: // amd64:-".*typeAssert"
+ t.Z()
+ case interface{ G() T }: // amd64:-".*typeAssert",".*assertE2I"
+ t.G()
+ }
+}
+
+func swCaller() {
+ swGYZ[int]((Ig[int])(nil))
+}