@@ -1872,6 +1872,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
18721872 expectedTypeContext.possibleTypes .push_back (T);
18731873 }
18741874
1875+ void setIdealExpectedType (Type Ty) {
1876+ expectedTypeContext.idealType = Ty;
1877+ }
1878+
18751879 CodeCompletionContext::TypeContextKind typeContextKind () const {
18761880 if (expectedTypeContext.empty ()) {
18771881 return CodeCompletionContext::TypeContextKind::None;
@@ -2011,12 +2015,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20112015 if (ForcedSemanticContext)
20122016 return *ForcedSemanticContext;
20132017
2014- if (IsUnresolvedMember) {
2015- if (isa<EnumElementDecl>(D)) {
2016- return SemanticContextKind::ExpressionSpecific;
2017- }
2018- }
2019-
20202018 switch (Reason) {
20212019 case DeclVisibilityKind::LocalVariable:
20222020 case DeclVisibilityKind::FunctionParameter:
@@ -2080,6 +2078,23 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20802078 llvm_unreachable (" unhandled kind" );
20812079 }
20822080
2081+ bool isUnresolvedMemberIdealType (Type Ty) {
2082+ assert (Ty);
2083+ if (!IsUnresolvedMember)
2084+ return false ;
2085+ Type idealTy = expectedTypeContext.idealType ;
2086+ if (!idealTy)
2087+ return false ;
2088+ // / Consider optional object type is the ideal.
2089+ // / For exmaple:
2090+ // / enum MyEnum { case foo, bar }
2091+ // / func foo(_: MyEnum?)
2092+ // / fooo(.<HERE>)
2093+ // / Prefer '.foo' and '.bar' over '.some' and '.none'.
2094+ idealTy = idealTy->lookThroughAllOptionalTypes ();
2095+ return idealTy->isEqual (Ty);
2096+ }
2097+
20832098 void addValueBaseName (CodeCompletionResultBuilder &Builder,
20842099 DeclBaseName Name) {
20852100 auto NameStr = Name.userFacingName ();
@@ -2406,6 +2421,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
24062421 DynamicOrOptional);
24072422 else
24082423 addTypeAnnotation (Builder, VarType);
2424+
2425+ if (isUnresolvedMemberIdealType (VarType))
2426+ Builder.setSemanticContext (SemanticContextKind::ExpressionSpecific);
24092427 }
24102428
24112429 static bool hasInterestingDefaultValues (const AbstractFunctionDecl *func) {
@@ -2862,6 +2880,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
28622880 }
28632881 }
28642882 Builder.addTypeAnnotation (TypeStr);
2883+
2884+ if (isUnresolvedMemberIdealType (ResultType))
2885+ Builder.setSemanticContext (SemanticContextKind::ExpressionSpecific);
28652886 };
28662887
28672888 if (!AFT || IsImplicitlyCurriedInstanceMethod) {
@@ -3148,6 +3169,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
31483169 }
31493170
31503171 addTypeAnnotation (Builder, EnumType);
3172+
3173+ if (isUnresolvedMemberIdealType (EnumType))
3174+ Builder.setSemanticContext (SemanticContextKind::ExpressionSpecific);
31513175 }
31523176
31533177 void addKeyword (StringRef Name, Type TypeAnnotation = Type(),
@@ -4117,7 +4141,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
41174141 auto &SM = CurrDeclContext->getASTContext ().SourceMgr ;
41184142 if (DotLoc.isValid ())
41194143 bytesToErase = SM.getByteDistance (DotLoc, SM.getCodeCompletionLoc ());
4120- addKeyword (" nil" , T, SemanticContextKind::ExpressionSpecific ,
4144+ addKeyword (" nil" , T, SemanticContextKind::None ,
41214145 CodeCompletionKeywordKind::kw_nil, bytesToErase);
41224146 }
41234147 getUnresolvedMemberCompletions (T);
@@ -5755,6 +5779,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
57555779 ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
57565780 Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
57575781 ContextInfo.isSingleExpressionBody ());
5782+ Lookup.setIdealExpectedType (CodeCompleteTokenExpr->getType ());
57585783 Lookup.getUnresolvedMemberCompletions (ContextInfo.getPossibleTypes ());
57595784 DoPostfixExprBeginning ();
57605785 break ;
@@ -5810,6 +5835,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
58105835 ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
58115836 Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
58125837 ContextInfo.isSingleExpressionBody ());
5838+ Lookup.setIdealExpectedType (CodeCompleteTokenExpr->getType ());
58135839 Lookup.getUnresolvedMemberCompletions (ContextInfo.getPossibleTypes ());
58145840 break ;
58155841 }
0 commit comments