Skip to content

Commit 8401512

Browse files
Mark Freemangopherbot
authored andcommitted
go/types, types2: rename complete namedState to hasMethods
The complete namedState tracks whether an instantiated named type has expanded all of its methods. In the past, this was the terminal state of a linear lifecycle, and so a term like "complete" made sense. Now that we've expanded the lifecycle of named types to a tree structure, the complete namedState is no longer a terminal state, and so the term "complete" is now a bit confusing. This change a) makes the expansion aspect of the complete namedState more explicit and b) removes a misleading suggestion of terminality by changing the name from complete to hasMethods. To take a similar naming convention with the underlying namedState, which signals presence of Named.underlying, we rename the underlying namedState to hasUnder. Change-Id: I29fee26efea3de88c7c1240f2dc53df218acf8b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/713280 Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Mark Freeman <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent cf826bf commit 8401512

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ import (
6868
// arguments from the instantiation. A type may be partially expanded if some
6969
// but not all of these details have been substituted. Similarly, we refer to
7070
// these individual details (RHS or method) as being "expanded".
71-
// - When all information is known for a named type, we say it is "complete".
7271
//
7372
// Some invariants to keep in mind: each declared Named type has a single
7473
// corresponding object, and that object's type is the (possibly generic) Named
@@ -85,8 +84,8 @@ import (
8584
// presence of a cycle of named types, expansion will eventually find an
8685
// existing instance in the Context and short-circuit the expansion.
8786
//
88-
// Once an instance is complete, we can nil out this shared Context to unpin
89-
// memory, though this Context may still be held by other incomplete instances
87+
// Once an instance is fully expanded, we can nil out this shared Context to unpin
88+
// memory, though the Context may still be held by other incomplete instances
9089
// in its "lineage".
9190

9291
// A Named represents a named (defined) type.
@@ -145,11 +144,11 @@ type instance struct {
145144
// unresolved
146145
// loaded
147146
// resolved
148-
// └── complete
149-
// └── underlying
147+
// └── hasMethods
148+
// └── hasUnder
150149
//
151150
// That is, descent down the tree is mostly linear (unresolved through resolved), except upon
152-
// reaching the leaves (complete and underlying). A type may occupy any combination of the
151+
// reaching the leaves (hasMethods and hasUnder). A type may occupy any combination of the
153152
// leaf states at once (they are independent states).
154153
//
155154
// To represent this independence, the set of active states is represented with a bit set. State
@@ -161,19 +160,19 @@ type instance struct {
161160
// 0000 | unresolved
162161
// 1000 | loaded
163162
// 1100 | resolved, which implies loaded
164-
// 1110 | completed, which implies resolved (which in turn implies loaded)
165-
// 1101 | underlying, which implies resolved ...
166-
// 1111 | both completed and underlying which implies resolved ...
163+
// 1110 | hasMethods, which implies resolved (which in turn implies loaded)
164+
// 1101 | hasUnder, which implies resolved ...
165+
// 1111 | both hasMethods and hasUnder which implies resolved ...
167166
//
168167
// To read the state of a named type, use [Named.stateHas]; to write, use [Named.setState].
169168
type stateMask uint32
170169

171170
const (
172171
// before resolved, type parameters, RHS, underlying, and methods might be unavailable
173172
resolved stateMask = 1 << iota // methods might be unexpanded (for instances)
174-
complete // methods are all expanded (for instances)
173+
hasMethods // methods are all expanded (for instances)
175174
loaded // methods are available, but constraints might be unexpanded (for generic types)
176-
underlying // underlying type is available
175+
hasUnder // underlying type is available
177176
)
178177

179178
// NewNamed returns a new named type for the given type name, underlying type, and associated methods.
@@ -229,7 +228,7 @@ func (n *Named) resolve() *Named {
229228
}
230229

231230
// underlying comes after resolving, do not set it
232-
defer (func() { assert(!n.stateHas(underlying)) })()
231+
defer (func() { assert(!n.stateHas(hasUnder)) })()
233232

234233
if n.inst != nil {
235234
assert(n.fromRHS == nil) // instantiated types are not declared types
@@ -242,7 +241,7 @@ func (n *Named) resolve() *Named {
242241
n.tparams = orig.tparams
243242

244243
if len(orig.methods) == 0 {
245-
n.setState(resolved | complete) // nothing further to do
244+
n.setState(resolved | hasMethods) // nothing further to do
246245
n.inst.ctxt = nil
247246
} else {
248247
n.setState(resolved)
@@ -274,7 +273,7 @@ func (n *Named) resolve() *Named {
274273
}
275274
}
276275

277-
n.setState(resolved | complete)
276+
n.setState(resolved | hasMethods)
278277
return n
279278
}
280279

@@ -396,11 +395,11 @@ func (t *Named) NumMethods() int {
396395
func (t *Named) Method(i int) *Func {
397396
t.resolve()
398397

399-
if t.stateHas(complete) {
398+
if t.stateHas(hasMethods) {
400399
return t.methods[i]
401400
}
402401

403-
assert(t.inst != nil) // only instances should have incomplete methods
402+
assert(t.inst != nil) // only instances should have unexpanded methods
404403
orig := t.inst.orig
405404

406405
t.mu.Lock()
@@ -417,9 +416,9 @@ func (t *Named) Method(i int) *Func {
417416
t.inst.expandedMethods++
418417

419418
// Check if we've created all methods at this point. If we have, mark the
420-
// type as fully expanded.
419+
// type as having all of its methods.
421420
if t.inst.expandedMethods == len(orig.methods) {
422-
t.setState(complete)
421+
t.setState(hasMethods)
423422
t.inst.ctxt = nil // no need for a context anymore
424423
}
425424
}
@@ -502,11 +501,11 @@ func (t *Named) SetUnderlying(u Type) {
502501

503502
t.fromRHS = u
504503
t.allowNilRHS = false
505-
t.setState(resolved | complete) // TODO(markfreeman): Why complete?
504+
t.setState(resolved | hasMethods) // TODO(markfreeman): Why hasMethods?
506505

507506
t.underlying = u
508507
t.allowNilUnderlying = false
509-
t.setState(underlying)
508+
t.setState(hasUnder)
510509
}
511510

512511
// AddMethod adds method m unless it is already in the method list.
@@ -562,7 +561,7 @@ func (n *Named) Underlying() Type {
562561
}
563562
}
564563

565-
if !n.stateHas(underlying) {
564+
if !n.stateHas(hasUnder) {
566565
n.resolveUnderlying()
567566
}
568567

@@ -617,7 +616,7 @@ func (n *Named) resolveUnderlying() {
617616
}
618617

619618
// avoid acquiring the lock if we can
620-
if t.stateHas(underlying) {
619+
if t.stateHas(hasUnder) {
621620
u = t.underlying
622621
break
623622
}
@@ -644,11 +643,11 @@ func (n *Named) resolveUnderlying() {
644643
// Careful, t.underlying has lock-free readers. Since we might be racing
645644
// another call to resolveUnderlying, we have to avoid overwriting
646645
// t.underlying. Otherwise, the race detector will be tripped.
647-
if t.stateHas(underlying) {
646+
if t.stateHas(hasUnder) {
648647
continue
649648
}
650649
t.underlying = u
651-
t.setState(underlying)
650+
t.setState(hasUnder)
652651
}
653652
}
654653

src/go/types/named.go

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

0 commit comments

Comments
 (0)