mirror of
https://github.com/golang/go
synced 2024-11-26 22:51:23 -07:00
5736eb0013
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>
33 lines
1.3 KiB
Go
33 lines
1.3 KiB
Go
// 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)
|
|
}
|
|
}
|