Skip to content

[Sema] NFC: Simplify type coercion of ClosureExprs #12468

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
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
24 changes: 8 additions & 16 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6317,24 +6317,16 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
// Coercion from one function type to another, this produces a
// FunctionConversionExpr in its full generality.
if (auto fromFunc = fromType->getAs<FunctionType>()) {
// If toType is a NoEscape or NoReturn function type and the expression is
// a ClosureExpr, propagate these bits onto the ClosureExpr. Do not
// *remove* any bits that are already on the closure though.
// Note that in this case, we do not want to propagate the 'throws' bit
// to the closure type, as the closure has already been analyzed for
// throwing subexpressions. We also don't want to change the convention
// of the original closure.
// If we have a ClosureExpr, then we can safely propagate the 'no escape'
// bit to the closure without invalidating prior analysis.
auto fromEI = fromFunc->getExtInfo(), toEI = toFunc->getExtInfo();
if (toEI.isNoEscape() && !fromEI.isNoEscape()) {
swift::AnyFunctionType::ExtInfo newEI(fromEI.getRepresentation(),
toEI.isAutoClosure(),
toEI.isNoEscape() | fromEI.isNoEscape(),
toEI.throws() & fromEI.throws());
Copy link
Member

Choose a reason for hiding this comment

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

This is such a weird computation. We take the "autoclosure" bit from the "to" type, "noescape" from either type (even though it's only on the "to" type based on the "if" statement above) and "throws" from both types (which makes no sense).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. Don't forget that this goofiness only happened when the no escape bit and only the no escape bit needed propagating. Thanks for the review!

auto newToType = FunctionType::get(fromFunc->getInput(),
fromFunc->getResult(), newEI);
if (applyTypeToClosureExpr(cs, expr, newToType)) {
fromFunc = newToType;
// Propagating the bits in might have satisfied the entire
auto newFromFunc = FunctionType::get(fromFunc->getInput(),
fromFunc->getResult(),
fromEI.withNoEscape());
if (applyTypeToClosureExpr(cs, expr, newFromFunc)) {
fromFunc = newFromFunc;
// Propagating the 'no escape' bit might have satisfied the entire
// conversion. If so, we're done, otherwise keep converting.
if (fromFunc->isEqual(toType))
return expr;
Expand Down