Skip to content

[CSApply] NFC: Clean up some duplicated logic for propagating async from contextual types to closure types. #37282

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
May 12, 2021
Merged
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
44 changes: 15 additions & 29 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6120,13 +6120,14 @@ static bool isClosureLiteralExpr(Expr *expr) {
return (isa<CaptureListExpr>(expr) || isa<ClosureExpr>(expr));
}

/// Whether we should propagate async down to a closure.
static bool shouldPropagateAsyncToClosure(Expr *expr) {
/// Whether the given expression is a closure that should inherit
/// the actor context from where it was formed.
static bool closureInheritsActorContext(Expr *expr) {
if (auto IE = dyn_cast<IdentityExpr>(expr))
return shouldPropagateAsyncToClosure(IE->getSubExpr());
return closureInheritsActorContext(IE->getSubExpr());

if (auto CLE = dyn_cast<CaptureListExpr>(expr))
return shouldPropagateAsyncToClosure(CLE->getClosureBody());
return closureInheritsActorContext(CLE->getClosureBody());

if (auto CE = dyn_cast<ClosureExpr>(expr))
return CE->inheritsActorContext();
Expand Down Expand Up @@ -7012,22 +7013,6 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}
}

// If we have a ClosureExpr, then we can safely propagate the 'async'
// bit to the closure without invalidating prior analysis.
fromEI = fromFunc->getExtInfo();
if (toEI.isAsync() && !fromEI.isAsync() &&
shouldPropagateAsyncToClosure(expr)) {
auto newFromFuncType = fromFunc->withExtInfo(fromEI.withAsync());
if (applyTypeToClosureExpr(cs, expr, newFromFuncType)) {
fromFunc = newFromFuncType->castTo<FunctionType>();

// Propagating the 'concurrent' bit might have satisfied the entire
// conversion. If so, we're done, otherwise keep converting.
if (fromFunc->isEqual(toType))
return expr;
}
}

// If we have a ClosureExpr, then we can safely propagate a global actor
// to the closure without invalidating prior analysis.
fromEI = fromFunc->getExtInfo();
Expand All @@ -7044,27 +7029,28 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}
}

/// Whether the given effect should be propagated to a closure expression.
auto shouldApplyEffect = [&](EffectKind kind) -> bool {
/// Whether the given effect is polymorphic at this location.
auto isEffectPolymorphic = [&](EffectKind kind) -> bool {
auto last = locator.last();
if (!(last && last->is<LocatorPathElt::ApplyArgToParam>()))
return true;
return false;

// The effect should not be applied if the closure is an argument
// to a function where that effect is polymorphic.
if (auto *call = getAsExpr<ApplyExpr>(locator.getAnchor())) {
if (auto *declRef = dyn_cast<DeclRefExpr>(call->getFn())) {
if (auto *fn = dyn_cast<AbstractFunctionDecl>(declRef->getDecl()))
return !fn->hasPolymorphicEffect(kind);
return fn->hasPolymorphicEffect(kind);
Copy link
Contributor

Choose a reason for hiding this comment

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

IHMO naming of this method could be improved it's clear what polymorphism has to do with this and there is no comment at the declaration site. @hborla told me that this is about interaction between reasync/rethrows and async/throws attributes.

}
}

return true;
return false;
};

// If we have a ClosureExpr, we can safely propagate 'async' to the closure.
// If we have a ClosureExpr, and we can safely propagate 'async' to the
// closure, do that here.
fromEI = fromFunc->getExtInfo();
if (toEI.isAsync() && !fromEI.isAsync() && shouldApplyEffect(EffectKind::Async)) {
bool shouldPropagateAsync =
!isEffectPolymorphic(EffectKind::Async) || closureInheritsActorContext(expr);
if (toEI.isAsync() && !fromEI.isAsync() && shouldPropagateAsync) {
auto newFromFuncType = fromFunc->withExtInfo(fromEI.withAsync());
if (applyTypeToClosureExpr(cs, expr, newFromFuncType)) {
fromFunc = newFromFuncType->castTo<FunctionType>();
Expand Down