Skip to content

Commit 6175c90

Browse files
authored
Merge pull request swiftlang#31932 from rintaro/ide-completion-rdar62479469
[CodeCompletion] Handle variadic parameter in expr context analysis
2 parents d53cd53 + 2eb623e commit 6175c90

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,21 +656,22 @@ class ExprContextAnalyzer {
656656
if (memberDC && ty->hasTypeParameter())
657657
ty = memberDC->mapTypeIntoContext(ty);
658658

659+
bool canSkip =
660+
paramList && (paramList->get(Pos)->isDefaultArgument() ||
661+
paramList->get(Pos)->isVariadic());
662+
659663
if (paramType.hasLabel() && MayNeedName) {
660-
bool isDefaulted = paramList &&
661-
paramList->get(Pos)->isDefaultArgument();
662664
if (seenArgs.insert({paramType.getLabel(), ty.getPointer()}).second)
663-
recordPossibleParam(&paramType, !isDefaulted);
664-
if (isDefaulted)
665-
continue;
665+
recordPossibleParam(&paramType, !canSkip);
666666
} else {
667667
auto argTy = ty;
668668
if (paramType.isInOut())
669669
argTy = InOutType::get(argTy);
670670
if (seenTypes.insert(argTy.getPointer()).second)
671671
recordPossibleType(argTy);
672672
}
673-
break;
673+
if (!canSkip)
674+
break;
674675
}
675676
// If the argument position is out of expeceted number, indicate that
676677
// with optional nullptr param.
@@ -980,11 +981,13 @@ class ExprContextAnalyzer {
980981
case ExprKind::Binary:
981982
case ExprKind::PrefixUnary:
982983
case ExprKind::Assign:
983-
case ExprKind::Array:
984984
case ExprKind::Dictionary:
985985
case ExprKind::If:
986986
case ExprKind::UnresolvedMember:
987987
return true;
988+
case ExprKind::Array:
989+
return (!Parent.getAsExpr() ||
990+
!isa<VarargExpansionExpr>(Parent.getAsExpr()));
988991
case ExprKind::Tuple: {
989992
auto ParentE = Parent.getAsExpr();
990993
return !ParentE ||

test/IDE/complete_call_arg.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@
106106

107107
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYPATH_THUNK_BASE | %FileCheck %s -check-prefix=KEYPATH_THUNK_BASE
108108

109+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VARIADIC_1 | %FileCheck %s -check-prefix=VARIADIC_1
110+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VARIADIC_2 | %FileCheck %s -check-prefix=VARIADIC_2
111+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=VARIADIC_3 | %FileCheck %s -check-prefix=VARIADIC_2
112+
109113
var i1 = 1
110114
var i2 = 2
111115
var oi1 : Int?
@@ -848,3 +852,19 @@ func testKeyPathThunkInBase() {
848852
// KEYPATH_THUNK_BASE-DAG: Decl[EnumElement]/ExprSpecific/TypeRelation[Identical]: baz[#SimpleEnum#]; name=baz
849853
// KEYPATH_THUNK_BASE: End completions
850854
}
855+
856+
func testVariadic(_ arg: Any..., option1: Int = 0, option2: String = 1) {
857+
testVariadic(#^VARIADIC_1^#)
858+
// VARIADIC_1: Begin completions
859+
// VARIADIC_1-DAG: Decl[FreeFunction]/CurrModule: ['(']{#(arg): Any...#}, {#option1: Int#}, {#option2: String#}[')'][#Void#];
860+
// VARIADIC_1-DAG: Decl[GlobalVar]/CurrModule: i1[#Int#];
861+
// VARIADIC_1: End completions
862+
testVariadic(1, #^VARIADIC_2^#)
863+
// VARIADIC_2: Begin completions
864+
// VARIADIC_2-DAG: Pattern/ExprSpecific: {#option1: Int#}[#Int#];
865+
// VARIADIC_2-DAG: Pattern/ExprSpecific: {#option2: String#}[#String#];
866+
// VARIADIC_2-DAG: Decl[GlobalVar]/CurrModule: i1[#Int#];
867+
// VARIADIC_2: End completions
868+
testVariadic(1, 2, #^VARIADIC_3^#)
869+
// Same as VARIADIC_2.
870+
}

0 commit comments

Comments
 (0)