@@ -89,11 +89,11 @@ static Type defaultTypeLiteralKind(CodeCompletionLiteralKind kind,
8989 case CodeCompletionLiteralKind::ArrayLiteral:
9090 if (!Ctx.getArrayDecl ())
9191 return Type ();
92- return Ctx.getArrayDecl ()->getDeclaredType ();
92+ return Ctx.getArrayDecl ()->getDeclaredInterfaceType ();
9393 case CodeCompletionLiteralKind::DictionaryLiteral:
9494 if (!Ctx.getDictionaryDecl ())
9595 return Type ();
96- return Ctx.getDictionaryDecl ()->getDeclaredType ();
96+ return Ctx.getDictionaryDecl ()->getDeclaredInterfaceType ();
9797 case CodeCompletionLiteralKind::NilLiteral:
9898 case CodeCompletionLiteralKind::ColorLiteral:
9999 case CodeCompletionLiteralKind::ImageLiteral:
@@ -603,7 +603,7 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD,
603603 DynamicLookupInfo dynamicLookupInfo) {
604604 switch (dynamicLookupInfo.getKind ()) {
605605 case DynamicLookupInfo::None:
606- return getTypeOfMember (VD, this -> ExprType );
606+ return getTypeOfMember (VD, getMemberBaseType () );
607607 case DynamicLookupInfo::AnyObject:
608608 return getTypeOfMember (VD, Type ());
609609 case DynamicLookupInfo::KeyPathDynamicMember: {
@@ -676,7 +676,14 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD,
676676}
677677
678678Type 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+ }
680687 assert (!T.isNull ());
681688
682689 if (ExprType) {
@@ -763,9 +770,7 @@ Type CompletionLookup::getTypeOfMember(const ValueDecl *VD, Type ExprType) {
763770}
764771
765772Type CompletionLookup::getAssociatedTypeType (const AssociatedTypeDecl *ATD) {
766- Type BaseTy = BaseType;
767- if (!BaseTy)
768- BaseTy = ExprType;
773+ Type BaseTy = getMemberBaseType ();
769774 if (!BaseTy && CurrDeclContext)
770775 BaseTy =
771776 CurrDeclContext->getInnermostTypeContext ()->getDeclaredTypeInContext ();
@@ -1709,13 +1714,16 @@ void CompletionLookup::addNominalTypeRef(const NominalTypeDecl *NTD,
17091714 addLeadingDot (Builder);
17101715 Builder.addBaseName (NTD->getName ().str ());
17111716
1717+ // Substitute the base type for a nested type if needed.
1718+ auto nominalTy = getTypeOfMember (NTD, dynamicLookupInfo);
1719+
17121720 // "Fake" annotation for custom attribute types.
17131721 SmallVector<char , 0 > stash;
17141722 StringRef customAttributeAnnotation = getTypeAnnotationString (NTD, stash);
17151723 if (!customAttributeAnnotation.empty ()) {
17161724 Builder.addTypeAnnotation (customAttributeAnnotation);
17171725 } else {
1718- addTypeAnnotation (Builder, NTD-> getDeclaredType () );
1726+ addTypeAnnotation (Builder, nominalTy );
17191727 }
17201728
17211729 // Override the type relation for NominalTypes. Use the better relation
@@ -1725,8 +1733,7 @@ void CompletionLookup::addNominalTypeRef(const NominalTypeDecl *NTD,
17251733 // func receiveMetatype(_: Int.Type) {}
17261734 //
17271735 // 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});
17301737 Builder.setTypeContext (expectedTypeContext, CurrDeclContext);
17311738}
17321739
@@ -1739,17 +1746,18 @@ void CompletionLookup::addTypeAliasRef(const TypeAliasDecl *TAD,
17391746 Builder.setAssociatedDecl (TAD);
17401747 addLeadingDot (Builder);
17411748 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 ());
17481749
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;
17521759 }
1760+ addTypeAnnotation (Builder, ty);
17531761}
17541762
17551763void CompletionLookup::addGenericTypeParamRef (
@@ -3010,11 +3018,14 @@ void CompletionLookup::getGenericRequirementCompletions(
30103018 /* includeDerivedRequirements*/ false ,
30113019 /* includeProtocolExtensionMembers*/ true );
30123020 // 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
30143023 // 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+ }
30183029 }
30193030
30203031 // Self is also valid in all cases in which it can be used in function
0 commit comments