@@ -1872,6 +1872,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1872
1872
expectedTypeContext.possibleTypes .push_back (T);
1873
1873
}
1874
1874
1875
+ void setIdealExpectedType (Type Ty) {
1876
+ expectedTypeContext.idealType = Ty;
1877
+ }
1878
+
1875
1879
CodeCompletionContext::TypeContextKind typeContextKind () const {
1876
1880
if (expectedTypeContext.empty ()) {
1877
1881
return CodeCompletionContext::TypeContextKind::None;
@@ -2011,12 +2015,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2011
2015
if (ForcedSemanticContext)
2012
2016
return *ForcedSemanticContext;
2013
2017
2014
- if (IsUnresolvedMember) {
2015
- if (isa<EnumElementDecl>(D)) {
2016
- return SemanticContextKind::ExpressionSpecific;
2017
- }
2018
- }
2019
-
2020
2018
switch (Reason) {
2021
2019
case DeclVisibilityKind::LocalVariable:
2022
2020
case DeclVisibilityKind::FunctionParameter:
@@ -2080,6 +2078,23 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2080
2078
llvm_unreachable (" unhandled kind" );
2081
2079
}
2082
2080
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
+
2083
2098
void addValueBaseName (CodeCompletionResultBuilder &Builder,
2084
2099
DeclBaseName Name) {
2085
2100
auto NameStr = Name.userFacingName ();
@@ -2406,6 +2421,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2406
2421
DynamicOrOptional);
2407
2422
else
2408
2423
addTypeAnnotation (Builder, VarType);
2424
+
2425
+ if (isUnresolvedMemberIdealType (VarType))
2426
+ Builder.setSemanticContext (SemanticContextKind::ExpressionSpecific);
2409
2427
}
2410
2428
2411
2429
static bool hasInterestingDefaultValues (const AbstractFunctionDecl *func) {
@@ -2862,6 +2880,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
2862
2880
}
2863
2881
}
2864
2882
Builder.addTypeAnnotation (TypeStr);
2883
+
2884
+ if (isUnresolvedMemberIdealType (ResultType))
2885
+ Builder.setSemanticContext (SemanticContextKind::ExpressionSpecific);
2865
2886
};
2866
2887
2867
2888
if (!AFT || IsImplicitlyCurriedInstanceMethod) {
@@ -3148,6 +3169,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3148
3169
}
3149
3170
3150
3171
addTypeAnnotation (Builder, EnumType);
3172
+
3173
+ if (isUnresolvedMemberIdealType (EnumType))
3174
+ Builder.setSemanticContext (SemanticContextKind::ExpressionSpecific);
3151
3175
}
3152
3176
3153
3177
void addKeyword (StringRef Name, Type TypeAnnotation = Type(),
@@ -4117,7 +4141,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4117
4141
auto &SM = CurrDeclContext->getASTContext ().SourceMgr ;
4118
4142
if (DotLoc.isValid ())
4119
4143
bytesToErase = SM.getByteDistance (DotLoc, SM.getCodeCompletionLoc ());
4120
- addKeyword (" nil" , T, SemanticContextKind::ExpressionSpecific ,
4144
+ addKeyword (" nil" , T, SemanticContextKind::None ,
4121
4145
CodeCompletionKeywordKind::kw_nil, bytesToErase);
4122
4146
}
4123
4147
getUnresolvedMemberCompletions (T);
@@ -5755,6 +5779,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5755
5779
ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
5756
5780
Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5757
5781
ContextInfo.isSingleExpressionBody ());
5782
+ Lookup.setIdealExpectedType (CodeCompleteTokenExpr->getType ());
5758
5783
Lookup.getUnresolvedMemberCompletions (ContextInfo.getPossibleTypes ());
5759
5784
DoPostfixExprBeginning ();
5760
5785
break ;
@@ -5810,6 +5835,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5810
5835
ExprContextInfo ContextInfo (CurDeclContext, CodeCompleteTokenExpr);
5811
5836
Lookup.setExpectedTypes (ContextInfo.getPossibleTypes (),
5812
5837
ContextInfo.isSingleExpressionBody ());
5838
+ Lookup.setIdealExpectedType (CodeCompleteTokenExpr->getType ());
5813
5839
Lookup.getUnresolvedMemberCompletions (ContextInfo.getPossibleTypes ());
5814
5840
break ;
5815
5841
}
0 commit comments