From: Robert Griesemer Date: Thu, 16 Oct 2025 23:58:25 +0000 (-0700) Subject: go/types, types2: rename Named.under to Named.resolveUnderlying X-Git-Tag: go1.26rc1~537 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4bdb55b5b86fc96addd43a845dabf6b76d100202;p=gostls13.git 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 Reviewed-by: Mark Freeman Auto-Submit: Robert Griesemer Reviewed-by: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/named.go b/src/cmd/compile/internal/types2/named.go index 8f3249e44e..856f33028a 100644 --- a/src/cmd/compile/internal/types2/named.go +++ b/src/cmd/compile/internal/types2/named.go @@ -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) { diff --git a/src/go/types/named.go b/src/go/types/named.go index 35ca325db2..5c5c837987 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -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) {