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:
Robert Griesemer 2025-10-16 16:58:25 -07:00 committed by Gopher Robot
parent 29d43df8ab
commit 4bdb55b5b8
2 changed files with 28 additions and 30 deletions

View file

@ -335,7 +335,7 @@ func (n *Named) cleanup() {
// 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.
if n.inst == nil {
n.resolve().under()
n.Underlying()
}
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) }
@ -573,7 +574,7 @@ func (t *Named) String() string { return TypeString(t, nil) }
// TODO(rfindley): reorganize the loading and expansion methods under this
// 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
// 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
// reachable only in the case where resolve() expanded an instantiated
// type which became self-referencing without indirection. If such a
// cycle is found, the result is Typ[Invalid]; if n.check != nil, the
// cycle is also reported.
func (n *Named) under() Type {
// type which became self-referencing without indirection.
// If such a cycle is found, the underlying type is set to Typ[Invalid]
// and a cycle is reported.
func (n *Named) resolveUnderlying() {
assert(n.stateHas(resolved))
// optimization for likely case
if n.stateHas(underlying) {
return n.underlying
return
}
var rhs Type = n
var u Type
seen := make(map[*Named]int)
var path []Object
for u == nil {
var u Type
for rhs := Type(n); u == nil; {
switch t := rhs.(type) {
case nil:
u = Typ[Invalid]
@ -608,6 +607,8 @@ func (n *Named) under() Type {
case *Named:
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:]))
u = Typ[Invalid]
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 {
t.underlying = u
t.setState(underlying)
}
return u
}
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {

View file

@ -338,7 +338,7 @@ func (n *Named) cleanup() {
// 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.
if n.inst == nil {
n.resolve().under()
n.Underlying()
}
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) }
@ -576,7 +577,7 @@ func (t *Named) String() string { return TypeString(t, nil) }
// TODO(rfindley): reorganize the loading and expansion methods under this
// 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
// 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
// reachable only in the case where resolve() expanded an instantiated
// type which became self-referencing without indirection. If such a
// cycle is found, the result is Typ[Invalid]; if n.check != nil, the
// cycle is also reported.
func (n *Named) under() Type {
// type which became self-referencing without indirection.
// If such a cycle is found, the underlying type is set to Typ[Invalid]
// and a cycle is reported.
func (n *Named) resolveUnderlying() {
assert(n.stateHas(resolved))
// optimization for likely case
if n.stateHas(underlying) {
return n.underlying
return
}
var rhs Type = n
var u Type
seen := make(map[*Named]int)
var path []Object
for u == nil {
var u Type
for rhs := Type(n); u == nil; {
switch t := rhs.(type) {
case nil:
u = Typ[Invalid]
@ -611,6 +610,8 @@ func (n *Named) under() Type {
case *Named:
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:]))
u = Typ[Invalid]
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 {
t.underlying = u
t.setState(underlying)
}
return u
}
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {