From f38da2cbb66cadebd3b6887c48919269f37ca69d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 15 Dec 2020 12:17:01 -0800 Subject: [PATCH] [dev.typeparams] cmd/compile/internal/types2: review of unify.go Make unify.go match the corresponding and reviewed go/types version. The remaining differences are due to other differences in the packages. Also, this version of unify opted to preserve the longer comment around case tj > 0. $ diff $GOROOT/src/cmd/compile/internal/types2/unify.go $GOROOT/src/go/types/unify.go 7c7 < package types2 --- > package types 9c9,12 < import "sort" --- > import ( > "go/token" > "sort" > ) 120,123d122 < // This case is handled like the default case. < // case tj > 0: < // // Only the type parameter for y has an inferred type. Use y slot for x. < // u.x.setIndex(i, tj) 125,126c124,125 < // Neither type parameter has an inferred type. Use y slot for x < // (or x slot for y, it doesn't matter). --- > // Either the type parameter for y has an inferred type, or neither type > // parameter has an inferred type. In either case, use y slot for x. 216c215 < // basic types and type parameters. We use Named() because we only --- > // basic types and type parameters. We use asNamed() because we only 219,222c218,221 < case !isNamed(x) && y != nil && y.Named() != nil: < return u.nify(x, y.Under(), p) < case x != nil && x.Named() != nil && !isNamed(y): < return u.nify(x.Under(), y, p) --- > case !isNamed(x) && y != nil && asNamed(y) != nil: > return u.nify(x, under(y), p) > case x != nil && asNamed(x) != nil && !isNamed(y): > return u.nify(under(x), y, p) 353,354c352,353 < u.check.completeInterface(nopos, x) < u.check.completeInterface(nopos, y) --- > u.check.completeInterface(token.NoPos, x) > u.check.completeInterface(token.NoPos, y) Change-Id: Icb246d4befedfa82cc3dcfdb7dd162cd4127fbe9 Reviewed-on: https://go-review.googlesource.com/c/go/+/278572 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Go Bot Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/unify.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cmd/compile/internal/types2/unify.go b/src/cmd/compile/internal/types2/unify.go index 7c639366ef..60ccf625b9 100644 --- a/src/cmd/compile/internal/types2/unify.go +++ b/src/cmd/compile/internal/types2/unify.go @@ -1,4 +1,3 @@ -// UNREVIEWED // Copyright 2020 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -17,7 +16,7 @@ import "sort" // (even if that also contains possibly the same type parameters). This // is crucial to infer the type parameters of self-recursive calls: // -// func f[type P](a P) { f(a) } +// func f[P any](a P) { f(a) } // // For the call f(a) we want to infer that the type argument for P is P. // During unification, the parameter type P must be resolved to the type @@ -63,9 +62,9 @@ type tparamsList struct { unifier *unifier tparams []*TypeName // For each tparams element, there is a corresponding type slot index in indices. - // index < 0: unifier.types[-index] == nil + // index < 0: unifier.types[-index-1] == nil // index == 0: no type slot allocated yet - // index > 0: unifier.types[index] == typ + // index > 0: unifier.types[index-1] == typ // Joined tparams elements share the same type slot and thus have the same index. // By using a negative index for nil types we don't need to check unifier.types // to see if we have a type or not.