Skip to content

[Sema] Maintain the implicitness of call argument tuple/parens in coerceCallArguments #31184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4133,9 +4133,9 @@ class DefaultArgumentExpr final : public Expr {
DefaultArgsOwner(defaultArgsOwner), ParamIndex(paramIndex), Loc(loc),
ContextOrCallerSideExpr(dc) { }

SourceRange getSourceRange() const {
return Loc;
}
SourceRange getSourceRange() const { return {}; }

SourceLoc getArgumentListLoc() const { return Loc; }

ConcreteDeclRef getDefaultArgsOwner() const {
return DefaultArgsOwner;
Expand Down
30 changes: 16 additions & 14 deletions lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,22 +547,24 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
return {false, E};

auto addCallArgExpr = [&](Expr *Elem, TupleExpr *ParentTupleExpr) {
if (isCurrentCallArgExpr(ParentTupleExpr)) {
CharSourceRange NR = parameterNameRangeOfCallArg(ParentTupleExpr, Elem);
SyntaxStructureNode SN;
SN.Kind = SyntaxStructureKind::Argument;
SN.NameRange = NR;
SN.BodyRange = charSourceRangeFromSourceRange(SM, Elem->getSourceRange());
if (NR.isValid()) {
SN.Range = charSourceRangeFromSourceRange(SM, SourceRange(NR.getStart(),
Elem->getEndLoc()));
passTokenNodesUntil(NR.getStart(), ExcludeNodeAtLocation);
}
else
SN.Range = SN.BodyRange;
if (isa<DefaultArgumentExpr>(Elem) ||
!isCurrentCallArgExpr(ParentTupleExpr))
return;

pushStructureNode(SN, Elem);
CharSourceRange NR = parameterNameRangeOfCallArg(ParentTupleExpr, Elem);
SyntaxStructureNode SN;
SN.Kind = SyntaxStructureKind::Argument;
SN.NameRange = NR;
SN.BodyRange = charSourceRangeFromSourceRange(SM, Elem->getSourceRange());
if (NR.isValid()) {
SN.Range = charSourceRangeFromSourceRange(SM, SourceRange(NR.getStart(),
Elem->getEndLoc()));
passTokenNodesUntil(NR.getStart(), ExcludeNodeAtLocation);
}
else
SN.Range = SN.BodyRange;

pushStructureNode(SN, Elem);
};

if (auto *ParentTupleExpr = dyn_cast_or_null<TupleExpr>(Parent.getAsExpr())) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5759,9 +5759,10 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
// We already had a ParenExpr, so replace it's sub-expression.
argParen->setSubExpr(newArgs[0]);
} else {
bool isImplicit = arg->isImplicit();
arg = new (ctx)
ParenExpr(lParenLoc, newArgs[0], rParenLoc, hasTrailingClosure);
arg->setImplicit();
arg->setImplicit(isImplicit);
}
} else {
assert(isa<TupleType>(paramType.getPointer()));
Expand All @@ -5776,7 +5777,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
// Build a new TupleExpr, re-using source location information.
arg = TupleExpr::create(ctx, lParenLoc, newArgs, newLabels, newLabelLocs,
rParenLoc, hasTrailingClosure,
/*implicit=*/true);
/*implicit=*/arg->isImplicit());
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,8 @@ Expr *CallerSideDefaultArgExprRequest::evaluate(
auto paramTy = defaultExpr->getType();

// Re-create the default argument using the location info of the call site.
auto *initExpr = synthesizeCallerSideDefault(param, defaultExpr->getLoc());
auto *initExpr =
synthesizeCallerSideDefault(param, defaultExpr->getArgumentListLoc());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xedin this is the bit I was missing that caused the test failures previously. getLoc() was based on getSourceRange().

auto *dc = defaultExpr->ContextOrCallerSideExpr.get<DeclContext *>();
assert(dc && "Expected a DeclContext before type-checking caller-side arg");

Expand Down
12 changes: 12 additions & 0 deletions test/SourceKit/Refactoring/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ struct TestDefaultedParen {

TestDefaultedParen.init()

struct HasInitWithDefaultArgs {
init(x: Int = 10, y: Int = 20, z: Int = 10)
}

HasInitWithDefaultArgs(z: 45)
HasInitWithDefaultArgs(y: 45, z: 89)

// RUN: %sourcekitd-test -req=cursor -pos=3:1 -end-pos=5:13 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK1

// CHECK1: ACTIONS BEGIN
Expand Down Expand Up @@ -139,6 +146,11 @@ TestDefaultedParen.init()

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

// RUN: %sourcekitd-test -req=cursor -pos=113:24 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
// RUN: %sourcekitd-test -req=cursor -pos=114:24 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
// RUN: %sourcekitd-test -req=cursor -pos=114:31 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
// RUN: %sourcekitd-test -req=cursor -pos=114:31 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL

// RUN: %sourcekitd-test -req=cursor -pos=35:10 -end-pos=35:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
// RUN: %sourcekitd-test -req=cursor -pos=35:10 -end-pos=35:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT

Expand Down