]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: rename isNamed predicate to hasName
authorRobert Griesemer <gri@golang.org>
Tue, 26 Oct 2021 04:43:07 +0000 (21:43 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 27 Oct 2021 20:24:56 +0000 (20:24 +0000)
isNamed(t) is easily confused with asNamed(t) != nil (e.g., we
have isPointer(t) that is defined as asPointer(t) != nil).

This rename also helped clarifying a couple of places in the
assignability rules where it makes sense to simply look for
types that have names.

Change-Id: Ie995908613a26883ffe0562343d297a1e981e9ef
Reviewed-on: https://go-review.googlesource.com/c/go/+/358621
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/operand.go
src/cmd/compile/internal/types2/predicates.go
src/cmd/compile/internal/types2/testdata/spec/assignability.go2
src/cmd/compile/internal/types2/unify.go

index 92ae0a95fc507dbc06154f3deb1e681b4f2451c8..e4db0554f3e4c8a5bcc1aee74f819bfafd719636 100644 (file)
@@ -282,9 +282,8 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
        // Vu is typed
 
        // x's type V and T have identical underlying types
-       // and at least one of V or T is not a named type
-       // and neither is a type parameter.
-       if Identical(Vu, Tu) && (!isNamed(V) || !isNamed(T)) && Vp == nil && Tp == nil {
+       // and at least one of V or T is not a named type.
+       if Identical(Vu, Tu) && (!hasName(V) || !hasName(T)) {
                return true, 0
        }
 
@@ -311,10 +310,10 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
 
        // x is a bidirectional channel value, T is a channel
        // type, x's type V and T have identical element types,
-       // and at least one of V or T is not a named type
+       // and at least one of V or T is not a named type.
        if Vc, ok := Vu.(*Chan); ok && Vc.dir == SendRecv {
                if Tc, ok := Tu.(*Chan); ok && Identical(Vc.elem, Tc.elem) {
-                       return !isNamed(V) || !isNamed(T), _InvalidChanAssign
+                       return !hasName(V) || !hasName(T), _InvalidChanAssign
                }
        }
 
index 4faa09ebd0dc2384d0fe4dac54911746a2ce51e3..409715ad9d4479dc24067b7cfd72b37204580de2 100644 (file)
@@ -6,9 +6,10 @@
 
 package types2
 
-// isNamed reports whether typ has a name.
-// isNamed may be called with types that are not fully set up.
-func isNamed(typ Type) bool {
+// hasName reports whether typ has a name. This includes
+// predeclared types, defined types, and type parameters.
+// hasName may be called with types that are not fully set up.
+func hasName(typ Type) bool {
        switch typ.(type) {
        case *Basic, *Named, *TypeParam:
                return true
index 1507cabb1d2879ece08a3a2356fe695908dcce94..ab8f9c08b26a257f1783f928b79b1e985f02f7b4 100644 (file)
@@ -33,8 +33,9 @@ func _[TP any](X TP) {
        X = X
 }
 
-// "x's type V and T have identical underlying types and at least one
-// of V or T is not a defined type and neither is a type parameter"
+// "x's type V and T have identical underlying types
+// and at least one of V or T is not a named type."
+// (here a named type is a type with a name)
 func _[TP1, TP2 Interface](X1 TP1, X2 TP2) {
        b = B // ERROR cannot use B .* as int value
        a = A
@@ -69,7 +70,8 @@ func _[TP Interface](X TP) {
        X = i // ERROR cannot use i .* as TP value
 }
 
-// "x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a defined type"
+// "x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a named type"
+// (here a named type is a type with a name)
 type (
        _SendChan = chan<- int
        _RecvChan = <-chan int
index a252c5e1a56973e4cfeb8549d0eb92dd1fdc8863..7f636c30d3f9a5c598075865a058721bbfa9ae75 100644 (file)
@@ -235,13 +235,13 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
                // If exact unification is known to fail because we attempt to
                // match a type name against an unnamed type literal, consider
                // the underlying type of the named type.
-               // (Subtle: We use isNamed to include any type with a name (incl.
+               // (Subtle: We use hasName to include any type with a name (incl.
                // basic types and type parameters. We use asNamed because we only
                // want *Named types.)
                switch {
-               case !isNamed(x) && y != nil && asNamed(y) != nil:
+               case !hasName(x) && y != nil && asNamed(y) != nil:
                        return u.nify(x, under(y), p)
-               case x != nil && asNamed(x) != nil && !isNamed(y):
+               case x != nil && asNamed(x) != nil && !hasName(y):
                        return u.nify(under(x), y, p)
                }
        }