Skip to content

Commit

Permalink
[dev.typeparams] cmd/compile/internal/types2: review of predicates.go
Browse files Browse the repository at this point in the history
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 <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
griesemer committed Dec 16, 2020
1 parent 09abd23 commit 3b5918c
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions 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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
}
Expand Down

0 comments on commit 3b5918c

Please sign in to comment.