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