mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
go/types, types2: rename Named.under to Named.resolveUnderlying
Named.resolveUnderlying is now just a helper function for Underlying and only called from there. The name makes is clearer what this function does; it also doesn't need to return a result anymore. While at it, slightly simplify the function body. Change-Id: I167c4be89b1bfcc69f6b528ddb6ed4c90481194a Reviewed-on: https://go-review.googlesource.com/c/go/+/712521 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
29d43df8ab
commit
4bdb55b5b8
2 changed files with 28 additions and 30 deletions
|
|
@ -335,7 +335,7 @@ func (n *Named) cleanup() {
|
||||||
// Instances can have a nil underlying at the end of type checking — they
|
// Instances can have a nil underlying at the end of type checking — they
|
||||||
// will lazily expand it as needed. All other types must have one.
|
// will lazily expand it as needed. All other types must have one.
|
||||||
if n.inst == nil {
|
if n.inst == nil {
|
||||||
n.resolve().under()
|
n.Underlying()
|
||||||
}
|
}
|
||||||
n.check = nil
|
n.check = nil
|
||||||
}
|
}
|
||||||
|
|
@ -562,7 +562,8 @@ func (n *Named) Underlying() Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n.under()
|
n.resolveUnderlying()
|
||||||
|
return n.underlying
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Named) String() string { return TypeString(t, nil) }
|
func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
|
|
@ -573,7 +574,7 @@ func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
// TODO(rfindley): reorganize the loading and expansion methods under this
|
// TODO(rfindley): reorganize the loading and expansion methods under this
|
||||||
// heading.
|
// heading.
|
||||||
|
|
||||||
// under returns the (possibly expanded) underlying type of n.
|
// resolveUnderlying computes the underlying type of n.
|
||||||
//
|
//
|
||||||
// It does so by following RHS type chains. If a type literal is found, each
|
// It does so by following RHS type chains. If a type literal is found, each
|
||||||
// named type in the chain has its underlying set to that type. Aliases are
|
// named type in the chain has its underlying set to that type. Aliases are
|
||||||
|
|
@ -581,24 +582,22 @@ func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
//
|
//
|
||||||
// This function also checks for instantiated layout cycles, which are
|
// This function also checks for instantiated layout cycles, which are
|
||||||
// reachable only in the case where resolve() expanded an instantiated
|
// reachable only in the case where resolve() expanded an instantiated
|
||||||
// type which became self-referencing without indirection. If such a
|
// type which became self-referencing without indirection.
|
||||||
// cycle is found, the result is Typ[Invalid]; if n.check != nil, the
|
// If such a cycle is found, the underlying type is set to Typ[Invalid]
|
||||||
// cycle is also reported.
|
// and a cycle is reported.
|
||||||
func (n *Named) under() Type {
|
func (n *Named) resolveUnderlying() {
|
||||||
assert(n.stateHas(resolved))
|
assert(n.stateHas(resolved))
|
||||||
|
|
||||||
// optimization for likely case
|
// optimization for likely case
|
||||||
if n.stateHas(underlying) {
|
if n.stateHas(underlying) {
|
||||||
return n.underlying
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var rhs Type = n
|
|
||||||
var u Type
|
|
||||||
|
|
||||||
seen := make(map[*Named]int)
|
seen := make(map[*Named]int)
|
||||||
var path []Object
|
var path []Object
|
||||||
|
|
||||||
for u == nil {
|
var u Type
|
||||||
|
for rhs := Type(n); u == nil; {
|
||||||
switch t := rhs.(type) {
|
switch t := rhs.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
u = Typ[Invalid]
|
u = Typ[Invalid]
|
||||||
|
|
@ -608,6 +607,8 @@ func (n *Named) under() Type {
|
||||||
|
|
||||||
case *Named:
|
case *Named:
|
||||||
if i, ok := seen[t]; ok {
|
if i, ok := seen[t]; ok {
|
||||||
|
// Note: This code may only be called during type checking,
|
||||||
|
// hence n.check != nil.
|
||||||
n.check.cycleError(path[i:], firstInSrc(path[i:]))
|
n.check.cycleError(path[i:], firstInSrc(path[i:]))
|
||||||
u = Typ[Invalid]
|
u = Typ[Invalid]
|
||||||
break
|
break
|
||||||
|
|
@ -635,13 +636,11 @@ func (n *Named) under() Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// go back up the chain
|
// set underlying for all Named types in the chain
|
||||||
for t := range seen {
|
for t := range seen {
|
||||||
t.underlying = u
|
t.underlying = u
|
||||||
t.setState(underlying)
|
t.setState(underlying)
|
||||||
}
|
}
|
||||||
|
|
||||||
return u
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
|
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
|
||||||
|
|
|
||||||
|
|
@ -338,7 +338,7 @@ func (n *Named) cleanup() {
|
||||||
// Instances can have a nil underlying at the end of type checking — they
|
// Instances can have a nil underlying at the end of type checking — they
|
||||||
// will lazily expand it as needed. All other types must have one.
|
// will lazily expand it as needed. All other types must have one.
|
||||||
if n.inst == nil {
|
if n.inst == nil {
|
||||||
n.resolve().under()
|
n.Underlying()
|
||||||
}
|
}
|
||||||
n.check = nil
|
n.check = nil
|
||||||
}
|
}
|
||||||
|
|
@ -565,7 +565,8 @@ func (n *Named) Underlying() Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n.under()
|
n.resolveUnderlying()
|
||||||
|
return n.underlying
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Named) String() string { return TypeString(t, nil) }
|
func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
|
|
@ -576,7 +577,7 @@ func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
// TODO(rfindley): reorganize the loading and expansion methods under this
|
// TODO(rfindley): reorganize the loading and expansion methods under this
|
||||||
// heading.
|
// heading.
|
||||||
|
|
||||||
// under returns the (possibly expanded) underlying type of n.
|
// resolveUnderlying computes the underlying type of n.
|
||||||
//
|
//
|
||||||
// It does so by following RHS type chains. If a type literal is found, each
|
// It does so by following RHS type chains. If a type literal is found, each
|
||||||
// named type in the chain has its underlying set to that type. Aliases are
|
// named type in the chain has its underlying set to that type. Aliases are
|
||||||
|
|
@ -584,24 +585,22 @@ func (t *Named) String() string { return TypeString(t, nil) }
|
||||||
//
|
//
|
||||||
// This function also checks for instantiated layout cycles, which are
|
// This function also checks for instantiated layout cycles, which are
|
||||||
// reachable only in the case where resolve() expanded an instantiated
|
// reachable only in the case where resolve() expanded an instantiated
|
||||||
// type which became self-referencing without indirection. If such a
|
// type which became self-referencing without indirection.
|
||||||
// cycle is found, the result is Typ[Invalid]; if n.check != nil, the
|
// If such a cycle is found, the underlying type is set to Typ[Invalid]
|
||||||
// cycle is also reported.
|
// and a cycle is reported.
|
||||||
func (n *Named) under() Type {
|
func (n *Named) resolveUnderlying() {
|
||||||
assert(n.stateHas(resolved))
|
assert(n.stateHas(resolved))
|
||||||
|
|
||||||
// optimization for likely case
|
// optimization for likely case
|
||||||
if n.stateHas(underlying) {
|
if n.stateHas(underlying) {
|
||||||
return n.underlying
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var rhs Type = n
|
|
||||||
var u Type
|
|
||||||
|
|
||||||
seen := make(map[*Named]int)
|
seen := make(map[*Named]int)
|
||||||
var path []Object
|
var path []Object
|
||||||
|
|
||||||
for u == nil {
|
var u Type
|
||||||
|
for rhs := Type(n); u == nil; {
|
||||||
switch t := rhs.(type) {
|
switch t := rhs.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
u = Typ[Invalid]
|
u = Typ[Invalid]
|
||||||
|
|
@ -611,6 +610,8 @@ func (n *Named) under() Type {
|
||||||
|
|
||||||
case *Named:
|
case *Named:
|
||||||
if i, ok := seen[t]; ok {
|
if i, ok := seen[t]; ok {
|
||||||
|
// Note: This code may only be called during type checking,
|
||||||
|
// hence n.check != nil.
|
||||||
n.check.cycleError(path[i:], firstInSrc(path[i:]))
|
n.check.cycleError(path[i:], firstInSrc(path[i:]))
|
||||||
u = Typ[Invalid]
|
u = Typ[Invalid]
|
||||||
break
|
break
|
||||||
|
|
@ -638,13 +639,11 @@ func (n *Named) under() Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// go back up the chain
|
// set underlying for all Named types in the chain
|
||||||
for t := range seen {
|
for t := range seen {
|
||||||
t.underlying = u
|
t.underlying = u
|
||||||
t.setState(underlying)
|
t.setState(underlying)
|
||||||
}
|
}
|
||||||
|
|
||||||
return u
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
|
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue