1
0
mirror of https://github.com/golang/go synced 2024-11-13 20:20:30 -07:00
go/test/fixedbugs/issue37837.dir/a.go
Matthew Dempsky 5736eb0013 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>
2020-11-06 20:49:11 +00:00

34 lines
736 B
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 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
}
}