Skip to content

Commit 7183e77

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: use exact matching when unifying constraint method signatures
Fixes #60556. Change-Id: I203a0bc79eff607654c3e8350d259e694cb035b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/499995 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
1 parent 936ce87 commit 7183e77

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/cmd/compile/internal/types2/infer.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,15 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
249249
// It must have (at least) all the methods of the type constraint,
250250
// and the method signatures must unify; otherwise tx cannot satisfy
251251
// the constraint.
252+
// TODO(gri) Now that unification handles interfaces, this code can
253+
// be reduced to calling u.unify(tx, tpar.iface(), assign)
254+
// (which will compare signatures exactly as we do below).
255+
// We leave it as is for now because missingMethod provides
256+
// a failure cause which allows for a better error message.
257+
// Eventually, unify should return an error with cause.
252258
var cause string
253259
constraint := tpar.iface()
254-
if m, _ := check.missingMethod(tx, constraint, true, func(x, y Type) bool { return u.unify(x, y, 0) }, &cause); m != nil {
260+
if m, _ := check.missingMethod(tx, constraint, true, func(x, y Type) bool { return u.unify(x, y, exact) }, &cause); m != nil {
255261
// TODO(gri) better error message (see TODO above)
256262
check.errorf(pos, CannotInferTypeArgs, "%s (type %s) does not satisfy %s %s", tpar, tx, tpar.Constraint(), cause)
257263
return nil

src/go/types/infer.go

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
type I[T any] interface {
8+
m(I[T])
9+
}
10+
11+
type S[T any] struct{}
12+
13+
func (S[T]) m(I[T]) {}
14+
15+
func f[T I[E], E any](T) {}
16+
17+
func _() {
18+
f(S[int]{})
19+
}

0 commit comments

Comments
 (0)