Skip to content

[CSApply] For-in: Extend try injector to handle erasure and opened … #61920

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
Nov 4, 2022
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
53 changes: 39 additions & 14 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8929,21 +8929,46 @@ static Optional<SolutionApplicationTarget> applySolutionToForEachStmt(

Expr *nextCall = rewrittenTarget->getAsExpr();
// Wrap a call to `next()` into `try await` since `AsyncIteratorProtocol`
// requirement is `async throws`
// witness could be `async throws`.
if (isAsync) {
auto &ctx = cs.getASTContext();
auto nextRefType =
solution
.getResolvedType(
cast<ApplyExpr>(cast<AwaitExpr>(nextCall)->getSubExpr())
->getFn())
->castTo<FunctionType>();

// If the inferred witness is throwing, we need to wrap the call
// into `try` expression.
if (nextRefType->isThrowing())
nextCall = TryExpr::createImplicit(ctx, /*tryLoc=*/SourceLoc(),
nextCall, nextCall->getType());
// Cannot use `forEachChildExpr` here because we need to
// to wrap a call in `try` and then stop immediately after.
struct TryInjector : ASTWalker {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of injecting 'try' can we just teach the effect checker to do the right thing with ForEachStmt? Seems like a lot of extra work just to appease it. SILGen ignores TryExpr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe @etcwilde has some ideas how to do that but the last time I tried it couldn't make it happen due the fact that this code has been synthesized.

ASTContext &C;
const Solution &S;

bool ShouldStop = false;

TryInjector(ASTContext &ctx, const Solution &solution)
: C(ctx), S(solution) {}

PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (ShouldStop)
return Action::Stop();

if (auto *call = dyn_cast<CallExpr>(E)) {
// There is a single call expression in `nextCall`.
ShouldStop = true;

auto nextRefType =
S.getResolvedType(call->getFn())->castTo<FunctionType>();

// If the inferred witness is throwing, we need to wrap the call
// into `try` expression.
if (nextRefType->isThrowing()) {
auto *tryExpr = TryExpr::createImplicit(
C, /*tryLoc=*/call->getStartLoc(), call, call->getType());
// Cannot stop here because we need to make sure that
// the new expression gets injected into AST.
return Action::SkipChildren(tryExpr);
}
}

return Action::Continue(E);
}
};

nextCall->walk(TryInjector(cs.getASTContext(), solution));
}

stmt->setNextCall(nextCall);
Expand Down
10 changes: 10 additions & 0 deletions test/Concurrency/async_sequence_syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ func forAwaitWithConcreteType(_ seq: ThrowingAsyncSequence) throws { // expected
_ = elt
}
}

@available(SwiftStdlib 5.1, *)
func forTryAwaitReturningExistentialType() async throws {
struct S {
func seq() -> any AsyncSequence { fatalError() }
}

for try await _ in S().seq() { // Ok
}
}