From 3b5918c757eb32b4a05a0b4ba4bbab001175ebf0 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 15 Dec 2020 17:48:51 -0800 Subject: [PATCH] [dev.typeparams] cmd/compile/internal/types2: review of predicates.go Make predicates.go match the corresponding and reviewed go/types version. The remaining diffs are due to the difference in the implementations of the type conversion methods/functions: $ diff $GOROOT/src/cmd/compile/internal/types2/predicates.go $GOROOT/src/go/types/predicates.go 7c7 < package types2 --- > package types 9a10 > "go/token" 32c33 < switch t := optype(typ.Under()).(type) { --- > switch t := optype(typ).(type) { 63c64 < // set up. Must not call Basic()! --- > // set up. Must not call asBasic()! 79c80 < t := typ.Basic() --- > t := asBasic(typ) 85c86 < return typ.Interface() != nil --- > return asInterface(typ) != nil 110c111 < if t := T.TypeParam(); t != nil && optype(t) == theTop { --- > if t := asTypeParam(T); t != nil && optype(t) == theTop { 114c115 < switch t := optype(T.Under()).(type) { --- > switch t := optype(T).(type) { 143c144 < switch t := optype(typ.Under()).(type) { --- > switch t := optype(typ).(type) { 300,301c301,302 < check.completeInterface(nopos, x) < check.completeInterface(nopos, y) --- > check.completeInterface(token.NoPos, x) > check.completeInterface(token.NoPos, y) Change-Id: I174d8a8a22fbd8814ede25002cb2705588912329 Reviewed-on: https://go-review.googlesource.com/c/go/+/278474 Trust: Robert Griesemer Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/predicates.go | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/cmd/compile/internal/types2/predicates.go b/src/cmd/compile/internal/types2/predicates.go index 048519471c..b910d8d0ee 100644 --- a/src/cmd/compile/internal/types2/predicates.go +++ b/src/cmd/compile/internal/types2/predicates.go @@ -1,4 +1,3 @@ -// UNREVIEWED // Copyright 2012 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. @@ -21,7 +20,8 @@ func isNamed(typ Type) bool { return false } -// isGeneric reports whether a type is a generic, uninstantiated type (generic signatures are not included). +// isGeneric reports whether a type is a generic, uninstantiated type (generic +// signatures are not included). func isGeneric(typ Type) bool { // A parameterized type is only instantiated if it doesn't have an instantiation already. named, _ := typ.(*Named) @@ -90,9 +90,16 @@ func Comparable(T Type) bool { return comparable(T, nil) } -// comparable should only be called by Comparable. func comparable(T Type, seen map[Type]bool) bool { - // If T is a type parameter not constraint by any type + if seen[T] { + return true + } + if seen == nil { + seen = make(map[Type]bool) + } + seen[T] = true + + // If T is a type parameter not constrained by any type // list (i.e., it's underlying type is the top type), // T is comparable if it has the == method. Otherwise, // the underlying type "wins". For instance @@ -104,14 +111,6 @@ func comparable(T Type, seen map[Type]bool) bool { return t.Bound().IsComparable() } - if seen[T] { - return true - } - if seen == nil { - seen = make(map[Type]bool) - } - seen[T] = true - switch t := optype(T.Under()).(type) { case *Basic: // assume invalid types to be comparable @@ -129,7 +128,10 @@ func comparable(T Type, seen map[Type]bool) bool { case *Array: return comparable(t.elem, seen) case *Sum: - return t.is(Comparable) + pred := func(t Type) bool { + return comparable(t, seen) + } + return t.is(pred) case *TypeParam: return t.Bound().IsComparable() } -- 2.50.0