]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: rename Named.under to Named.resolveUnderlying
authorRobert Griesemer <gri@google.com>
Thu, 16 Oct 2025 23:58:25 +0000 (16:58 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 22 Oct 2025 15:20:32 +0000 (08:20 -0700)
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>
src/cmd/compile/internal/types2/named.go
src/go/types/named.go

index 8f3249e44ed99eaded9fe525f74408516322941d..856f33028a41a14cd20451ffe6a29a4cff9f87e5 100644 (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) {
index 35ca325db2554d00fe41e30b4bc89e88e7879826..5c5c837987539cba882209dbfc1ccd8e918bc696 100644 (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) {