From: Robert Griesemer Date: Fri, 24 Feb 2023 02:55:11 +0000 (-0800) Subject: go/types, types2: simplify unification with constraints X-Git-Tag: go1.21rc1~1402 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e2f2123e256094c64377008c17ca20538c4742d8;p=gostls13.git go/types, types2: simplify unification with constraints Change-Id: I399f0ac12e65713f3018a89da55ecd3cdb855c50 Reviewed-on: https://go-review.googlesource.com/c/go/+/471017 Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/infer.go b/src/cmd/compile/internal/types2/infer.go index 1aa8e31f5d..4d842fa388 100644 --- a/src/cmd/compile/internal/types2/infer.go +++ b/src/cmd/compile/internal/types2/infer.go @@ -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 diff --git a/src/go/types/infer.go b/src/go/types/infer.go index af1001cd3c..59f982b584 100644 --- a/src/go/types/infer.go +++ b/src/go/types/infer.go @@ -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