1
0
mirror of https://github.com/golang/go synced 2024-09-29 03:34:33 -06:00
go/test/typeparam/dottype.go
Keith Randall b27c7e30dc [dev.typeparams] cmd/compile: fix HasShape, add dottype test
HasShape needs a TINTER case.

Add a test for x.(T) in various situations. Needs the fix above.

Also remove ONEW unshapify case. It is ok for ONEW to have a shape
type, as it will just be passed to mallocgc, or possibly used as a
stack object type, both of which are ok.

Change-Id: Ibddf8f5c8c254d32cb5ebcaca7dc94b4c00ab893
Reviewed-on: https://go-review.googlesource.com/c/go/+/337231
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
2021-07-24 22:49:24 +00:00

82 lines
1.3 KiB
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
func f[T any](x interface{}) T {
return x.(T)
}
func f2[T any](x interface{}) (T, bool) {
t, ok := x.(T)
return t, ok
}
type I interface {
foo()
}
type myint int
func (myint) foo() {
}
type myfloat float64
func (myfloat) foo() {
}
func g[T I](x I) T {
return x.(T)
}
func g2[T I](x I) (T, bool) {
t, ok := x.(T)
return t, ok
}
func h[T any](x interface{}) struct{a, b T} {
return x.(struct{a, b T})
}
func k[T any](x interface{}) interface { bar() T } {
return x.(interface{bar() T })
}
type mybar int
func (x mybar) bar() int {
return int(x)
}
func main() {
var i interface{} = int(3)
var j I = myint(3)
var x interface{} = float64(3)
var y I = myfloat(3)
println(f[int](i))
shouldpanic(func() { f[int](x) })
println(f2[int](i))
println(f2[int](x))
println(g[myint](j))
shouldpanic(func() { g[myint](y) })
println(g2[myint](j))
println(g2[myint](y))
println(h[int](struct{a, b int}{3, 5}).a)
println(k[int](mybar(3)).bar())
}
func shouldpanic(x func()) {
defer func() {
e := recover()
if e == nil {
panic("didn't panic")
}
}()
x()
}