From: Cuong Manh Le Date: Tue, 28 Jun 2022 17:07:37 +0000 (+0700) Subject: [release-branch.go1.18] cmd/compile: fix generic inter-inter comparisons from value... X-Git-Tag: go1.18.4~17 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f86c6b9a32daacef5169bba0d94cbd9da6813bbe;p=gostls13.git [release-branch.go1.18] cmd/compile: fix generic inter-inter comparisons from value switch statements If value is a non-empty interface and has shape, we still need to convert it to an interface{} first. Fixes #53587 Change-Id: I516063ba4429a6cc24c483758387ec13815fc63e Reviewed-on: https://go-review.googlesource.com/c/go/+/414834 Reviewed-by: Matthew Dempsky TryBot-Result: Gopher Robot Reviewed-by: David Chase Run-TryBot: Cuong Manh Le Reviewed-on: https://go-review.googlesource.com/c/go/+/414835 --- diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go index 7cfd70ec54..4c3dd37d94 100644 --- a/src/cmd/compile/internal/noder/stencil.go +++ b/src/cmd/compile/internal/noder/stencil.go @@ -1186,7 +1186,7 @@ func (subst *subster) node(n ir.Node) ir.Node { if m.Tag != nil && m.Tag.Op() == ir.OTYPESW { break // Nothing to do here for type switches. } - if m.Tag != nil && !m.Tag.Type().IsInterface() && m.Tag.Type().HasShape() { + if m.Tag != nil && !m.Tag.Type().IsEmptyInterface() && m.Tag.Type().HasShape() { // To implement a switch on a value that is or has a type parameter, we first convert // that thing we're switching on to an interface{}. m.Tag = assignconvfn(m.Tag, types.Types[types.TINTER]) @@ -1195,7 +1195,7 @@ func (subst *subster) node(n ir.Node) ir.Node { for i, x := range c.List { // If we have a case that is or has a type parameter, convert that case // to an interface{}. - if !x.Type().IsInterface() && x.Type().HasShape() { + if !x.Type().IsEmptyInterface() && x.Type().HasShape() { c.List[i] = assignconvfn(x, types.Types[types.TINTER]) } } diff --git a/test/typeparam/issue53477.go b/test/typeparam/issue53477.go new file mode 100644 index 0000000000..314171a758 --- /dev/null +++ b/test/typeparam/issue53477.go @@ -0,0 +1,34 @@ +// run -gcflags=-G=3 + +// Copyright 2022 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. + +// Test that generic interface-interface comparisons resulting from +// value switch statements are handled correctly. + +package main + +func main() { + f[X](0) +} + +type Mer[T any] interface{ M(T) } +type MNer[T any] interface { + Mer[T] + N() +} + +type X int + +func (X) M(X) {} +func (X) N() {} + +func f[T MNer[T]](t T) { + switch Mer[T](t) { + case MNer[T](t): + // ok + default: + panic("FAIL") + } +}