diff options
author | Keith Randall <khr@golang.org> | 2021-06-04 22:54:08 -0700 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2021-06-07 19:53:38 +0000 |
commit | cf4b6dc48eba807e7d85fb6ab30cbbbdb143c552 (patch) | |
tree | d7cfb7cb1d50d079b600dda8d81028b8545c5e02 /src/cmd/compile/internal/typecheck/subr.go | |
parent | bcb3927cb51af39f44d810aab809dff27c950697 (diff) | |
download | go-cf4b6dc48eba807e7d85fb6ab30cbbbdb143c552.tar.gz go-cf4b6dc48eba807e7d85fb6ab30cbbbdb143c552.zip |
[dev.typeparams] cmd/compile: allow conversions from type parameter to interface
When converting from a type param to an interface, allow it if
the type bound implements that interface.
Query: some conversions go through this path, some use another path?
The test does
var i interface{foo()int} = x
but
i := (interface{foo()int})(x)
works at tip.
Change-Id: I84d497e5228c0e1d1c9d76ffebaedce09dc45e8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/325409
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>
Diffstat (limited to 'src/cmd/compile/internal/typecheck/subr.go')
-rw-r--r-- | src/cmd/compile/internal/typecheck/subr.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/typecheck/subr.go b/src/cmd/compile/internal/typecheck/subr.go index e9a9a57126..0e306eaea8 100644 --- a/src/cmd/compile/internal/typecheck/subr.go +++ b/src/cmd/compile/internal/typecheck/subr.go @@ -723,13 +723,23 @@ func ifacelookdot(s *types.Sym, t *types.Type, ignorecase bool) (m *types.Field, return m, followptr } +// implements reports whether t implements the interface iface. t can be +// an interface, a type parameter, or a concrete type. If implements returns +// false, it stores a method of iface that is not implemented in *m. If the +// method name matches but the type is wrong, it additionally stores the type +// of the method (on t) in *samename. func implements(t, iface *types.Type, m, samename **types.Field, ptr *int) bool { t0 := t if t == nil { return false } - if t.IsInterface() { + if t.IsInterface() || t.IsTypeParam() { + if t.IsTypeParam() { + // A typeparam satisfies an interface if its type bound + // has all the methods of that interface. + t = t.Bound() + } i := 0 tms := t.AllMethods().Slice() for _, im := range iface.AllMethods().Slice() { |