Skip to content

[ResultBuilder] AST transform: don't try type erasure of result expressions #60449

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 2 commits into from
Aug 9, 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
9 changes: 1 addition & 8 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,6 @@ class ResultBuilderTransform

using UnsupportedElt = SkipUnhandledConstructInResultBuilder::UnhandledNode;

/// The constraint system this transform is associated with.
ConstraintSystem &CS;
/// The result type of this result builder body.
Type ResultType;

Expand All @@ -930,8 +928,7 @@ class ResultBuilderTransform
public:
ResultBuilderTransform(ConstraintSystem &cs, DeclContext *dc,
Type builderType, Type resultTy)
: BuilderTransformerBase(&cs, dc, builderType), CS(cs),
ResultType(resultTy) {}
: BuilderTransformerBase(&cs, dc, builderType), ResultType(resultTy) {}

UnsupportedElt getUnsupportedElement() const { return FirstUnsupported; }

Expand Down Expand Up @@ -1180,10 +1177,6 @@ class ResultBuilderTransform
{buildBlockResult}, {Identifier()});
}

// Type erase return if the result type requires it.
buildBlockResult = CS.buildTypeErasedExpr(buildBlockResult, dc,
ResultType, CTP_ReturnStmt);

elements.push_back(new (ctx) ReturnStmt(resultLoc, buildBlockResult,
/*Implicit=*/true));
}
Expand Down
27 changes: 19 additions & 8 deletions lib/Sema/CSClosure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,8 @@ class SyntacticElementSolutionApplication
}

ASTNode visitReturnStmt(ReturnStmt *returnStmt) {
auto &cs = solution.getConstraintSystem();

if (!returnStmt->hasResult()) {
// If contextual is not optional, there is nothing to do here.
if (resultType->isVoid())
Expand All @@ -1598,8 +1600,6 @@ class SyntacticElementSolutionApplication
assert(resultType->getOptionalObjectType() &&
resultType->lookThroughAllOptionalTypes()->isVoid());

auto &cs = solution.getConstraintSystem();

auto target = *cs.getSolutionApplicationTarget(returnStmt);
returnStmt->setResult(target.getAsExpr());
}
Expand Down Expand Up @@ -1629,13 +1629,24 @@ class SyntacticElementSolutionApplication
mode = convertToResult;
}

SolutionApplicationTarget resultTarget(
resultExpr, context.getAsDeclContext(),
mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
mode == convertToResult ? resultType : Type(),
/*isDiscarded=*/false);
if (auto newResultTarget = rewriteTarget(resultTarget))
Optional<SolutionApplicationTarget> resultTarget;
if (auto target = cs.getSolutionApplicationTarget(returnStmt)) {
resultTarget = *target;
} else {
// Single-expression closures have to handle returns in a special
// way so the target has to be created for them during solution
// application based on the resolved type.
assert(isSingleExpression);
resultTarget = SolutionApplicationTarget(
resultExpr, context.getAsDeclContext(),
mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
mode == convertToResult ? resultType : Type(),
/*isDiscarded=*/false);
}

if (auto newResultTarget = rewriteTarget(*resultTarget)) {
resultExpr = newResultTarget->getAsExpr();
}

switch (mode) {
case convertToResult:
Expand Down