]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: adjust printing of type parameter in error
authorRobert Griesemer <gri@golang.org>
Mon, 1 Nov 2021 20:41:32 +0000 (13:41 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 2 Nov 2021 16:12:23 +0000 (16:12 +0000)
For constraint type inference failures where the type parameter doesn't
match the constraint, print the type parameter rather than its type name
object. This provides more flexibility for improving the error message
down the road.

Change-Id: I188871d6f26a16cd96e59770966a1ec65607b128
Reviewed-on: https://go-review.googlesource.com/c/go/+/360514
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/infer.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue45985.go2

index 494e896ee972332b51530eeae9813a2678fa51da..24c461f1c368af3bcb61aacdec9fed35f83d1902 100644 (file)
@@ -378,7 +378,6 @@ func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type,
 
        // If a constraint has a structural type, unify the corresponding type parameter with it.
        for _, tpar := range tparams {
-               typ := tpar
                sbound := structure(tpar)
                if sbound != nil {
                        // If the structural type is the underlying type of a single
@@ -386,8 +385,10 @@ func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type,
                        if named, _ := tpar.singleType().(*Named); named != nil {
                                sbound = named
                        }
-                       if !u.unify(typ, sbound) {
-                               check.errorf(tpar.obj, "%s does not match %s", tpar.obj, sbound)
+                       if !u.unify(tpar, sbound) {
+                               // TODO(gri) improve error message by providing the type arguments
+                               //           which we know already
+                               check.errorf(tpar.obj, "%s does not match %s", tpar, sbound)
                                return nil, 0
                        }
                }
index ee5282d6ef3489fd07ba4f60cba5a72333a6409a..9963d2ee003c165774539cca29614f05147908e9 100644 (file)
@@ -5,7 +5,7 @@
 package issue45985
 
 // TODO(gri): this error should be on app[int] below.
-func app[S /* ERROR "type S S does not match" */ interface{ ~[]T }, T any](s S, e T) S {
+func app[S /* ERROR "S does not match" */ interface{ ~[]T }, T any](s S, e T) S {
     return append(s, e)
 }