Skip to content

Commit 7c0a178

Browse files
author
Nathan Hawes
committed
[Sema] Maintain the implicitness of call argument tuple/parens in coerceCallArguments
If any arguments were defaulted the tuple/paren was made implicit, even though the original tuple/paren was present in the source. This prevented some sourcekit ASTWalkers from considering them, making refactorings, documentation info, jump-to-definition and other features unavailable when queried via their argument labels. Resolves rdar://problem/62118957
1 parent 82d73b5 commit 7c0a178

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

include/swift/AST/Expr.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,9 +4133,9 @@ class DefaultArgumentExpr final : public Expr {
41334133
DefaultArgsOwner(defaultArgsOwner), ParamIndex(paramIndex), Loc(loc),
41344134
ContextOrCallerSideExpr(dc) { }
41354135

4136-
SourceRange getSourceRange() const {
4137-
return Loc;
4138-
}
4136+
SourceRange getSourceRange() const { return {}; }
4137+
4138+
SourceLoc getArgumentListLoc() const { return Loc; }
41394139

41404140
ConcreteDeclRef getDefaultArgsOwner() const {
41414141
return DefaultArgsOwner;

lib/IDE/SyntaxModel.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -547,22 +547,24 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
547547
return {false, E};
548548

549549
auto addCallArgExpr = [&](Expr *Elem, TupleExpr *ParentTupleExpr) {
550-
if (isCurrentCallArgExpr(ParentTupleExpr)) {
551-
CharSourceRange NR = parameterNameRangeOfCallArg(ParentTupleExpr, Elem);
552-
SyntaxStructureNode SN;
553-
SN.Kind = SyntaxStructureKind::Argument;
554-
SN.NameRange = NR;
555-
SN.BodyRange = charSourceRangeFromSourceRange(SM, Elem->getSourceRange());
556-
if (NR.isValid()) {
557-
SN.Range = charSourceRangeFromSourceRange(SM, SourceRange(NR.getStart(),
558-
Elem->getEndLoc()));
559-
passTokenNodesUntil(NR.getStart(), ExcludeNodeAtLocation);
560-
}
561-
else
562-
SN.Range = SN.BodyRange;
550+
if (isa<DefaultArgumentExpr>(Elem) ||
551+
!isCurrentCallArgExpr(ParentTupleExpr))
552+
return;
563553

564-
pushStructureNode(SN, Elem);
554+
CharSourceRange NR = parameterNameRangeOfCallArg(ParentTupleExpr, Elem);
555+
SyntaxStructureNode SN;
556+
SN.Kind = SyntaxStructureKind::Argument;
557+
SN.NameRange = NR;
558+
SN.BodyRange = charSourceRangeFromSourceRange(SM, Elem->getSourceRange());
559+
if (NR.isValid()) {
560+
SN.Range = charSourceRangeFromSourceRange(SM, SourceRange(NR.getStart(),
561+
Elem->getEndLoc()));
562+
passTokenNodesUntil(NR.getStart(), ExcludeNodeAtLocation);
565563
}
564+
else
565+
SN.Range = SN.BodyRange;
566+
567+
pushStructureNode(SN, Elem);
566568
};
567569

568570
if (auto *ParentTupleExpr = dyn_cast_or_null<TupleExpr>(Parent.getAsExpr())) {

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5759,9 +5759,10 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
57595759
// We already had a ParenExpr, so replace it's sub-expression.
57605760
argParen->setSubExpr(newArgs[0]);
57615761
} else {
5762+
bool isImplicit = arg->isImplicit();
57625763
arg = new (ctx)
57635764
ParenExpr(lParenLoc, newArgs[0], rParenLoc, hasTrailingClosure);
5764-
arg->setImplicit();
5765+
arg->setImplicit(isImplicit);
57655766
}
57665767
} else {
57675768
assert(isa<TupleType>(paramType.getPointer()));
@@ -5776,7 +5777,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
57765777
// Build a new TupleExpr, re-using source location information.
57775778
arg = TupleExpr::create(ctx, lParenLoc, newArgs, newLabels, newLabelLocs,
57785779
rParenLoc, hasTrailingClosure,
5779-
/*implicit=*/true);
5780+
/*implicit=*/arg->isImplicit());
57805781
}
57815782
}
57825783

lib/Sema/TypeCheckExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ Expr *CallerSideDefaultArgExprRequest::evaluate(
788788
auto paramTy = defaultExpr->getType();
789789

790790
// Re-create the default argument using the location info of the call site.
791-
auto *initExpr = synthesizeCallerSideDefault(param, defaultExpr->getLoc());
791+
auto *initExpr =
792+
synthesizeCallerSideDefault(param, defaultExpr->getArgumentListLoc());
792793
auto *dc = defaultExpr->ContextOrCallerSideExpr.get<DeclContext *>();
793794
assert(dc && "Expected a DeclContext before type-checking caller-side arg");
794795

test/SourceKit/Refactoring/basic.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ struct TestDefaultedParen {
106106

107107
TestDefaultedParen.init()
108108

109+
struct HasInitWithDefaultArgs {
110+
init(x: Int = 10, y: Int = 20, z: Int = 10)
111+
}
112+
113+
HasInitWithDefaultArgs(z: 45)
114+
HasInitWithDefaultArgs(y: 45, z: 89)
115+
109116
// RUN: %sourcekitd-test -req=cursor -pos=3:1 -end-pos=5:13 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK1
110117

111118
// CHECK1: ACTIONS BEGIN
@@ -139,6 +146,11 @@ TestDefaultedParen.init()
139146

140147
// RUN: %sourcekitd-test -req=cursor -pos=107:20 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-NORENAME
141148

149+
// RUN: %sourcekitd-test -req=cursor -pos=113:24 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
150+
// RUN: %sourcekitd-test -req=cursor -pos=114:24 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
151+
// RUN: %sourcekitd-test -req=cursor -pos=114:31 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
152+
// RUN: %sourcekitd-test -req=cursor -pos=114:31 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
153+
142154
// RUN: %sourcekitd-test -req=cursor -pos=35:10 -end-pos=35:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
143155
// RUN: %sourcekitd-test -req=cursor -pos=35:10 -end-pos=35:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
144156

0 commit comments

Comments
 (0)