Skip to content

[IDE] Skip synthesized curry thunks and walk their unwrapped expression #38712

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
Aug 2, 2021
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: 4 additions & 2 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,10 @@ ClangNode extensionGetClangNode(const ExtensionDecl *ext);

/// Utility for finding the referenced declaration from a call, which might
/// include a second level of function application for a 'self.' expression,
/// or a curry thunk, etc.
std::pair<Type, ConcreteDeclRef> getReferencedDecl(Expr *expr);
/// or a curry thunk, etc. If \p semantic is true then the underlying semantic
/// expression of \p expr is used.
std::pair<Type, ConcreteDeclRef> getReferencedDecl(Expr *expr,
bool semantic = true);

/// Whether the last expression in \p ExprStack is being called.
bool isBeingCalled(ArrayRef<Expr*> ExprStack);
Expand Down
36 changes: 15 additions & 21 deletions lib/IDE/SourceEntityWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
return { false, E };
}

auto doStopTraversal = [&]() -> std::pair<bool, Expr *> {
Cancelled = true;
return { false, nullptr };
};

// Skip the synthesized curry thunks and just walk over the unwrapped
// expression
if (auto *ACE = dyn_cast<AutoClosureExpr>(E)) {
if (auto *SubExpr = ACE->getUnwrappedCurryThunkExpr()) {
if (!SubExpr->walk(*this))
return doStopTraversal();
return { false, E };
}
}

if (!SEWalker.walkToExprPre(E)) {
return { false, E };
}
Expand All @@ -291,30 +306,9 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
return { false, E };
};

auto doStopTraversal = [&]() -> std::pair<bool, Expr *> {
Cancelled = true;
return { false, nullptr };
};

if (auto *CtorRefE = dyn_cast<ConstructorRefCallExpr>(E))
CtorRefs.push_back(CtorRefE);

if (auto *ACE = dyn_cast<AutoClosureExpr>(E)) {
if (auto *SubExpr = ACE->getUnwrappedCurryThunkExpr()) {
if (auto *DRE = dyn_cast<DeclRefExpr>(SubExpr)) {
if (!passReference(DRE->getDecl(), DRE->getType(),
DRE->getNameLoc(),
ReferenceMetaData(getReferenceKind(Parent.getAsExpr(), DRE),
OpAccess)))
return doStopTraversal();

return doSkipChildren();
}
}

return { true, E };
}

if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
auto *FD = dyn_cast<FuncDecl>(DRE->getDecl());
// Handle implicit callAsFunction reference. An explicit reference will be
Expand Down
12 changes: 11 additions & 1 deletion lib/IDE/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,11 @@ ClangNode swift::ide::extensionGetClangNode(const ExtensionDecl *ext) {
return ClangNode();
}

std::pair<Type, ConcreteDeclRef> swift::ide::getReferencedDecl(Expr *expr) {
std::pair<Type, ConcreteDeclRef> swift::ide::getReferencedDecl(Expr *expr,
bool semantic) {
if (semantic)
expr = expr->getSemanticsProvidingExpr();

auto exprTy = expr->getType();

// Look through unbound instance member accesses.
Expand Down Expand Up @@ -1219,13 +1223,19 @@ Expr *swift::ide::getBase(ArrayRef<Expr *> ExprStack) {
Expr *CurrentE = ExprStack.back();
Expr *ParentE = getContainingExpr(ExprStack, 1);
Expr *Base = nullptr;

if (auto DSE = dyn_cast_or_null<DotSyntaxCallExpr>(ParentE))
Base = DSE->getBase();
else if (auto MRE = dyn_cast<MemberRefExpr>(CurrentE))
Base = MRE->getBase();
else if (auto SE = dyn_cast<SubscriptExpr>(CurrentE))
Base = SE->getBase();

// Look through curry thunks
if (auto ACE = dyn_cast_or_null<AutoClosureExpr>(Base))
if (auto *Unwrapped = ACE->getUnwrappedCurryThunkExpr())
Base = Unwrapped;

if (Base) {
while (auto ICE = dyn_cast<ImplicitConversionExpr>(Base))
Base = ICE->getSubExpr();
Expand Down
2 changes: 1 addition & 1 deletion lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
}
return false;
};
if (auto *VD = getReferencedDecl(Call).second.getDecl())
if (auto *VD = getReferencedDecl(Call, /*semantic=*/false).second.getDecl())
if (handleDecl(VD, Call->getSourceRange()))
return true;

Expand Down
12 changes: 12 additions & 0 deletions test/Index/index_curry_thunk.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s

struct SomeStruct {
func simple(_ someClosure: () -> Void) { }
}

func test(s: SomeStruct) {
s.simple { }
// CHECK: [[@LINE-1]]:5 | instance-method/Swift | simple(_:) | s:14swift_ide_test10SomeStructV6simpleyyyyXEF | Ref,Call,RelRec,RelCall,RelCont | rel: 2
(((s).simple)) { }
// CHECK: [[@LINE-1]]:9 | instance-method/Swift | simple(_:) | s:14swift_ide_test10SomeStructV6simpleyyyyXEF | Ref,Call,RelRec,RelCall,RelCont | rel: 2
}