@@ -1755,6 +1755,43 @@ void CompletionLookup::addNominalTypeRef(const NominalTypeDecl *NTD,
1755
1755
Builder.setTypeContext (expectedTypeContext, CurrDeclContext);
1756
1756
}
1757
1757
1758
+ Type CompletionLookup::getTypeAliasType (const TypeAliasDecl *TAD,
1759
+ DynamicLookupInfo dynamicLookupInfo) {
1760
+ // Substitute the base type for a nested typealias if needed.
1761
+ auto ty = getTypeOfMember (TAD, dynamicLookupInfo);
1762
+ auto *typeAliasTy = dyn_cast<TypeAliasType>(ty.getPointer ());
1763
+ if (!typeAliasTy)
1764
+ return ty;
1765
+
1766
+ // If the underlying type has an error, prefer to print the full typealias,
1767
+ // otherwise get the underlying type.
1768
+ Type underlyingTy = typeAliasTy->getSinglyDesugaredType ();
1769
+ if (underlyingTy->hasError ())
1770
+ return ty;
1771
+
1772
+ // The underlying type might be unbound for e.g:
1773
+ //
1774
+ // struct S<T> {}
1775
+ // typealias X = S
1776
+ //
1777
+ // Introduce type parameters such that we print the underlying type as
1778
+ // 'S<T>'. We only expect unbound generics at the top-level of a type-alias,
1779
+ // they are rejected by type resolution in any other position.
1780
+ //
1781
+ // FIXME: This is a hack – using the declared interface type isn't correct
1782
+ // since the generic parameters ought to be introduced at a higher depth,
1783
+ // i.e we should be treating it as `typealias X<T> = S<T>`. Ideally this would
1784
+ // be fixed by desugaring the unbound typealias during type resolution. For
1785
+ // now this is fine though since we only use the resulting type for printing
1786
+ // the type annotation; the type relation logic currently skips type
1787
+ // parameters.
1788
+ if (auto *UGT = underlyingTy->getAs <UnboundGenericType>())
1789
+ underlyingTy = UGT->getDecl ()->getDeclaredInterfaceType ();
1790
+
1791
+ ASSERT (!underlyingTy->hasUnboundGenericType ());
1792
+ return underlyingTy;
1793
+ }
1794
+
1758
1795
void CompletionLookup::addTypeAliasRef (const TypeAliasDecl *TAD,
1759
1796
DeclVisibilityKind Reason,
1760
1797
DynamicLookupInfo dynamicLookupInfo) {
@@ -1764,18 +1801,7 @@ void CompletionLookup::addTypeAliasRef(const TypeAliasDecl *TAD,
1764
1801
Builder.setAssociatedDecl (TAD);
1765
1802
addLeadingDot (Builder);
1766
1803
addValueBaseName (Builder, TAD->getBaseName ());
1767
-
1768
- // Substitute the base type for a nested typealias if needed.
1769
- auto ty = getTypeOfMember (TAD, dynamicLookupInfo);
1770
-
1771
- // If the underlying type has an error, prefer to print the full typealias,
1772
- // otherwise get the underlying type.
1773
- if (auto *TA = dyn_cast<TypeAliasType>(ty.getPointer ())) {
1774
- auto underlyingTy = TA->getSinglyDesugaredType ();
1775
- if (!underlyingTy->hasError ())
1776
- ty = underlyingTy;
1777
- }
1778
- addTypeAnnotation (Builder, ty);
1804
+ addTypeAnnotation (Builder, getTypeAliasType (TAD, dynamicLookupInfo));
1779
1805
}
1780
1806
1781
1807
void CompletionLookup::addGenericTypeParamRef (
0 commit comments