@@ -38,12 +38,6 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
3838
3939 ArrayRef<AnyFunctionType::Param> ParamsToPass = Res.FuncTy ->getParams ();
4040
41- ParameterList *PL = nullptr ;
42- if (Res.FuncD ) {
43- PL = swift::getParameterList (Res.FuncD );
44- }
45- assert (!PL || PL->size () == ParamsToPass.size ());
46-
4741 bool ShowGlobalCompletions = false ;
4842 for (auto Idx : range (*Res.ParamIdx , ParamsToPass.size ())) {
4943 bool IsCompletion = (Idx == Res.ParamIdx );
@@ -53,22 +47,35 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
5347 break ;
5448 }
5549
56- const AnyFunctionType::Param *P = &ParamsToPass[Idx];
57- bool Required =
58- !(PL && PL->get (Idx)->isDefaultArgument ()) && !P->isVariadic ();
50+ // We work with the parameter from the function type and the declaration
51+ // because they contain different information that we need.
52+ //
53+ // Since not all function types are backed by declarations (e.g. closure
54+ // paramters), `DeclParam` might be `nullptr`.
55+ const AnyFunctionType::Param *TypeParam = &ParamsToPass[Idx];
56+ const ParamDecl *DeclParam = getParameterAt (Res.FuncDeclRef , Idx);
57+
58+ bool Required = true ;
59+ if (DeclParam && DeclParam->isDefaultArgument ()) {
60+ Required = false ;
61+ } else if (DeclParam && DeclParam->getType ()->is <PackExpansionType>()) {
62+ Required = false ;
63+ } else if (TypeParam->isVariadic ()) {
64+ Required = false ;
65+ }
5966
60- if (P ->hasLabel () && !(IsCompletion && Res.IsNoninitialVariadic )) {
67+ if (TypeParam ->hasLabel () && !(IsCompletion && Res.IsNoninitialVariadic )) {
6168 // Suggest parameter label if parameter has label, we are completing in it
6269 // and it is not a variadic parameter that already has arguments
63- PossibleParamInfo PP (P , Required);
70+ PossibleParamInfo PP (TypeParam , Required);
6471 if (!llvm::is_contained (Params, PP)) {
6572 Params.push_back (std::move (PP));
6673 }
6774 } else {
6875 // We have a parameter that doesn't require a label. Suggest global
6976 // results for that type.
7077 ShowGlobalCompletions = true ;
71- Types.push_back (P ->getPlainType ());
78+ Types.push_back (TypeParam ->getPlainType ());
7279 }
7380 if (Required) {
7481 // The user should only be suggested the first required param. Stop.
@@ -157,7 +164,7 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
157164 auto *CalleeLocator = S.getCalleeLocator (CallLocator);
158165
159166 auto Info = getSelectedOverloadInfo (S, CalleeLocator);
160- if (Info.Value && Info.Value ->shouldHideFromEditor ()) {
167+ if (Info.getValue () && Info.getValue () ->shouldHideFromEditor ()) {
161168 return ;
162169 }
163170 // Disallow invalid initializer references
@@ -215,7 +222,7 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
215222
216223 // If this is a duplicate of any other result, ignore this solution.
217224 if (llvm::any_of (Results, [&](const Result &R) {
218- return R.FuncD == Info.Value &&
225+ return R.FuncDeclRef == Info.ValueRef &&
219226 nullableTypesEqual (R.FuncTy , Info.ValueTy ) &&
220227 nullableTypesEqual (R.BaseType , Info.BaseTy ) &&
221228 R.ParamIdx == ParamIdx &&
@@ -232,9 +239,10 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
232239 FuncTy = Info.ValueTy ->lookThroughAllOptionalTypes ()->getAs <AnyFunctionType>();
233240 }
234241 Results.push_back ({ExpectedTy, ExpectedCallType,
235- isa<SubscriptExpr>(ParentCall), Info.Value , FuncTy, ArgIdx,
236- ParamIdx, std::move (ClaimedParams), IsNoninitialVariadic,
237- Info.BaseTy , HasLabel, IsAsync, SolutionSpecificVarTypes});
242+ isa<SubscriptExpr>(ParentCall), Info.ValueRef , FuncTy,
243+ ArgIdx, ParamIdx, std::move (ClaimedParams),
244+ IsNoninitialVariadic, Info.BaseTy , HasLabel, IsAsync,
245+ SolutionSpecificVarTypes});
238246}
239247
240248void ArgumentTypeCheckCompletionCallback::computeShadowedDecls (
@@ -243,24 +251,25 @@ void ArgumentTypeCheckCompletionCallback::computeShadowedDecls(
243251 auto &ResultA = Results[i];
244252 for (size_t j = i + 1 ; j < Results.size (); ++j) {
245253 auto &ResultB = Results[j];
246- if (!ResultA.FuncD || !ResultB.FuncD || !ResultA.FuncTy || !ResultB.FuncTy ) {
254+ if (!ResultA.getFuncD () || !ResultB.getFuncD () || !ResultA.FuncTy ||
255+ !ResultB.FuncTy ) {
247256 continue ;
248257 }
249- if (ResultA.FuncD ->getName () != ResultB.FuncD ->getName ()) {
258+ if (ResultA.getFuncD () ->getName () != ResultB.getFuncD () ->getName ()) {
250259 continue ;
251260 }
252261 if (!ResultA.FuncTy ->isEqual (ResultB.FuncTy )) {
253262 continue ;
254263 }
255264 ProtocolDecl *inProtocolExtensionA =
256- ResultA.FuncD ->getDeclContext ()->getExtendedProtocolDecl ();
265+ ResultA.getFuncD () ->getDeclContext ()->getExtendedProtocolDecl ();
257266 ProtocolDecl *inProtocolExtensionB =
258- ResultB.FuncD ->getDeclContext ()->getExtendedProtocolDecl ();
267+ ResultB.getFuncD () ->getDeclContext ()->getExtendedProtocolDecl ();
259268
260269 if (inProtocolExtensionA && !inProtocolExtensionB) {
261- ShadowedDecls.insert (ResultA.FuncD );
270+ ShadowedDecls.insert (ResultA.getFuncD () );
262271 } else if (!inProtocolExtensionA && inProtocolExtensionB) {
263- ShadowedDecls.insert (ResultB.FuncD );
272+ ShadowedDecls.insert (ResultB.getFuncD () );
264273 }
265274 }
266275 }
@@ -301,35 +310,37 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
301310 }
302311 if ((BaseNominal = BaseTy->getAnyNominal ())) {
303312 SemanticContext = SemanticContextKind::CurrentNominal;
304- if (Result.FuncD &&
305- Result.FuncD ->getDeclContext ()->getSelfNominalTypeDecl () !=
313+ if (Result.getFuncD () &&
314+ Result.getFuncD () ->getDeclContext ()->getSelfNominalTypeDecl () !=
306315 BaseNominal) {
307316 SemanticContext = SemanticContextKind::Super;
308317 }
309318 } else if (BaseTy->is <TupleType>() || BaseTy->is <SubstitutableType>()) {
310319 SemanticContext = SemanticContextKind::CurrentNominal;
311320 }
312321 }
313- if (SemanticContext == SemanticContextKind::None && Result.FuncD ) {
314- if (Result.FuncD ->getDeclContext ()->isTypeContext ()) {
322+ if (SemanticContext == SemanticContextKind::None && Result.getFuncD () ) {
323+ if (Result.getFuncD () ->getDeclContext ()->isTypeContext ()) {
315324 SemanticContext = SemanticContextKind::CurrentNominal;
316- } else if (Result.FuncD ->getDeclContext ()->isLocalContext ()) {
325+ } else if (Result.getFuncD () ->getDeclContext ()->isLocalContext ()) {
317326 SemanticContext = SemanticContextKind::Local;
318- } else if (Result.FuncD ->getModuleContext () == DC->getParentModule ()) {
327+ } else if (Result.getFuncD ()->getModuleContext () ==
328+ DC->getParentModule ()) {
319329 SemanticContext = SemanticContextKind::CurrentModule;
320330 }
321331 }
322332 if (Result.FuncTy ) {
323333 if (auto FuncTy = Result.FuncTy ) {
324- if (ShadowedDecls.count (Result.FuncD ) == 0 ) {
334+ if (ShadowedDecls.count (Result.getFuncD () ) == 0 ) {
325335 // Don't show call pattern completions if the function is
326336 // overridden.
327337 if (Result.IsSubscript ) {
328338 assert (SemanticContext != SemanticContextKind::None);
329- auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.FuncD );
339+ auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.getFuncD () );
330340 Lookup.addSubscriptCallPattern (FuncTy, SD, SemanticContext);
331341 } else {
332- auto *FD = dyn_cast_or_null<AbstractFunctionDecl>(Result.FuncD );
342+ auto *FD =
343+ dyn_cast_or_null<AbstractFunctionDecl>(Result.getFuncD ());
333344 Lookup.addFunctionCallPattern (FuncTy, FD, SemanticContext);
334345 }
335346 }
0 commit comments