1
0
mirror of https://github.com/golang/go synced 2024-11-17 21:44:43 -07:00
go/test/typeparam/typeswitch2.go
Dan Scales 891470fbf7 cmd/compile: fix handling of Defn field during stenciling
When the Defn field of a name node is not an ONAME (for a closure
variable), then it points to a body node of the same function/closure.
Therefore, we should not attempt to substitute it at the time we are
substituting the local variables. Instead, we remember a mapping from the
Defn node to the nodes that reference it, and update the Defn fields of
the copied name nodes at the time that we create the new copy of the
Defn node.

Added some comments to the Defn field of ir.Name.

Moved the Defn (and Outer code, for consistency) from namelist() to
localvar(), since Defn needs to updated for all local variables, not
just those in a closure. Fixed case where .Defn was not being set
properly in noder2 for type switches. Fixed another case where the Defn
field had to be updated during transformSelect() because the Defn node
was being completely changed to a new node.

Fixed some spacing in typeswitch2.go

Fixes #47676
Fixes #48016

Change-Id: Iae70dd76575f4a647c1db79e1eba9bbe44bfc226
Reviewed-on: https://go-review.googlesource.com/c/go/+/346290
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2021-08-31 19:07:50 +00:00

32 lines
668 B
Go

// 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 "reflect"
func f[T any](i interface{}) {
switch x := i.(type) {
case T:
println("T", x)
case int:
println("int", x)
case int32, int16:
println("int32/int16", reflect.ValueOf(x).Int())
case struct{ a, b T }:
println("struct{T,T}", x.a, x.b)
default:
println("other", reflect.ValueOf(x).Int())
}
}
func main() {
f[float64](float64(6))
f[float64](int(7))
f[float64](int32(8))
f[float64](struct{ a, b float64 }{a: 1, b: 2})
f[float64](int8(9))
}