named.go raw
1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
2 // Source: ../../cmd/compile/internal/types2/named.go
3
4 // Copyright 2011 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package types
9
10 import (
11 "go/token"
12 "strings"
13 "sync"
14 "sync/atomic"
15 )
16
17 // Type-checking Named types is subtle, because they may be recursively
18 // defined, and because their full details may be spread across multiple
19 // declarations (via methods). For this reason they are type-checked lazily,
20 // to avoid information being accessed before it is complete.
21 //
22 // Conceptually, it is helpful to think of named types as having two distinct
23 // sets of information:
24 // - "LHS" information, defining their identity: Obj() and TypeArgs()
25 // - "RHS" information, defining their details: TypeParams(), Underlying(),
26 // and methods.
27 //
28 // In this taxonomy, LHS information is available immediately, but RHS
29 // information is lazy. Specifically, a named type N may be constructed in any
30 // of the following ways:
31 // 1. type-checked from the source
32 // 2. loaded eagerly from export data
33 // 3. loaded lazily from export data (when using unified IR)
34 // 4. instantiated from a generic type
35 //
36 // In cases 1, 3, and 4, it is possible that the underlying type or methods of
37 // N may not be immediately available.
38 // - During type-checking, we allocate N before type-checking its underlying
39 // type or methods, so that we may resolve recursive references.
40 // - When loading from export data, we may load its methods and underlying
41 // type lazily using a provided load function.
42 // - After instantiating, we lazily expand the underlying type and methods
43 // (note that instances may be created while still in the process of
44 // type-checking the original type declaration).
45 //
46 // In cases 3 and 4 this lazy construction may also occur concurrently, due to
47 // concurrent use of the type checker API (after type checking or importing has
48 // finished). It is critical that we keep track of state, so that Named types
49 // are constructed exactly once and so that we do not access their details too
50 // soon.
51 //
52 // We achieve this by tracking state with an atomic state variable, and
53 // guarding potentially concurrent calculations with a mutex. At any point in
54 // time this state variable determines which data on N may be accessed. As
55 // state monotonically progresses, any data available at state M may be
56 // accessed without acquiring the mutex at state N, provided N >= M.
57 //
58 // GLOSSARY: Here are a few terms used in this file to describe Named types:
59 // - We say that a Named type is "instantiated" if it has been constructed by
60 // instantiating a generic named type with type arguments.
61 // - We say that a Named type is "declared" if it corresponds to a type
62 // declaration in the source. Instantiated named types correspond to a type
63 // instantiation in the source, not a declaration. But their Origin type is
64 // a declared type.
65 // - We say that a Named type is "resolved" if its RHS information has been
66 // loaded or fully type-checked. For Named types constructed from export
67 // data, this may involve invoking a loader function to extract information
68 // from export data. For instantiated named types this involves reading
69 // information from their origin.
70 // - We say that a Named type is "expanded" if it is an instantiated type and
71 // type parameters in its underlying type and methods have been substituted
72 // with the type arguments from the instantiation. A type may be partially
73 // expanded if some but not all of these details have been substituted.
74 // Similarly, we refer to these individual details (underlying type or
75 // method) as being "expanded".
76 // - When all information is known for a named type, we say it is "complete".
77 //
78 // Some invariants to keep in mind: each declared Named type has a single
79 // corresponding object, and that object's type is the (possibly generic) Named
80 // type. Declared Named types are identical if and only if their pointers are
81 // identical. On the other hand, multiple instantiated Named types may be
82 // identical even though their pointers are not identical. One has to use
83 // Identical to compare them. For instantiated named types, their obj is a
84 // synthetic placeholder that records their position of the corresponding
85 // instantiation in the source (if they were constructed during type checking).
86 //
87 // To prevent infinite expansion of named instances that are created outside of
88 // type-checking, instances share a Context with other instances created during
89 // their expansion. Via the pidgeonhole principle, this guarantees that in the
90 // presence of a cycle of named types, expansion will eventually find an
91 // existing instance in the Context and short-circuit the expansion.
92 //
93 // Once an instance is complete, we can nil out this shared Context to unpin
94 // memory, though this Context may still be held by other incomplete instances
95 // in its "lineage".
96
97 // A Named represents a named (defined) type.
98 //
99 // A declaration such as:
100 //
101 // type S struct { ... }
102 //
103 // creates a defined type whose underlying type is a struct,
104 // and binds this type to the object S, a [TypeName].
105 // Use [Named.Underlying] to access the underlying type.
106 // Use [Named.Obj] to obtain the object S.
107 //
108 // Before type aliases (Go 1.9), the spec called defined types "named types".
109 type Named struct {
110 check *Checker // non-nil during type-checking; nil otherwise
111 obj *TypeName // corresponding declared object for declared types; see above for instantiated types
112
113 // fromRHS holds the type (on RHS of declaration) this *Named type is derived
114 // from (for cycle reporting). Only used by validType, and therefore does not
115 // require synchronization.
116 fromRHS Type
117
118 // information for instantiated types; nil otherwise
119 inst *instance
120
121 mu sync.Mutex // guards all fields below
122 state_ uint32 // the current state of this type; must only be accessed atomically
123 underlying Type // possibly a *Named during setup; never a *Named once set up completely
124 tparams *TypeParamList // type parameters, or nil
125
126 // methods declared for this type (not the method set of this type)
127 // Signatures are type-checked lazily.
128 // For non-instantiated types, this is a fully populated list of methods. For
129 // instantiated types, methods are individually expanded when they are first
130 // accessed.
131 methods []*Func
132
133 // loader may be provided to lazily load type parameters, underlying type, and methods.
134 loader func(*Named) (tparams []*TypeParam, underlying Type, methods []*Func)
135 }
136
137 // instance holds information that is only necessary for instantiated named
138 // types.
139 type instance struct {
140 orig *Named // original, uninstantiated type
141 targs *TypeList // type arguments
142 expandedMethods int // number of expanded methods; expandedMethods <= len(orig.methods)
143 ctxt *Context // local Context; set to nil after full expansion
144 }
145
146 // namedState represents the possible states that a named type may assume.
147 type namedState uint32
148
149 const (
150 unresolved namedState = iota // tparams, underlying type and methods might be unavailable
151 resolved // resolve has run; methods might be incomplete (for instances)
152 complete // all data is known
153 )
154
155 // NewNamed returns a new named type for the given type name, underlying type, and associated methods.
156 // If the given type name obj doesn't have a type yet, its type is set to the returned named type.
157 // The underlying type must not be a *Named.
158 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
159 if asNamed(underlying) != nil {
160 panic("underlying type must not be *Named")
161 }
162 return (*Checker)(nil).newNamed(obj, underlying, methods)
163 }
164
165 // resolve resolves the type parameters, methods, and underlying type of n.
166 // This information may be loaded from a provided loader function, or computed
167 // from an origin type (in the case of instances).
168 //
169 // After resolution, the type parameters, methods, and underlying type of n are
170 // accessible; but if n is an instantiated type, its methods may still be
171 // unexpanded.
172 func (n *Named) resolve() *Named {
173 if n.state() >= resolved { // avoid locking below
174 return n
175 }
176
177 // TODO(rfindley): if n.check is non-nil we can avoid locking here, since
178 // type-checking is not concurrent. Evaluate if this is worth doing.
179 n.mu.Lock()
180 defer n.mu.Unlock()
181
182 if n.state() >= resolved {
183 return n
184 }
185
186 if n.inst != nil {
187 assert(n.underlying == nil) // n is an unresolved instance
188 assert(n.loader == nil) // instances are created by instantiation, in which case n.loader is nil
189
190 orig := n.inst.orig
191 orig.resolve()
192 underlying := n.expandUnderlying()
193
194 n.tparams = orig.tparams
195 n.underlying = underlying
196 n.fromRHS = orig.fromRHS // for cycle detection
197
198 if len(orig.methods) == 0 {
199 n.setState(complete) // nothing further to do
200 n.inst.ctxt = nil
201 } else {
202 n.setState(resolved)
203 }
204 return n
205 }
206
207 // TODO(mdempsky): Since we're passing n to the loader anyway
208 // (necessary because types2 expects the receiver type for methods
209 // on defined interface types to be the Named rather than the
210 // underlying Interface), maybe it should just handle calling
211 // SetTypeParams, SetUnderlying, and AddMethod instead? Those
212 // methods would need to support reentrant calls though. It would
213 // also make the API more future-proof towards further extensions.
214 if n.loader != nil {
215 assert(n.underlying == nil)
216 assert(n.TypeArgs().Len() == 0) // instances are created by instantiation, in which case n.loader is nil
217
218 tparams, underlying, methods := n.loader(n)
219
220 n.tparams = bindTParams(tparams)
221 n.underlying = underlying
222 n.fromRHS = underlying // for cycle detection
223 n.methods = methods
224 n.loader = nil
225 }
226
227 n.setState(complete)
228 return n
229 }
230
231 // state atomically accesses the current state of the receiver.
232 func (n *Named) state() namedState {
233 return namedState(atomic.LoadUint32(&n.state_))
234 }
235
236 // setState atomically stores the given state for n.
237 // Must only be called while holding n.mu.
238 func (n *Named) setState(state namedState) {
239 atomic.StoreUint32(&n.state_, uint32(state))
240 }
241
242 // newNamed is like NewNamed but with a *Checker receiver.
243 func (check *Checker) newNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
244 typ := &Named{check: check, obj: obj, fromRHS: underlying, underlying: underlying, methods: methods}
245 if obj.typ == nil {
246 obj.typ = typ
247 }
248 // Ensure that typ is always sanity-checked.
249 if check != nil {
250 check.needsCleanup(typ)
251 }
252 return typ
253 }
254
255 // newNamedInstance creates a new named instance for the given origin and type
256 // arguments, recording pos as the position of its synthetic object (for error
257 // reporting).
258 //
259 // If set, expanding is the named type instance currently being expanded, that
260 // led to the creation of this instance.
261 func (check *Checker) newNamedInstance(pos token.Pos, orig *Named, targs []Type, expanding *Named) *Named {
262 assert(len(targs) > 0)
263
264 obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
265 inst := &instance{orig: orig, targs: newTypeList(targs)}
266
267 // Only pass the expanding context to the new instance if their packages
268 // match. Since type reference cycles are only possible within a single
269 // package, this is sufficient for the purposes of short-circuiting cycles.
270 // Avoiding passing the context in other cases prevents unnecessary coupling
271 // of types across packages.
272 if expanding != nil && expanding.Obj().pkg == obj.pkg {
273 inst.ctxt = expanding.inst.ctxt
274 }
275 typ := &Named{check: check, obj: obj, inst: inst}
276 obj.typ = typ
277 // Ensure that typ is always sanity-checked.
278 if check != nil {
279 check.needsCleanup(typ)
280 }
281 return typ
282 }
283
284 func (t *Named) cleanup() {
285 assert(t.inst == nil || t.inst.orig.inst == nil)
286 // Ensure that every defined type created in the course of type-checking has
287 // either non-*Named underlying type, or is unexpanded.
288 //
289 // This guarantees that we don't leak any types whose underlying type is
290 // *Named, because any unexpanded instances will lazily compute their
291 // underlying type by substituting in the underlying type of their origin.
292 // The origin must have either been imported or type-checked and expanded
293 // here, and in either case its underlying type will be fully expanded.
294 switch t.underlying.(type) {
295 case nil:
296 if t.TypeArgs().Len() == 0 {
297 panic("nil underlying")
298 }
299 case *Named, *Alias:
300 t.under() // t.under may add entries to check.cleaners
301 }
302 t.check = nil
303 }
304
305 // Obj returns the type name for the declaration defining the named type t. For
306 // instantiated types, this is same as the type name of the origin type.
307 func (t *Named) Obj() *TypeName {
308 if t.inst == nil {
309 return t.obj
310 }
311 return t.inst.orig.obj
312 }
313
314 // Origin returns the generic type from which the named type t is
315 // instantiated. If t is not an instantiated type, the result is t.
316 func (t *Named) Origin() *Named {
317 if t.inst == nil {
318 return t
319 }
320 return t.inst.orig
321 }
322
323 // TypeParams returns the type parameters of the named type t, or nil.
324 // The result is non-nil for an (originally) generic type even if it is instantiated.
325 func (t *Named) TypeParams() *TypeParamList { return t.resolve().tparams }
326
327 // SetTypeParams sets the type parameters of the named type t.
328 // t must not have type arguments.
329 func (t *Named) SetTypeParams(tparams []*TypeParam) {
330 assert(t.inst == nil)
331 t.resolve().tparams = bindTParams(tparams)
332 }
333
334 // TypeArgs returns the type arguments used to instantiate the named type t.
335 func (t *Named) TypeArgs() *TypeList {
336 if t.inst == nil {
337 return nil
338 }
339 return t.inst.targs
340 }
341
342 // NumMethods returns the number of explicit methods defined for t.
343 func (t *Named) NumMethods() int {
344 return len(t.Origin().resolve().methods)
345 }
346
347 // Method returns the i'th method of named type t for 0 <= i < t.NumMethods().
348 //
349 // For an ordinary or instantiated type t, the receiver base type of this
350 // method is the named type t. For an uninstantiated generic type t, each
351 // method receiver is instantiated with its receiver type parameters.
352 //
353 // Methods are numbered deterministically: given the same list of source files
354 // presented to the type checker, or the same sequence of NewMethod and AddMethod
355 // calls, the mapping from method index to corresponding method remains the same.
356 // But the specific ordering is not specified and must not be relied on as it may
357 // change in the future.
358 func (t *Named) Method(i int) *Func {
359 t.resolve()
360
361 if t.state() >= complete {
362 return t.methods[i]
363 }
364
365 assert(t.inst != nil) // only instances should have incomplete methods
366 orig := t.inst.orig
367
368 t.mu.Lock()
369 defer t.mu.Unlock()
370
371 if len(t.methods) != len(orig.methods) {
372 assert(len(t.methods) == 0)
373 t.methods = make([]*Func, len(orig.methods))
374 }
375
376 if t.methods[i] == nil {
377 assert(t.inst.ctxt != nil) // we should still have a context remaining from the resolution phase
378 t.methods[i] = t.expandMethod(i)
379 t.inst.expandedMethods++
380
381 // Check if we've created all methods at this point. If we have, mark the
382 // type as fully expanded.
383 if t.inst.expandedMethods == len(orig.methods) {
384 t.setState(complete)
385 t.inst.ctxt = nil // no need for a context anymore
386 }
387 }
388
389 return t.methods[i]
390 }
391
392 // expandMethod substitutes type arguments in the i'th method for an
393 // instantiated receiver.
394 func (t *Named) expandMethod(i int) *Func {
395 // t.orig.methods is not lazy. origm is the method instantiated with its
396 // receiver type parameters (the "origin" method).
397 origm := t.inst.orig.Method(i)
398 assert(origm != nil)
399
400 check := t.check
401 // Ensure that the original method is type-checked.
402 if check != nil {
403 check.objDecl(origm, nil)
404 }
405
406 origSig := origm.typ.(*Signature)
407 rbase, _ := deref(origSig.Recv().Type())
408
409 // If rbase is t, then origm is already the instantiated method we're looking
410 // for. In this case, we return origm to preserve the invariant that
411 // traversing Method->Receiver Type->Method should get back to the same
412 // method.
413 //
414 // This occurs if t is instantiated with the receiver type parameters, as in
415 // the use of m in func (r T[_]) m() { r.m() }.
416 if rbase == t {
417 return origm
418 }
419
420 sig := origSig
421 // We can only substitute if we have a correspondence between type arguments
422 // and type parameters. This check is necessary in the presence of invalid
423 // code.
424 if origSig.RecvTypeParams().Len() == t.inst.targs.Len() {
425 smap := makeSubstMap(origSig.RecvTypeParams().list(), t.inst.targs.list())
426 var ctxt *Context
427 if check != nil {
428 ctxt = check.context()
429 }
430 sig = check.subst(origm.pos, origSig, smap, t, ctxt).(*Signature)
431 }
432
433 if sig == origSig {
434 // No substitution occurred, but we still need to create a new signature to
435 // hold the instantiated receiver.
436 copy := *origSig
437 sig = ©
438 }
439
440 var rtyp Type
441 if origm.hasPtrRecv() {
442 rtyp = NewPointer(t)
443 } else {
444 rtyp = t
445 }
446
447 sig.recv = cloneVar(origSig.recv, rtyp)
448 return cloneFunc(origm, sig)
449 }
450
451 // SetUnderlying sets the underlying type and marks t as complete.
452 // t must not have type arguments.
453 func (t *Named) SetUnderlying(underlying Type) {
454 assert(t.inst == nil)
455 if underlying == nil {
456 panic("underlying type must not be nil")
457 }
458 if asNamed(underlying) != nil {
459 panic("underlying type must not be *Named")
460 }
461 t.resolve().underlying = underlying
462 if t.fromRHS == nil {
463 t.fromRHS = underlying // for cycle detection
464 }
465 }
466
467 // AddMethod adds method m unless it is already in the method list.
468 // The method must be in the same package as t, and t must not have
469 // type arguments.
470 func (t *Named) AddMethod(m *Func) {
471 assert(samePkg(t.obj.pkg, m.pkg))
472 assert(t.inst == nil)
473 t.resolve()
474 if t.methodIndex(m.name, false) < 0 {
475 t.methods = append(t.methods, m)
476 }
477 }
478
479 // methodIndex returns the index of the method with the given name.
480 // If foldCase is set, capitalization in the name is ignored.
481 // The result is negative if no such method exists.
482 func (t *Named) methodIndex(name string, foldCase bool) int {
483 if name == "_" {
484 return -1
485 }
486 if foldCase {
487 for i, m := range t.methods {
488 if strings.EqualFold(m.name, name) {
489 return i
490 }
491 }
492 } else {
493 for i, m := range t.methods {
494 if m.name == name {
495 return i
496 }
497 }
498 }
499 return -1
500 }
501
502 // Underlying returns the [underlying type] of the named type t, resolving all
503 // forwarding declarations. Underlying types are never Named, TypeParam, or
504 // Alias types.
505 //
506 // [underlying type]: https://go.dev/ref/spec#Underlying_types.
507 func (t *Named) Underlying() Type {
508 // TODO(gri) Investigate if Unalias can be moved to where underlying is set.
509 return Unalias(t.resolve().underlying)
510 }
511
512 func (t *Named) String() string { return TypeString(t, nil) }
513
514 // ----------------------------------------------------------------------------
515 // Implementation
516 //
517 // TODO(rfindley): reorganize the loading and expansion methods under this
518 // heading.
519
520 // under returns the expanded underlying type of n0; possibly by following
521 // forward chains of named types. If an underlying type is found, resolve
522 // the chain by setting the underlying type for each defined type in the
523 // chain before returning it. If no underlying type is found or a cycle
524 // is detected, the result is Typ[Invalid]. If a cycle is detected and
525 // n0.check != nil, the cycle is reported.
526 //
527 // This is necessary because the underlying type of named may be itself a
528 // named type that is incomplete:
529 //
530 // type (
531 // A B
532 // B *C
533 // C A
534 // )
535 //
536 // The type of C is the (named) type of A which is incomplete,
537 // and which has as its underlying type the named type B.
538 func (n0 *Named) under() Type {
539 u := n0.Underlying()
540
541 // If the underlying type of a defined type is not a defined
542 // (incl. instance) type, then that is the desired underlying
543 // type.
544 var n1 *Named
545 switch u1 := u.(type) {
546 case nil:
547 // After expansion via Underlying(), we should never encounter a nil
548 // underlying.
549 panic("nil underlying")
550 default:
551 // common case
552 return u
553 case *Named:
554 // handled below
555 n1 = u1
556 }
557
558 if n0.check == nil {
559 panic("Named.check == nil but type is incomplete")
560 }
561
562 // Invariant: after this point n0 as well as any named types in its
563 // underlying chain should be set up when this function exits.
564 check := n0.check
565 n := n0
566
567 seen := make(map[*Named]int) // types that need their underlying type resolved
568 var path []Object // objects encountered, for cycle reporting
569
570 loop:
571 for {
572 seen[n] = len(seen)
573 path = append(path, n.obj)
574 n = n1
575 if i, ok := seen[n]; ok {
576 // cycle
577 check.cycleError(path[i:], firstInSrc(path[i:]))
578 u = Typ[Invalid]
579 break
580 }
581 u = n.Underlying()
582 switch u1 := u.(type) {
583 case nil:
584 u = Typ[Invalid]
585 break loop
586 default:
587 break loop
588 case *Named:
589 // Continue collecting *Named types in the chain.
590 n1 = u1
591 }
592 }
593
594 for n := range seen {
595 // We should never have to update the underlying type of an imported type;
596 // those underlying types should have been resolved during the import.
597 // Also, doing so would lead to a race condition (was go.dev/issue/31749).
598 // Do this check always, not just in debug mode (it's cheap).
599 if n.obj.pkg != check.pkg {
600 panic("imported type with unresolved underlying type")
601 }
602 n.underlying = u
603 }
604
605 return u
606 }
607
608 func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
609 n.resolve()
610 if samePkg(n.obj.pkg, pkg) || isExported(name) || foldCase {
611 // If n is an instance, we may not have yet instantiated all of its methods.
612 // Look up the method index in orig, and only instantiate method at the
613 // matching index (if any).
614 if i := n.Origin().methodIndex(name, foldCase); i >= 0 {
615 // For instances, m.Method(i) will be different from the orig method.
616 return i, n.Method(i)
617 }
618 }
619 return -1, nil
620 }
621
622 // context returns the type-checker context.
623 func (check *Checker) context() *Context {
624 if check.ctxt == nil {
625 check.ctxt = NewContext()
626 }
627 return check.ctxt
628 }
629
630 // expandUnderlying substitutes type arguments in the underlying type n.orig,
631 // returning the result. Returns Typ[Invalid] if there was an error.
632 func (n *Named) expandUnderlying() Type {
633 check := n.check
634 if check != nil && check.conf._Trace {
635 check.trace(n.obj.pos, "-- Named.expandUnderlying %s", n)
636 check.indent++
637 defer func() {
638 check.indent--
639 check.trace(n.obj.pos, "=> %s (tparams = %s, under = %s)", n, n.tparams.list(), n.underlying)
640 }()
641 }
642
643 assert(n.inst.orig.underlying != nil)
644 if n.inst.ctxt == nil {
645 n.inst.ctxt = NewContext()
646 }
647
648 orig := n.inst.orig
649 targs := n.inst.targs
650
651 if asNamed(orig.underlying) != nil {
652 // We should only get a Named underlying type here during type checking
653 // (for example, in recursive type declarations).
654 assert(check != nil)
655 }
656
657 if orig.tparams.Len() != targs.Len() {
658 // Mismatching arg and tparam length may be checked elsewhere.
659 return Typ[Invalid]
660 }
661
662 // Ensure that an instance is recorded before substituting, so that we
663 // resolve n for any recursive references.
664 h := n.inst.ctxt.instanceHash(orig, targs.list())
665 n2 := n.inst.ctxt.update(h, orig, n.TypeArgs().list(), n)
666 assert(n == n2)
667
668 smap := makeSubstMap(orig.tparams.list(), targs.list())
669 var ctxt *Context
670 if check != nil {
671 ctxt = check.context()
672 }
673 underlying := n.check.subst(n.obj.pos, orig.underlying, smap, n, ctxt)
674 // If the underlying type of n is an interface, we need to set the receiver of
675 // its methods accurately -- we set the receiver of interface methods on
676 // the RHS of a type declaration to the defined type.
677 if iface, _ := underlying.(*Interface); iface != nil {
678 if methods, copied := replaceRecvType(iface.methods, orig, n); copied {
679 // If the underlying type doesn't actually use type parameters, it's
680 // possible that it wasn't substituted. In this case we need to create
681 // a new *Interface before modifying receivers.
682 if iface == orig.underlying {
683 old := iface
684 iface = check.newInterface()
685 iface.embeddeds = old.embeddeds
686 assert(old.complete) // otherwise we are copying incomplete data
687 iface.complete = old.complete
688 iface.implicit = old.implicit // should be false but be conservative
689 underlying = iface
690 }
691 iface.methods = methods
692 iface.tset = nil // recompute type set with new methods
693
694 // If check != nil, check.newInterface will have saved the interface for later completion.
695 if check == nil { // golang/go#61561: all newly created interfaces must be fully evaluated
696 iface.typeSet()
697 }
698 }
699 }
700
701 return underlying
702 }
703
704 // safeUnderlying returns the underlying type of typ without expanding
705 // instances, to avoid infinite recursion.
706 //
707 // TODO(rfindley): eliminate this function or give it a better name.
708 func safeUnderlying(typ Type) Type {
709 if t := asNamed(typ); t != nil {
710 return t.underlying
711 }
712 return typ.Underlying()
713 }
714