@@ -104,34 +104,39 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
104104
105105 auto *CallLocator = CS.getConstraintLocator (ParentCall);
106106 auto *CalleeLocator = S.getCalleeLocator (CallLocator);
107- auto SelectedOverload = S.getOverloadChoiceIfAvailable (CalleeLocator);
108- if (!SelectedOverload) {
109- return ;
110- }
111-
112- Type CallBaseTy = SelectedOverload->choice .getBaseType ();
113- if (CallBaseTy) {
114- CallBaseTy = S.simplifyType (CallBaseTy)->getRValueType ();
115- }
107+ ValueDecl *FuncD = nullptr ;
108+ Type FuncTy;
109+ Type CallBaseTy;
110+ // If we are calling a closure in-place there is no overload choice, but we
111+ // still have all the other required information (like the argument's
112+ // expected type) to provide useful code completion results.
113+ if (auto SelectedOverload = S.getOverloadChoiceIfAvailable (CalleeLocator)) {
114+
115+ CallBaseTy = SelectedOverload->choice .getBaseType ();
116+ if (CallBaseTy) {
117+ CallBaseTy = S.simplifyType (CallBaseTy)->getRValueType ();
118+ }
116119
117- ValueDecl *FuncD = SelectedOverload->choice .getDeclOrNull ();
118- Type FuncTy = S.simplifyType (SelectedOverload->openedType )->getRValueType ();
119-
120- // For completion as the arg in a call to the implicit [keypath: _] subscript
121- // the solver can't know what kind of keypath is expected without an actual
122- // argument (e.g. a KeyPath vs WritableKeyPath) so it ends up as a hole.
123- // Just assume KeyPath so we show the expected keypath's root type to users
124- // rather than '_'.
125- if (SelectedOverload->choice .getKind () ==
126- OverloadChoiceKind::KeyPathApplication) {
127- auto Params = FuncTy->getAs <AnyFunctionType>()->getParams ();
128- if (Params.size () == 1 && Params[0 ].getPlainType ()->is <UnresolvedType>()) {
129- auto *KPDecl = CS.getASTContext ().getKeyPathDecl ();
130- Type KPTy =
131- KPDecl->mapTypeIntoContext (KPDecl->getDeclaredInterfaceType ());
132- Type KPValueTy = KPTy->castTo <BoundGenericType>()->getGenericArgs ()[1 ];
133- KPTy = BoundGenericType::get (KPDecl, Type (), {CallBaseTy, KPValueTy});
134- FuncTy = FunctionType::get ({Params[0 ].withType (KPTy)}, KPValueTy);
120+ FuncD = SelectedOverload->choice .getDeclOrNull ();
121+ FuncTy = S.simplifyTypeForCodeCompletion (SelectedOverload->openedType );
122+
123+ // For completion as the arg in a call to the implicit [keypath: _]
124+ // subscript the solver can't know what kind of keypath is expected without
125+ // an actual argument (e.g. a KeyPath vs WritableKeyPath) so it ends up as a
126+ // hole. Just assume KeyPath so we show the expected keypath's root type to
127+ // users rather than '_'.
128+ if (SelectedOverload->choice .getKind () ==
129+ OverloadChoiceKind::KeyPathApplication) {
130+ auto Params = FuncTy->getAs <AnyFunctionType>()->getParams ();
131+ if (Params.size () == 1 &&
132+ Params[0 ].getPlainType ()->is <UnresolvedType>()) {
133+ auto *KPDecl = CS.getASTContext ().getKeyPathDecl ();
134+ Type KPTy =
135+ KPDecl->mapTypeIntoContext (KPDecl->getDeclaredInterfaceType ());
136+ Type KPValueTy = KPTy->castTo <BoundGenericType>()->getGenericArgs ()[1 ];
137+ KPTy = BoundGenericType::get (KPDecl, Type (), {CallBaseTy, KPValueTy});
138+ FuncTy = FunctionType::get ({Params[0 ].withType (KPTy)}, KPValueTy);
139+ }
135140 }
136141 }
137142
@@ -193,9 +198,13 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
193198 return ;
194199 }
195200
201+ llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
202+ getSolutionSpecificVarTypes (S, SolutionSpecificVarTypes);
203+
196204 Results.push_back ({ExpectedTy, isa<SubscriptExpr>(ParentCall), FuncD, FuncTy,
197205 ArgIdx, ParamIdx, std::move (ClaimedParams),
198- IsNoninitialVariadic, CallBaseTy, HasLabel, IsAsync});
206+ IsNoninitialVariadic, CallBaseTy, HasLabel, IsAsync,
207+ SolutionSpecificVarTypes});
199208}
200209
201210void ArgumentTypeCheckCompletionCallback::deliverResults (
@@ -231,15 +240,27 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
231240 SemanticContext = SemanticContextKind::CurrentNominal;
232241 }
233242 }
234- if (Result.IsSubscript ) {
235- assert (SemanticContext != SemanticContextKind::None);
236- auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.FuncD );
237- Lookup.addSubscriptCallPattern (Result.FuncTy ->getAs <AnyFunctionType>(),
238- SD, SemanticContext);
239- } else {
240- auto *FD = dyn_cast_or_null<AbstractFunctionDecl>(Result.FuncD );
241- Lookup.addFunctionCallPattern (Result.FuncTy ->getAs <AnyFunctionType>(),
242- FD, SemanticContext);
243+ if (SemanticContext == SemanticContextKind::None && Result.FuncD ) {
244+ if (Result.FuncD ->getDeclContext ()->isTypeContext ()) {
245+ SemanticContext = SemanticContextKind::CurrentNominal;
246+ } else if (Result.FuncD ->getDeclContext ()->isLocalContext ()) {
247+ SemanticContext = SemanticContextKind::Local;
248+ } else if (Result.FuncD ->getModuleContext () == DC->getParentModule ()) {
249+ SemanticContext = SemanticContextKind::CurrentModule;
250+ }
251+ }
252+ if (Result.FuncTy ) {
253+ if (auto FuncTy = Result.FuncTy ->lookThroughAllOptionalTypes ()
254+ ->getAs <AnyFunctionType>()) {
255+ if (Result.IsSubscript ) {
256+ assert (SemanticContext != SemanticContextKind::None);
257+ auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.FuncD );
258+ Lookup.addSubscriptCallPattern (FuncTy, SD, SemanticContext);
259+ } else {
260+ auto *FD = dyn_cast_or_null<AbstractFunctionDecl>(Result.FuncD );
261+ Lookup.addFunctionCallPattern (FuncTy, FD, SemanticContext);
262+ }
263+ }
243264 }
244265 }
245266 Lookup.setHaveLParen (false );
@@ -258,6 +279,7 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
258279 if (shouldPerformGlobalCompletion) {
259280 for (auto &Result : Results) {
260281 ExpectedTypes.push_back (Result.ExpectedType );
282+ Lookup.setSolutionSpecificVarTypes (Result.SolutionSpecificVarTypes );
261283 }
262284 Lookup.setExpectedTypes (ExpectedTypes, false );
263285 bool IsInAsyncContext = llvm::any_of (
0 commit comments