Skip to content

Commit 2ebc7a2

Browse files
authored
Merge pull request #60449 from xedin/dont-erase-result-during-transform
[ResultBuilder] AST transform: don't try type erasure of result expressions
2 parents ad26df0 + 422dbe4 commit 2ebc7a2

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,6 @@ class ResultBuilderTransform
919919

920920
using UnsupportedElt = SkipUnhandledConstructInResultBuilder::UnhandledNode;
921921

922-
/// The constraint system this transform is associated with.
923-
ConstraintSystem &CS;
924922
/// The result type of this result builder body.
925923
Type ResultType;
926924

@@ -930,8 +928,7 @@ class ResultBuilderTransform
930928
public:
931929
ResultBuilderTransform(ConstraintSystem &cs, DeclContext *dc,
932930
Type builderType, Type resultTy)
933-
: BuilderTransformerBase(&cs, dc, builderType), CS(cs),
934-
ResultType(resultTy) {}
931+
: BuilderTransformerBase(&cs, dc, builderType), ResultType(resultTy) {}
935932

936933
UnsupportedElt getUnsupportedElement() const { return FirstUnsupported; }
937934

@@ -1180,10 +1177,6 @@ class ResultBuilderTransform
11801177
{buildBlockResult}, {Identifier()});
11811178
}
11821179

1183-
// Type erase return if the result type requires it.
1184-
buildBlockResult = CS.buildTypeErasedExpr(buildBlockResult, dc,
1185-
ResultType, CTP_ReturnStmt);
1186-
11871180
elements.push_back(new (ctx) ReturnStmt(resultLoc, buildBlockResult,
11881181
/*Implicit=*/true));
11891182
}

lib/Sema/CSClosure.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,8 @@ class SyntacticElementSolutionApplication
16071607
}
16081608

16091609
ASTNode visitReturnStmt(ReturnStmt *returnStmt) {
1610+
auto &cs = solution.getConstraintSystem();
1611+
16101612
if (!returnStmt->hasResult()) {
16111613
// If contextual is not optional, there is nothing to do here.
16121614
if (resultType->isVoid())
@@ -1621,8 +1623,6 @@ class SyntacticElementSolutionApplication
16211623
assert(resultType->getOptionalObjectType() &&
16221624
resultType->lookThroughAllOptionalTypes()->isVoid());
16231625

1624-
auto &cs = solution.getConstraintSystem();
1625-
16261626
auto target = *cs.getSolutionApplicationTarget(returnStmt);
16271627
returnStmt->setResult(target.getAsExpr());
16281628
}
@@ -1652,13 +1652,24 @@ class SyntacticElementSolutionApplication
16521652
mode = convertToResult;
16531653
}
16541654

1655-
SolutionApplicationTarget resultTarget(
1656-
resultExpr, context.getAsDeclContext(),
1657-
mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1658-
mode == convertToResult ? resultType : Type(),
1659-
/*isDiscarded=*/false);
1660-
if (auto newResultTarget = rewriteTarget(resultTarget))
1655+
Optional<SolutionApplicationTarget> resultTarget;
1656+
if (auto target = cs.getSolutionApplicationTarget(returnStmt)) {
1657+
resultTarget = *target;
1658+
} else {
1659+
// Single-expression closures have to handle returns in a special
1660+
// way so the target has to be created for them during solution
1661+
// application based on the resolved type.
1662+
assert(isSingleExpression);
1663+
resultTarget = SolutionApplicationTarget(
1664+
resultExpr, context.getAsDeclContext(),
1665+
mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1666+
mode == convertToResult ? resultType : Type(),
1667+
/*isDiscarded=*/false);
1668+
}
1669+
1670+
if (auto newResultTarget = rewriteTarget(*resultTarget)) {
16611671
resultExpr = newResultTarget->getAsExpr();
1672+
}
16621673

16631674
switch (mode) {
16641675
case convertToResult:

0 commit comments

Comments
 (0)