diff options
| author | korzhao <korzhao95@gmail.com> | 2021-08-16 22:25:47 +0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2021-09-01 16:45:26 +0000 |
| commit | 5670ff4ae593019c33b8095a6341deb657be34d7 (patch) | |
| tree | dc6e903d69f4a4348952a5ad7385cae0cfcb68c4 /test/typeparam | |
| parent | 717f337d1d68299ca55a96e9fbb5168e8f7788f4 (diff) | |
| download | go-5670ff4ae593019c33b8095a6341deb657be34d7.tar.xz | |
cmd/compile: fix conversions from TypeParam to interface
If the TypeParam has all the methods of an interface, allow conversions from TypeParam to interface
Fixes #47708
Change-Id: I40a82a31f6ea9354130dbe3bcfc83537094bf12c
Reviewed-on: https://go-review.googlesource.com/c/go/+/342509
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Trust: Keith Randall <khr@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'test/typeparam')
| -rw-r--r-- | test/typeparam/issue47708.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/typeparam/issue47708.go b/test/typeparam/issue47708.go new file mode 100644 index 0000000000..261d6efb61 --- /dev/null +++ b/test/typeparam/issue47708.go @@ -0,0 +1,40 @@ +// 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 ( + "fmt" +) + +type FooType[T any] interface { + Foo(BarType[T])string +} +type BarType[T any] interface { + Bar(FooType[T])string +} + +type Baz[T any] T +func (l Baz[T]) Foo(v BarType[T]) string { + return v.Bar(l) +} +type Bob[T any] T +func (l Bob[T]) Bar(v FooType[T]) string { + if v,ok := v.(Baz[T]);ok{ + return fmt.Sprintf("%v%v",v,l) + } + return "" +} + + +func main() { + var baz Baz[int] = 123 + var bob Bob[int] = 456 + + if got, want := baz.Foo(bob), "123456"; got != want { + panic(fmt.Sprintf("got %s want %s", got, want)) + } +} |
