@@ -948,7 +948,7 @@ static CodeCompletionResult::ExpectedTypeRelation calculateTypeRelation(
948
948
if (!Ty->hasTypeParameter () && !ExpectedTy->hasTypeParameter ()) {
949
949
if (Ty->isEqual (ExpectedTy))
950
950
return CodeCompletionResult::ExpectedTypeRelation::Identical;
951
- if (isConvertibleTo (Ty, ExpectedTy, *DC))
951
+ if (!ExpectedTy-> isAny () && isConvertibleTo (Ty, ExpectedTy, *DC))
952
952
return CodeCompletionResult::ExpectedTypeRelation::Convertible;
953
953
}
954
954
if (auto FT = Ty->getAs <AnyFunctionType>()) {
@@ -3892,6 +3892,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3892
3892
3893
3893
static bool getPositionInArgs (DeclContext &DC, Expr *Args, Expr *CCExpr,
3894
3894
unsigned &Position, bool &HasName) {
3895
+ if (auto TSE = dyn_cast<TupleShuffleExpr>(Args))
3896
+ Args = TSE->getSubExpr ();
3897
+
3895
3898
if (isa<ParenExpr>(Args)) {
3896
3899
HasName = false ;
3897
3900
Position = 0 ;
@@ -3914,6 +3917,38 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3914
3917
return false ;
3915
3918
}
3916
3919
3920
+ // / Translate argument index in \p Args to parameter index.
3921
+ // / Does nothing unless \p Args is \c TupleShuffleExpr.
3922
+ static bool translateArgIndexToParamIndex (Expr *Args,
3923
+ unsigned &Position, bool &HasName) {
3924
+ auto TSE = dyn_cast<TupleShuffleExpr>(Args);
3925
+ if (!TSE)
3926
+ return true ;
3927
+
3928
+ auto mapping = TSE->getElementMapping ();
3929
+ for (unsigned destIdx = 0 , e = mapping.size (); destIdx != e; ++destIdx) {
3930
+ auto srcIdx = mapping[destIdx];
3931
+ if (srcIdx == (signed )Position) {
3932
+ Position = destIdx;
3933
+ return true ;
3934
+ }
3935
+ if (srcIdx == TupleShuffleExpr::Variadic &&
3936
+ llvm::is_contained (TSE->getVariadicArgs (), Position)) {
3937
+ // The arg is a part of variadic args.
3938
+ Position = destIdx;
3939
+ HasName = false ;
3940
+ if (auto Args = dyn_cast<TupleExpr>(TSE->getSubExpr ())) {
3941
+ // Check if the first variadiac argument has the label.
3942
+ auto firstVarArgIdx = TSE->getVariadicArgs ().front ();
3943
+ HasName = Args->getElementNameLoc (firstVarArgIdx).isValid ();
3944
+ }
3945
+ return true ;
3946
+ }
3947
+ }
3948
+
3949
+ return false ;
3950
+ }
3951
+
3917
3952
static bool
3918
3953
collectArgumentExpectation (DeclContext &DC, ApplyExpr *CallE, Expr *CCExpr,
3919
3954
std::vector<Type> &ExpectedTypes,
@@ -3928,6 +3963,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3928
3963
bool HasName;
3929
3964
if (!getPositionInArgs (DC, CallE->getArg (), CCExpr, Position, HasName))
3930
3965
return false ;
3966
+ if (!translateArgIndexToParamIndex (CallE->getArg (), Position, HasName))
3967
+ return false ;
3931
3968
3932
3969
// Collect possible types (or labels) at the position.
3933
3970
{
@@ -4989,7 +5026,7 @@ namespace {
4989
5026
class ExprParentFinder : public ASTWalker {
4990
5027
friend class CodeCompletionTypeContextAnalyzer ;
4991
5028
Expr *ChildExpr;
4992
- llvm::function_ref<bool (ParentTy)> Predicate;
5029
+ llvm::function_ref<bool (ParentTy, ParentTy )> Predicate;
4993
5030
4994
5031
bool arePositionsSame (Expr *E1 , Expr *E2 ) {
4995
5032
return E1 ->getSourceRange ().Start == E2 ->getSourceRange ().Start &&
@@ -5001,60 +5038,62 @@ namespace {
5001
5038
ParentTy ParentClosest;
5002
5039
ParentTy ParentFarthest;
5003
5040
ExprParentFinder (Expr* ChildExpr,
5004
- llvm::function_ref<bool (ParentTy)> Predicate) :
5041
+ llvm::function_ref<bool (ParentTy, ParentTy )> Predicate) :
5005
5042
ChildExpr (ChildExpr), Predicate(Predicate) {}
5006
5043
5007
5044
std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
5045
+ if (E != ChildExpr && Predicate (E, Parent)) {
5046
+ Ancestors.push_back (E);
5047
+ return { true , E };
5048
+ }
5008
5049
if (E == ChildExpr || arePositionsSame (E, ChildExpr)) {
5009
5050
if (!Ancestors.empty ()) {
5010
5051
ParentClosest = Ancestors.back ();
5011
5052
ParentFarthest = Ancestors.front ();
5012
5053
}
5013
5054
return {false , nullptr };
5014
5055
}
5015
- if (Predicate (E))
5016
- Ancestors.push_back (E);
5017
5056
return { true , E };
5018
5057
}
5019
5058
5020
5059
Expr *walkToExprPost (Expr *E) override {
5021
- if (Predicate (E))
5060
+ if (Predicate (E, Parent ))
5022
5061
Ancestors.pop_back ();
5023
5062
return E;
5024
5063
}
5025
5064
5026
5065
std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
5027
- if (Predicate (S))
5066
+ if (Predicate (S, Parent ))
5028
5067
Ancestors.push_back (S);
5029
5068
return { true , S };
5030
5069
}
5031
5070
5032
5071
Stmt *walkToStmtPost (Stmt *S) override {
5033
- if (Predicate (S))
5072
+ if (Predicate (S, Parent ))
5034
5073
Ancestors.pop_back ();
5035
5074
return S;
5036
5075
}
5037
5076
5038
5077
bool walkToDeclPre (Decl *D) override {
5039
- if (Predicate (D))
5078
+ if (Predicate (D, Parent ))
5040
5079
Ancestors.push_back (D);
5041
5080
return true ;
5042
5081
}
5043
5082
5044
5083
bool walkToDeclPost (Decl *D) override {
5045
- if (Predicate (D))
5084
+ if (Predicate (D, Parent ))
5046
5085
Ancestors.pop_back ();
5047
5086
return true ;
5048
5087
}
5049
5088
5050
5089
std::pair<bool , Pattern *> walkToPatternPre (Pattern *P) override {
5051
- if (Predicate (P))
5090
+ if (Predicate (P, Parent ))
5052
5091
Ancestors.push_back (P);
5053
5092
return { true , P };
5054
5093
}
5055
5094
5056
5095
Pattern *walkToPatternPost (Pattern *P) override {
5057
- if (Predicate (P))
5096
+ if (Predicate (P, Parent ))
5058
5097
Ancestors.pop_back ();
5059
5098
return P;
5060
5099
}
@@ -5074,14 +5113,20 @@ class CodeCompletionTypeContextAnalyzer {
5074
5113
CodeCompletionTypeContextAnalyzer (DeclContext *DC, Expr *ParsedExpr) : DC(DC),
5075
5114
ParsedExpr (ParsedExpr), SM(DC->getASTContext ().SourceMgr),
5076
5115
Context(DC->getASTContext ()),
5077
- Finder(ParsedExpr, [](ASTWalker::ParentTy Node) {
5116
+ Finder(ParsedExpr, [](ASTWalker::ParentTy Node, ASTWalker::ParentTy Parent ) {
5078
5117
if (auto E = Node.getAsExpr ()) {
5079
5118
switch (E->getKind ()) {
5080
5119
case ExprKind::Call:
5081
5120
case ExprKind::Binary:
5082
5121
case ExprKind::PrefixUnary:
5083
5122
case ExprKind::Assign:
5084
5123
return true ;
5124
+ case ExprKind::Tuple: {
5125
+ auto ParentE = Parent.getAsExpr ();
5126
+ return !ParentE || (!isa<CallExpr>(ParentE) &&
5127
+ !isa<BinaryExpr>(ParentE)&&
5128
+ !isa<TupleShuffleExpr>(ParentE));
5129
+ }
5085
5130
default :
5086
5131
return false ;
5087
5132
}
@@ -5150,6 +5195,16 @@ class CodeCompletionTypeContextAnalyzer {
5150
5195
}
5151
5196
break ;
5152
5197
}
5198
+ case ExprKind::Tuple: {
5199
+ if (!Parent->getType () || !Parent->getType ()->is <TupleType>())
5200
+ return ;
5201
+ unsigned Position = 0 ;
5202
+ bool HasName;
5203
+ if (CompletionLookup::getPositionInArgs (*DC, Parent, ParsedExpr, Position, HasName)) {
5204
+ Callback (Parent->getType ()->castTo <TupleType>()->getElementType (Position));
5205
+ }
5206
+ break ;
5207
+ }
5153
5208
default :
5154
5209
llvm_unreachable (" Unhandled expression kind." );
5155
5210
}
0 commit comments