Skip to content

Sema: Rewrite partial applications into closures #28698

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 4 commits into from
Mar 18, 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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ Swift Next
// OK, <U where U: Equatable> has broader visibility than <T where T == Int>
override func foo() where U: Equatable { ... }
}

* [SR-75][]:

Unapplied references to protocol methods methods are now supported. Previously this
Copy link
Contributor

Choose a reason for hiding this comment

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

methods methods ➡️ methods

-Unapplied references to protocol methods methods are now supported. Previously this
+Unapplied references to protocol methods are now supported. Previously this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

only worked for methods defined in structs, enums and classes.

```swift
protocol Cat {
func play(catToy: Toy)
}

let fn = Cat.play
Copy link
Contributor

Choose a reason for hiding this comment

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

Require parameter names when referencing to functions?

-let fn = Cat.play
+let fn = Cat.play(catToy:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not actually required, but I agree it makes the example more clear.

fn(myCat)(myToy)
```

* [SE-0266][]:
Expand Down Expand Up @@ -7939,6 +7952,7 @@ Swift 1.0
[SE-0267]: <https://github.com/apple/swift-evolution/blob/master/proposals/0267-where-on-contextually-generic.md>
[SE-0269]: <https://github.com/apple/swift-evolution/blob/master/proposals/0269-implicit-self-explicit-capture.md>

[SR-75]: <https://bugs.swift.org/browse/SR-75>
[SR-106]: <https://bugs.swift.org/browse/SR-106>
[SR-419]: <https://bugs.swift.org/browse/SR-419>
[SR-631]: <https://bugs.swift.org/browse/SR-631>
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,11 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
case AutoClosureExpr::Kind::SingleCurryThunk: {
auto *body = getSingleExpressionBody();
body = body->getSemanticsProvidingExpr();

if (auto *openExistential = dyn_cast<OpenExistentialExpr>(body)) {
body = openExistential->getSubExpr();
}

if (auto *outerCall = dyn_cast<ApplyExpr>(body)) {
return outerCall->getFn();
}
Expand All @@ -1866,6 +1871,11 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
AutoClosureExpr::Kind::SingleCurryThunk);
auto *innerBody = innerClosure->getSingleExpressionBody();
innerBody = innerBody->getSemanticsProvidingExpr();

if (auto *openExistential = dyn_cast<OpenExistentialExpr>(innerBody)) {
innerBody = openExistential->getSubExpr();
}

if (auto *outerCall = dyn_cast<ApplyExpr>(innerBody)) {
if (auto *innerCall = dyn_cast<ApplyExpr>(outerCall->getFn())) {
if (auto *declRef = dyn_cast<DeclRefExpr>(innerCall->getFn())) {
Expand Down
115 changes: 55 additions & 60 deletions lib/IDE/SourceEntityWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,23 @@ static SemaReferenceKind getReferenceKind(Expr *Parent, Expr *E) {
std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
assert(E);

std::pair<bool, Expr *> stopTraversal = { false, nullptr };
std::pair<bool, Expr *> skipChildren = { false, E };

auto doSkipChildren = [&]() -> std::pair<bool, Expr *> {
if (!SEWalker.walkToExprPost(E))
return stopTraversal;
return skipChildren;
};

if (isDone())
return { false, nullptr };
return stopTraversal;

if (ExprsToSkip.count(E) != 0)
return { false, E };
return skipChildren;

if (!SEWalker.walkToExprPre(E))
return { false, E };
return skipChildren;

if (auto *CtorRefE = dyn_cast<ConstructorRefCallExpr>(E))
CtorRefs.push_back(CtorRefE);
Expand All @@ -262,15 +271,16 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *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 { false, nullptr };
}
DRE->getNameLoc(),
ReferenceMetaData(getReferenceKind(Parent.getAsExpr(), DRE),
OpAccess)))
return stopTraversal;

return { true, E };
return doSkipChildren();
}
}

return { true, E };
}

