mirror of
https://github.com/golang/go
synced 2024-11-24 08:50:14 -07:00
go/types: rename isNamed predicate to hasName
This is a clean port of CL 358621 to go/types. Change-Id: I4e858b1b70cff69b6e0e76bb8a58a70ff54990c9 Reviewed-on: https://go-review.googlesource.com/c/go/+/360755 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
1012bc810f
commit
60fd3ed2b1
@ -267,9 +267,8 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
|
|||||||
// Vu is typed
|
// Vu is typed
|
||||||
|
|
||||||
// x's type V and T have identical underlying types
|
// x's type V and T have identical underlying 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.
|
||||||
// and neither is a type parameter.
|
if Identical(Vu, Tu) && (!hasName(V) || !hasName(T)) {
|
||||||
if Identical(Vu, Tu) && (!isNamed(V) || !isNamed(T)) && Vp == nil && Tp == nil {
|
|
||||||
return true, 0
|
return true, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,10 +295,10 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
|
|||||||
|
|
||||||
// x is a bidirectional channel value, T is a channel
|
// x is a bidirectional channel value, T is a channel
|
||||||
// type, x's type V and T have identical element types,
|
// 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 Vc, ok := Vu.(*Chan); ok && Vc.dir == SendRecv {
|
||||||
if Tc, ok := Tu.(*Chan); ok && Identical(Vc.elem, Tc.elem) {
|
if Tc, ok := Tu.(*Chan); ok && Identical(Vc.elem, Tc.elem) {
|
||||||
return !isNamed(V) || !isNamed(T), _InvalidChanAssign
|
return !hasName(V) || !hasName(T), _InvalidChanAssign
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,9 +8,10 @@ package types
|
|||||||
|
|
||||||
import "go/token"
|
import "go/token"
|
||||||
|
|
||||||
// isNamed reports whether typ has a name.
|
// hasName reports whether typ has a name. This includes
|
||||||
// isNamed may be called with types that are not fully set up.
|
// predeclared types, defined types, and type parameters.
|
||||||
func isNamed(typ Type) bool {
|
// hasName may be called with types that are not fully set up.
|
||||||
|
func hasName(typ Type) bool {
|
||||||
switch typ.(type) {
|
switch typ.(type) {
|
||||||
case *Basic, *Named, *TypeParam:
|
case *Basic, *Named, *TypeParam:
|
||||||
return true
|
return true
|
||||||
|
8
src/go/types/testdata/spec/assignability.go2
vendored
8
src/go/types/testdata/spec/assignability.go2
vendored
@ -33,8 +33,9 @@ func _[TP any](X TP) {
|
|||||||
X = X
|
X = X
|
||||||
}
|
}
|
||||||
|
|
||||||
// "x's type V and T have identical underlying types and at least one
|
// "x's type V and T have identical underlying types
|
||||||
// of V or T is not a defined type and neither is a type parameter"
|
// 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) {
|
func _[TP1, TP2 Interface](X1 TP1, X2 TP2) {
|
||||||
b = B // ERROR cannot use B .* as int value
|
b = B // ERROR cannot use B .* as int value
|
||||||
a = A
|
a = A
|
||||||
@ -69,7 +70,8 @@ func _[TP Interface](X TP) {
|
|||||||
X = i // ERROR cannot use i .* as TP value
|
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 (
|
type (
|
||||||
_SendChan = chan<- int
|
_SendChan = chan<- int
|
||||||
_RecvChan = <-chan int
|
_RecvChan = <-chan int
|
||||||
|
@ -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
|
// If exact unification is known to fail because we attempt to
|
||||||
// match a type name against an unnamed type literal, consider
|
// match a type name against an unnamed type literal, consider
|
||||||
// the underlying type of the named type.
|
// 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
|
// basic types and type parameters. We use asNamed because we only
|
||||||
// want *Named types.)
|
// want *Named types.)
|
||||||
switch {
|
switch {
|
||||||
case !isNamed(x) && y != nil && asNamed(y) != nil:
|
case !hasName(x) && y != nil && asNamed(y) != nil:
|
||||||
return u.nify(x, under(y), p)
|
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)
|
return u.nify(under(x), y, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user