@@ -1037,21 +1037,23 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
10371037
10381038 bool reportRelatedRef (ValueDecl *D, SourceLoc Loc, bool isImplicit, SymbolRoleSet Relations, Decl *Related);
10391039
1040- // / Report a related type relation for a given TypeRepr.
1040+ // / Report a related type relation for a given TypeLoc. Don't call this
1041+ // / directly, use \c reportRelatedTypeRef instead.
10411042 // /
10421043 // / NOTE: If the dependent type is a typealias, report the underlying types as
10431044 // / well.
10441045 // /
1045- // / \param TR The type being referenced.
1046+ // / \param TL The type being referenced.
10461047 // / \param Relations The relationship between the referenced type and the
10471048 // / passed Decl.
10481049 // / \param Related The Decl that is referencing the type.
10491050 // / \param Implicit Whether the reference is implicit, such as for a
10501051 // / typealias' underlying type.
10511052 // / \param ParentLoc The parent location of the reference that should be used
10521053 // / for implicit references.
1053- bool reportRelatedTypeRepr (const TypeRepr *TR, SymbolRoleSet Relations,
1054- Decl *Related, bool Implicit, SourceLoc ParentLoc);
1054+ bool reportRelatedTypeRefImpl (const TypeLoc &TL, SymbolRoleSet Relations,
1055+ Decl *Related, bool Implicit,
1056+ SourceLoc ParentLoc);
10551057
10561058 // / Report a related type relation for a Type at a given location.
10571059 // /
@@ -1531,66 +1533,85 @@ bool IndexSwiftASTWalker::reportInheritedTypeRefs(InheritedTypes Inherited,
15311533 return true ;
15321534}
15331535
1534- bool IndexSwiftASTWalker::reportRelatedTypeRepr (const TypeRepr *TR,
1535- SymbolRoleSet Relations,
1536- Decl *Related, bool Implicit,
1537- SourceLoc ParentLoc) {
1538- // Look through parens/specifiers/attributes.
1539- while (true ) {
1540- if (TR->isParenType ()) {
1541- TR = TR->getWithoutParens ();
1542- continue ;
1543- }
1544- if (auto *SPR = dyn_cast<SpecifierTypeRepr>(TR)) {
1545- TR = SPR->getBase ();
1546- continue ;
1547- }
1548- if (auto *ATR = dyn_cast<AttributedTypeRepr>(TR)) {
1549- TR = ATR->getTypeRepr ();
1550- continue ;
1536+ bool IndexSwiftASTWalker::reportRelatedTypeRefImpl (const TypeLoc &TL,
1537+ SymbolRoleSet Relations,
1538+ Decl *Related, bool Implicit,
1539+ SourceLoc ParentLoc) {
1540+ auto *TR = TL.getTypeRepr ();
1541+ if (TR) {
1542+ // Look through parens/specifiers/attributes.
1543+ while (true ) {
1544+ if (TR->isParenType ()) {
1545+ TR = TR->getWithoutParens ();
1546+ continue ;
1547+ }
1548+ if (auto *SPR = dyn_cast<SpecifierTypeRepr>(TR)) {
1549+ TR = SPR->getBase ();
1550+ continue ;
1551+ }
1552+ if (auto *ATR = dyn_cast<AttributedTypeRepr>(TR)) {
1553+ TR = ATR->getTypeRepr ();
1554+ continue ;
1555+ }
1556+ break ;
15511557 }
1552- break ;
1553- }
1554- // NOTE: We don't yet handle InverseTypeRepr since we don't have an inverse
1555- // relation for inheritance.
1558+ // NOTE: We don't yet handle InverseTypeRepr since we don't have an inverse
1559+ // relation for inheritance.
15561560
1557- if (auto *composite = dyn_cast<CompositionTypeRepr>(TR)) {
1558- for (auto *Type : composite->getTypes ()) {
1559- if (!reportRelatedTypeRepr (Type, Relations, Related, Implicit,
1560- ParentLoc)) {
1561- return false ;
1561+ if (auto *composite = dyn_cast<CompositionTypeRepr>(TR)) {
1562+ for (auto *Type : composite->getTypes ()) {
1563+ // Note this doesn't handle type sugar cases where the decl is only
1564+ // available on the semantic type. This isn't currently something that
1565+ // happens for type compositions though.
1566+ if (!reportRelatedTypeRefImpl (Type, Relations, Related, Implicit,
1567+ ParentLoc)) {
1568+ return false ;
1569+ }
15621570 }
1571+ return true ;
15631572 }
15641573 }
1565- auto *declRefTR = dyn_cast<DeclRefTypeRepr>(TR);
1566- if (!declRefTR)
1567- return true ;
1574+ SourceLoc Loc = [&]() {
1575+ if (ParentLoc)
1576+ return ParentLoc;
1577+ if (TR)
1578+ return TR->getLoc ();
1579+ return SourceLoc ();
1580+ }();
1581+
1582+ auto *declRefTR = dyn_cast_or_null<DeclRefTypeRepr>(TR);
1583+ if (!declRefTR) {
1584+ if (!TL.getType ())
1585+ return true ;
1586+
1587+ // If we don't have a TypeRepr, we're e.g indexing a Swift module, so want
1588+ // to look at the Type instead. If we have a TypeRepr but have no explicit
1589+ // decl reference, fall back to indexing an implicit reference to handle
1590+ // type sugar e.g Array for `[Int]`.
1591+ return reportRelatedType (TL.getType (), Relations, Related,
1592+ Implicit || TR, Loc);
1593+ }
15681594
15691595 auto *VD = declRefTR->getBoundDecl ();
15701596 if (!VD)
15711597 return true ;
15721598
1573- SourceLoc IdLoc = ParentLoc.isValid () ? ParentLoc : declRefTR->getLoc ();
15741599 if (auto *TAD = dyn_cast<TypeAliasDecl>(VD)) {
15751600 IndexSymbol Info;
15761601 if (Implicit)
15771602 Info.roles |= (unsigned )SymbolRole::Implicit;
1578- if (!reportRef (TAD, IdLoc , Info, std::nullopt ))
1603+ if (!reportRef (TAD, Loc , Info, std::nullopt ))
15791604 return false ;
15801605
15811606 // Recurse into the underlying type and report any found references as
15821607 // implicit references at the location of the typealias reference.
1583- if (auto *UTR = TAD->getUnderlyingTypeRepr ()) {
1584- return reportRelatedTypeRepr (UTR, Relations, Related,
1585- /* Implicit*/ true , /* ParentLoc*/ IdLoc);
1586- }
1587- // If we don't have a TypeRepr available, this is a typealias in another
1588- // module, consult the computed underlying type.
1589- return reportRelatedType (TAD->getUnderlyingType (), Relations, Related,
1590- /* Implicit*/ true , /* ParentLoc*/ IdLoc);
1608+ TypeLoc UnderlyingTL (TAD->getUnderlyingTypeRepr (),
1609+ TAD->getUnderlyingType ());
1610+ return reportRelatedTypeRefImpl (UnderlyingTL, Relations, Related,
1611+ /* Implicit*/ true , /* ParentLoc*/ Loc);
15911612 }
15921613 if (auto *NTD = dyn_cast<NominalTypeDecl>(VD)) {
1593- if (!reportRelatedRef (NTD, IdLoc , Implicit, Relations, Related))
1614+ if (!reportRelatedRef (NTD, Loc , Implicit, Relations, Related))
15941615 return false ;
15951616 }
15961617 return true ;
@@ -1618,19 +1639,8 @@ bool IndexSwiftASTWalker::reportRelatedType(Type Ty, SymbolRoleSet Relations,
16181639bool IndexSwiftASTWalker::reportRelatedTypeRef (const TypeLoc &TL,
16191640 SymbolRoleSet Relations,
16201641 Decl *Related) {
1621- // If we have a TypeRepr, prefer that since it lets us match up source
1622- // locations with the code the user wrote.
1623- if (auto *TR = TL.getTypeRepr ()) {
1624- return reportRelatedTypeRepr (TR, Relations, Related, /* Implicit*/ false ,
1625- /* ParentLoc*/ SourceLoc ());
1626- }
1627- // Otherwise fall back to reporting the Type, this is necessary when indexing
1628- // swiftmodules.
1629- if (auto Ty = TL.getType ()) {
1630- return reportRelatedType (Ty, Relations, Related,
1631- /* Implicit*/ false , SourceLoc ());
1632- }
1633- return true ;
1642+ return reportRelatedTypeRefImpl (TL, Relations, Related, /* Implicit*/ false ,
1643+ /* ParentLoc*/ SourceLoc ());
16341644}
16351645
16361646bool IndexSwiftASTWalker::reportPseudoAccessor (AbstractStorageDecl *D,
0 commit comments