if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Expand Down Expand Up @@ -300,12 +310,12 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
if (auto *module = dyn_cast<ModuleDecl>(DRE->getDecl())) {
if (!passReference(ModuleEntity(module),
{module->getName(), E->getLoc()}))
return { false, nullptr };
return stopTraversal;
} else if (!passReference(DRE->getDecl(), DRE->getType(),
DRE->getNameLoc(),
ReferenceMetaData(getReferenceKind(Parent.getAsExpr(), DRE),
OpAccess))) {
return { false, nullptr };
return stopTraversal;
}
} else if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
{
Expand All @@ -324,31 +334,29 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {

// Visit in source order.
if (!MRE->getBase()->walk(*this))
return { false, nullptr };
return stopTraversal;
}

if (!passReference(MRE->getMember().getDecl(), MRE->getType(),
MRE->getNameLoc(),
ReferenceMetaData(SemaReferenceKind::DeclMemberRef,
OpAccess)))
return { false, nullptr };
return stopTraversal;

// We already visited the children.
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return doSkipChildren();

} else if (auto OtherCtorE = dyn_cast<OtherConstructorDeclRefExpr>(E)) {
if (!passReference(OtherCtorE->getDecl(), OtherCtorE->getType(),
OtherCtorE->getConstructorLoc(),
ReferenceMetaData(SemaReferenceKind::DeclConstructorRef,
OpAccess)))
return { false, nullptr };
return stopTraversal;

} else if (auto *SE = dyn_cast<SubscriptExpr>(E)) {
// Visit in source order.
if (!SE->getBase()->walk(*this))
return { false, nullptr };
return stopTraversal;

ValueDecl *SubscrD = nullptr;
if (SE->hasDecl())
Expand All @@ -359,21 +367,19 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {

if (SubscrD) {
if (!passSubscriptReference(SubscrD, E->getLoc(), data, true))
return { false, nullptr };
return stopTraversal;
}

if (!SE->getIndex()->walk(*this))
return { false, nullptr };
return stopTraversal;

if (SubscrD) {
if (!passSubscriptReference(SubscrD, E->getEndLoc(), data, false))
return { false, nullptr };
return stopTraversal;
}

// We already visited the children.
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return doSkipChildren();

} else if (auto *KPE = dyn_cast<KeyPathExpr>(E)) {
for (auto &component : KPE->getComponents()) {
Expand Down Expand Up @@ -406,60 +412,54 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
} else if (auto *BinE = dyn_cast<BinaryExpr>(E)) {
// Visit in source order.
if (!BinE->getArg()->getElement(0)->walk(*this))
return { false, nullptr };
return stopTraversal;
if (!BinE->getFn()->walk(*this))
return { false, nullptr };
return stopTraversal;
if (!BinE->getArg()->getElement(1)->walk(*this))
return { false, nullptr };
return stopTraversal;

// We already visited the children.
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return doSkipChildren();

} else if (auto TupleE = dyn_cast<TupleExpr>(E)) {
if (auto CallE = dyn_cast_or_null<CallExpr>(Parent.getAsExpr())) {
if (!passCallArgNames(CallE->getFn(), TupleE))
return { false, nullptr };
return stopTraversal;
}
} else if (auto IOE = dyn_cast<InOutExpr>(E)) {
llvm::SaveAndRestore<Optional<AccessKind>>
C(this->OpAccess, AccessKind::ReadWrite);

if (!IOE->getSubExpr()->walk(*this))
return { false, nullptr };
return stopTraversal;

// We already visited the children.
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return stopTraversal;
return skipChildren;
} else if (auto LE = dyn_cast<LoadExpr>(E)) {
llvm::SaveAndRestore<Optional<AccessKind>>
C(this->OpAccess, AccessKind::Read);

if (!LE->getSubExpr()->walk(*this))
return { false, nullptr };
return stopTraversal;

// We already visited the children.
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return doSkipChildren();
} else if (auto AE = dyn_cast<AssignExpr>(E)) {
{
llvm::SaveAndRestore<Optional<AccessKind>>
C(this->OpAccess, AccessKind::Write);

if (AE->getDest() && !AE->getDest()->walk(*this))
return { false, nullptr };
return stopTraversal;
}

if (AE->getSrc() && !AE->getSrc()->walk(*this))
return { false, nullptr };
return stopTraversal;

// We already visited the children.
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return doSkipChildren();
} else if (auto OEE = dyn_cast<OpenExistentialExpr>(E)) {
// Record opaque value.
OpaqueValueMap[OEE->getOpaqueValue()] = OEE->getExistentialValue();
Expand All @@ -468,44 +468,39 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
};

if (!OEE->getSubExpr()->walk(*this))
return { false, nullptr };
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return stopTraversal;

return doSkipChildren();
} else if (auto MTEE = dyn_cast<MakeTemporarilyEscapableExpr>(E)) {
// Manually walk to original arguments in order. We don't handle
// OpaqueValueExpr here.

// Original non-escaping closure.
if (!MTEE->getNonescapingClosureValue()->walk(*this))
return { false, nullptr };
return stopTraversal;

// Body, which is called by synthesized CallExpr.
auto *callExpr = cast<CallExpr>(MTEE->getSubExpr());
if (!callExpr->getFn()->walk(*this))
return { false, nullptr };
return stopTraversal;

if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return doSkipChildren();
} else if (auto CUCE = dyn_cast<CollectionUpcastConversionExpr>(E)) {
// Ignore conversion expressions. We don't handle OpaqueValueExpr here
// because it's only in conversion expressions. Instead, just walk into
// sub expression.
if (!CUCE->getSubExpr()->walk(*this))
return { false, nullptr };
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return stopTraversal;

return doSkipChildren();
} else if (auto OVE = dyn_cast<OpaqueValueExpr>(E)) {
// Walk into mapped value.
auto value = OpaqueValueMap.find(OVE);
if (value != OpaqueValueMap.end()) {
if (!value->second->walk(*this))
return { false, nullptr };
if (!walkToExprPost(E))
return { false, nullptr };
return { false, E };
return stopTraversal;

return doSkipChildren();
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/SIL/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,11 @@ bool SILDeclRef::isTransparent() const {
if (isStoredPropertyInitializer())
return true;

if (hasAutoClosureExpr())
return true;
if (hasAutoClosureExpr()) {
auto *ace = getAutoClosureExpr();
if (ace->getThunkKind() == AutoClosureExpr::Kind::None)
return true;
}

if (hasDecl()) {
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDecl()))
Expand Down
6 changes: 6 additions & 0 deletions lib/SIL/SILProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ static bool isUnmapped(ASTNode N) {
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: implicit closure expr\n");
return true;
}

if (isa<AutoClosureExpr>(CE) &&
cast<AutoClosureExpr>(CE)->getThunkKind() != AutoClosureExpr::Kind::None) {
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: curry thunk expr\n");
return true;
}
}

// Map all other kinds of expressions.
Expand Down
Loading