]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: simplify unification with constraints
authorRobert Griesemer <gri@golang.org>
Fri, 24 Feb 2023 02:55:11 +0000 (18:55 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 1 Mar 2023 20:55:39 +0000 (20:55 +0000)
Change-Id: I399f0ac12e65713f3018a89da55ecd3cdb855c50
Reviewed-on: https://go-review.googlesource.com/c/go/+/471017
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

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

index 1aa8e31f5d979e296b097a25051c045369954aee..4d842fa3882a9f6122e36d897ba8405f14f89688 100644 (file)
@@ -198,16 +198,16 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
                                tx := u.at(tpar)
                                switch {
                                case tx != nil:
-                                       // The corresponding type argument tx is known.
-                                       // In this case, if the core type has a tilde, the type argument's underlying
-                                       // type must match the core type, otherwise the type argument and the core type
-                                       // must match.
-                                       // If tx is an (external) type parameter, don't consider its underlying type
-                                       // (which is an interface). The unifier will use the type parameter's core
-                                       // type automatically.
-                                       if core.tilde && !isTypeParam(tx) {
-                                               tx = under(tx)
-                                       }
+                                       // The corresponding type argument tx is known. There are 2 cases:
+                                       // 1) If the core type has a tilde, per spec requirement for tilde
+                                       //    elements, the core type is an underlying (literal) type.
+                                       //    And because of the tilde, the underlying type of tx must match
+                                       //    against the core type.
+                                       //    But because unify automatically matches a defined type against
+                                       //    an underlying literal type, we can simply unify tx with the
+                                       //    core type.
+                                       // 2) If the core type doesn't have a tilde, we also must unify tx
+                                       //    with the core type.
                                        if !u.unify(tx, core.typ) {
                                                check.errorf(pos, CannotInferTypeArgs, "%s does not match %s", tpar, core.typ)
                                                return nil
index af1001cd3c475ddd1b16bc4e5a3070d4b8628cf8..59f982b584fc7bb308aa13f5360a8918602b1ecd 100644 (file)
@@ -200,16 +200,16 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
                                tx := u.at(tpar)
                                switch {
                                case tx != nil:
-                                       // The corresponding type argument tx is known.
-                                       // In this case, if the core type has a tilde, the type argument's underlying
-                                       // type must match the core type, otherwise the type argument and the core type
-                                       // must match.
-                                       // If tx is an (external) type parameter, don't consider its underlying type
-                                       // (which is an interface). The unifier will use the type parameter's core
-                                       // type automatically.
-                                       if core.tilde && !isTypeParam(tx) {
-                                               tx = under(tx)
-                                       }
+                                       // The corresponding type argument tx is known. There are 2 cases:
+                                       // 1) If the core type has a tilde, per spec requirement for tilde
+                                       //    elements, the core type is an underlying (literal) type.
+                                       //    And because of the tilde, the underlying type of tx must match
+                                       //    against the core type.
+                                       //    But because unify automatically matches a defined type against
+                                       //    an underlying literal type, we can simply unify tx with the
+                                       //    core type.
+                                       // 2) If the core type doesn't have a tilde, we also must unify tx
+                                       //    with the core type.
                                        if !u.unify(tx, core.typ) {
                                                check.errorf(posn, CannotInferTypeArgs, "%s does not match %s", tpar, core.typ)
                                                return nil