@@ -89,11 +89,11 @@ static Type defaultTypeLiteralKind(CodeCompletionLiteralKind kind,
89
89
case CodeCompletionLiteralKind::ArrayLiteral:
90
90
if (!Ctx.getArrayDecl ())
91
91
return Type ();
92
- return Ctx.getArrayDecl ()->getDeclaredType ();
92
+ return Ctx.getArrayDecl ()->getDeclaredInterfaceType ();
93
93
case CodeCompletionLiteralKind::DictionaryLiteral:
94
94
if (!Ctx.getDictionaryDecl ())
95
95
return Type ();
96
- return Ctx.getDictionaryDecl ()->getDeclaredType ();
96
+ return Ctx.getDictionaryDecl ()->getDeclaredInterfaceType ();
97
97
case CodeCompletionLiteralKind::NilLiteral:
98
98
case CodeCompletionLiteralKind::ColorLiteral:
99
99
case CodeCompletionLiteralKind::ImageLiteral:
@@ -603,7 +603,7 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD,
603
603
DynamicLookupInfo dynamicLookupInfo) {
604
604
switch (dynamicLookupInfo.getKind ()) {
605
605
case DynamicLookupInfo::None:
606
- return getTypeOfMember (VD, this -> ExprType );
606
+ return getTypeOfMember (VD, getMemberBaseType () );
607
607
case DynamicLookupInfo::AnyObject:
608
608
return getTypeOfMember (VD, Type ());
609
609
case DynamicLookupInfo::KeyPathDynamicMember: {
@@ -676,7 +676,14 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD,
676
676
}
677
677
678
678
Type CompletionLookup::getTypeOfMember (const ValueDecl *VD, Type ExprType) {
679
- Type T = VD->getInterfaceType ();
679
+ Type T;
680
+ if (auto *TD = dyn_cast<TypeDecl>(VD)) {
681
+ // For a type decl we're interested in the declared interface type, i.e
682
+ // we don't want a metatype.
683
+ T = TD->getDeclaredInterfaceType ();
684
+ } else {
685
+ T = VD->getInterfaceType ();
686
+ }
680
687
assert (!T.isNull ());
681
688
682
689
if (ExprType) {
@@ -763,9 +770,7 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD, Type ExprType) {
763
770
}
764
771
765
772
Type CompletionLookup::getAssociatedTypeType (const AssociatedTypeDecl *ATD) {
766
- Type BaseTy = BaseType;
767
- if (!BaseTy)
768
- BaseTy = ExprType;
773
+ Type BaseTy = getMemberBaseType ();
769
774
if (!BaseTy && CurrDeclContext)
770
775
BaseTy =
771
776
CurrDeclContext->getInnermostTypeContext ()->getDeclaredTypeInContext ();
@@ -1709,13 +1714,16 @@ void CompletionLookup::addNominalTypeRef(const NominalTypeDecl *NTD,
1709
1714
addLeadingDot (Builder);
1710
1715
Builder.addBaseName (NTD->getName ().str ());
1711
1716
1717
+ // Substitute the base type for a nested type if needed.
1718
+ auto nominalTy = getTypeOfMember (NTD, dynamicLookupInfo);
1719
+
1712
1720
// "Fake" annotation for custom attribute types.
1713
1721
SmallVector<char , 0 > stash;
1714
1722
StringRef customAttributeAnnotation = getTypeAnnotationString (NTD, stash);
1715
1723
if (!customAttributeAnnotation.empty ()) {
1716
1724
Builder.addTypeAnnotation (customAttributeAnnotation);
1717
1725
} else {
1718
- addTypeAnnotation (Builder, NTD-> getDeclaredType () );
1726
+ addTypeAnnotation (Builder, nominalTy );
1719
1727
}
1720
1728
1721
1729
// Override the type relation for NominalTypes. Use the better relation
@@ -1725,8 +1733,7 @@ void CompletionLookup::addNominalTypeRef(const NominalTypeDecl *NTD,
1725
1733
// func receiveMetatype(_: Int.Type) {}
1726
1734
//
1727
1735
// We want to suggest 'Int' as 'Identical' for both arguments.
1728
- Builder.setResultTypes (
1729
- {NTD->getInterfaceType (), NTD->getDeclaredInterfaceType ()});
1736
+ Builder.setResultTypes ({MetatypeType::get (nominalTy), nominalTy});
1730
1737
Builder.setTypeContext (expectedTypeContext, CurrDeclContext);
1731
1738
}
1732
1739
@@ -1739,17 +1746,18 @@ void CompletionLookup::addTypeAliasRef(const TypeAliasDecl *TAD,
1739
1746
Builder.setAssociatedDecl (TAD);
1740
1747
addLeadingDot (Builder);
1741
1748
Builder.addBaseName (TAD->getName ().str ());
1742
- if (auto underlyingType = TAD->getUnderlyingType ()) {
1743
- if (underlyingType->hasError ()) {
1744
- addTypeAnnotation (Builder,
1745
- TAD->isGeneric ()
1746
- ? TAD->getUnboundGenericType ()
1747
- : TAD->getDeclaredInterfaceType ());
1748
1749
1749
- } else {
1750
- addTypeAnnotation (Builder, underlyingType);
1751
- }
1750
+ // Substitute the base type for a nested typealias if needed.
1751
+ auto ty = getTypeOfMember (TAD, dynamicLookupInfo);
1752
+
1753
+ // If the underlying type has an error, prefer to print the full typealias,
1754
+ // otherwise get the underlying type.
1755
+ if (auto *TA = dyn_cast<TypeAliasType>(ty.getPointer ())) {
1756
+ auto underlyingTy = TA->getSinglyDesugaredType ();
1757
+ if (!underlyingTy->hasError ())
1758
+ ty = underlyingTy;
1752
1759
}
1760
+ addTypeAnnotation (Builder, ty);
1753
1761
}
1754
1762
1755
1763
void CompletionLookup::addGenericTypeParamRef (
@@ -3010,11 +3018,14 @@ void CompletionLookup::getGenericRequirementCompletions(
3010
3018
/* includeDerivedRequirements*/ false ,
3011
3019
/* includeProtocolExtensionMembers*/ true );
3012
3020
// We not only allow referencing nested types/typealiases directly, but also
3013
- // qualified by the current type. Thus also suggest current self type so the
3021
+ // qualified by the current type, as long as it's a top-level type (nested
3022
+ // types need to be qualified). Thus also suggest current self type so the
3014
3023
// user can do a memberwise lookup on it.
3015
- if (auto SelfType = typeContext->getSelfNominalTypeDecl ()) {
3016
- addNominalTypeRef (SelfType, DeclVisibilityKind::LocalDecl,
3017
- DynamicLookupInfo ());
3024
+ if (auto *NTD = typeContext->getSelfNominalTypeDecl ()) {
3025
+ if (!NTD->getDeclContext ()->isTypeContext ()) {
3026
+ addNominalTypeRef (NTD, DeclVisibilityKind::LocalDecl,
3027
+ DynamicLookupInfo ());
3028
+ }
3018
3029
}
3019
3030
3020
3031
// Self is also valid in all cases in which it can be used in function
0 commit comments