Skip to content

Sema: Propagate noescape bit into closure passed to withoutActuallyEscaping [4.2 6/11] #17166

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
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
8 changes: 7 additions & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6085,6 +6085,12 @@ static bool applyTypeToClosureExpr(ConstraintSystem &cs,
// If we found an explicit ClosureExpr, update its type.
if (auto CE = dyn_cast<ClosureExpr>(expr)) {
cs.setType(CE, toType);

// If this is not a single-expression closure, write the type into the
// ClosureExpr directly here, since the visitor won't.
if (!CE->hasSingleExpressionBody())
CE->setType(toType);

return true;
}

Expand Down Expand Up @@ -7427,7 +7433,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
}

case DeclTypeCheckingSemantics::WithoutActuallyEscaping: {
// Resolve into a MakeTemporarilyEscapingExpr.
// Resolve into a MakeTemporarilyEscapableExpr.
auto arg = cast<TupleExpr>(apply->getArg());
assert(arg->getNumElements() == 2 && "should have two arguments");
auto nonescaping = arg->getElements()[0];
Expand Down
26 changes: 26 additions & 0 deletions test/Constraints/without_actually_escaping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,29 @@ func rethrowThroughWAE(_ zz: (Int, Int, Int) throws -> Int, _ value: Int) throws
let _: ((Int) -> Int, (@escaping (Int) -> Int) -> ()) -> ()
= withoutActuallyEscaping(_:do:) // expected-error{{}}


// Failing to propagate @noescape into non-single-expression
// closure passed to withoutActuallyEscaping

// https://bugs.swift.org/browse/SR-7886

class Box<T> {
let value: T

init(_ value: T) {
self.value = value
}

func map1<U>(_ transform: (T) -> U) -> Box<U> {
return withoutActuallyEscaping(transform) { transform in
return Box<U>(transform(value))
}
}

func map2<U>(_ transform: (T) -> U) -> Box<U> {
return withoutActuallyEscaping(transform) { transform in
let v = Box<U>(transform(value))
return v
}
}
}