return // not an interface
}
- // The unified IR importer always sets interface method receiver
- // parameters to point to the Interface type, rather than the Named.
- // See #49906.
- var want types.Type = named
- if goexperiment.Unified {
- want = iface
- }
-
// check explicitly declared methods
for i := 0; i < iface.NumExplicitMethods(); i++ {
m := iface.ExplicitMethod(i)
t.Errorf("%s: missing receiver type", m)
continue
}
- if recv.Type() != want {
- t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), want)
+ if recv.Type() != named {
+ t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
}
}
named.SetTypeParams(r.typeParamNames())
- // TODO(mdempsky): Rewrite receiver types to underlying is an
- // Interface? The go/types importer does this (I think because
- // unit tests expected that), but cmd/compile doesn't care
- // about it, so maybe we can avoid worrying about that here.
rhs := r.typ()
pk := r.p
pk.laterFor(named, func() {
f() // initialize RHS
}
underlying := rhs.Underlying()
+
+ // If the underlying type is an interface, we need to
+ // duplicate its methods so we can replace the receiver
+ // parameter's type (#49906).
+ if iface, ok := underlying.(*types.Interface); ok && iface.NumExplicitMethods() != 0 {
+ methods := make([]*types.Func, iface.NumExplicitMethods())
+ for i := range methods {
+ fn := iface.ExplicitMethod(i)
+ sig := fn.Type().(*types.Signature)
+
+ recv := types.NewVar(fn.Pos(), fn.Pkg(), "", named)
+ methods[i] = types.NewFunc(fn.Pos(), fn.Pkg(), fn.Name(), types.NewSignature(recv, sig.Params(), sig.Results(), sig.Variadic()))
+ }
+
+ embeds := make([]types.Type, iface.NumEmbeddeds())
+ for i := range embeds {
+ embeds[i] = iface.EmbeddedType(i)
+ }
+
+ underlying = types.NewInterfaceType(methods, embeds)
+ }
+
named.SetUnderlying(underlying)
})
"go/importer"
"go/parser"
"go/token"
- "internal/goexperiment"
"internal/testenv"
"strings"
"testing"
// expr is an identifier or selector expression that is passed
// to CheckExpr at the position of the comment, and object is
// the string form of the object it denotes.
- src := `
+ const src = `
package p
import "fmt"
return S{}
}`
- // The unified IR importer always sets interface method receiver
- // parameters to point to the Interface type, rather than the Named.
- // See #49906.
- if goexperiment.Unified {
- src = strings.ReplaceAll(src, "func (fmt.Stringer).", "func (interface).")
- }
-
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "p", src, parser.ParseComments)
if err != nil {