Skip to content

Commit 4bdb55b

Browse files
Robert Griesemergopherbot
authored andcommitted
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>
1 parent 29d43df commit 4bdb55b

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/cmd/compile/internal/types2/named.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (n *Named) cleanup() {
335335
// Instances can have a nil underlying at the end of type checking — they
336336
// will lazily expand it as needed. All other types must have one.
337337
if n.inst == nil {
338-
n.resolve().under()
338+
n.Underlying()
339339
}
340340
n.check = nil
341341
}
@@ -562,7 +562,8 @@ func (n *Named) Underlying() Type {
562562
}
563563
}
564564

565-
return n.under()
565+
n.resolveUnderlying()
566+
return n.underlying
566567
}
567568

568569
func (t *Named) String() string { return TypeString(t, nil) }
@@ -573,32 +574,30 @@ func (t *Named) String() string { return TypeString(t, nil) }
573574
// TODO(rfindley): reorganize the loading and expansion methods under this
574575
// heading.
575576

576-
// under returns the (possibly expanded) underlying type of n.
577+
// resolveUnderlying computes the underlying type of n.
577578
//
578579
// It does so by following RHS type chains. If a type literal is found, each
579580
// named type in the chain has its underlying set to that type. Aliases are
580581
// skipped because their underlying type is not memoized.
581582
//
582583
// This function also checks for instantiated layout cycles, which are
583584
// reachable only in the case where resolve() expanded an instantiated
584-
// type which became self-referencing without indirection. If such a
585-
// cycle is found, the result is Typ[Invalid]; if n.check != nil, the
586-
// cycle is also reported.
587-
func (n *Named) under() Type {
585+
// type which became self-referencing without indirection.
586+
// If such a cycle is found, the underlying type is set to Typ[Invalid]
587+
// and a cycle is reported.
588+
func (n *Named) resolveUnderlying() {
588589
assert(n.stateHas(resolved))
589590

590591
// optimization for likely case
591592
if n.stateHas(underlying) {
592-
return n.underlying
593+
return
593594
}
594595

595-
var rhs Type = n
596-
var u Type
597-
598596
seen := make(map[*Named]int)
599597
var path []Object
600598

601-
for u == nil {
599+
var u Type
600+
for rhs := Type(n); u == nil; {
602601
switch t := rhs.(type) {
603602
case nil:
604603
u = Typ[Invalid]
@@ -608,6 +607,8 @@ func (n *Named) under() Type {
608607

609608
case *Named:
610609
if i, ok := seen[t]; ok {
610+
// Note: This code may only be called during type checking,
611+
// hence n.check != nil.
611612
n.check.cycleError(path[i:], firstInSrc(path[i:]))
612613
u = Typ[Invalid]
613614
break
@@ -635,13 +636,11 @@ func (n *Named) under() Type {
635636
}
636637
}
637638

638-
// go back up the chain
639+
// set underlying for all Named types in the chain
639640
for t := range seen {
640641
t.underlying = u
641642
t.setState(underlying)
642643
}
643-
644-
return u
645644
}
646645

647646
func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {

src/go/types/named.go

Lines changed: 14 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)