@@ -627,8 +627,9 @@ class BuilderClosureRewriter
627
627
const Solution &solution;
628
628
DeclContext *dc;
629
629
AppliedBuilderTransform builderTransform;
630
- std::function<Expr *(Expr *)> rewriteExpr;
631
- std::function<Expr *(Expr *, Type, ConstraintLocator *)> coerceToType;
630
+ std::function<
631
+ Optional<SolutionApplicationTarget> (SolutionApplicationTarget)>
632
+ rewriteTarget;
632
633
633
634
// / Retrieve the temporary variable that will be used to capture the
634
635
// / value of the given expression.
@@ -648,6 +649,17 @@ class BuilderClosureRewriter
648
649
return recorded;
649
650
}
650
651
652
+ // / Rewrite an expression without any particularly special context.
653
+ Expr *rewriteExpr (Expr *expr) {
654
+ auto result = rewriteTarget (
655
+ SolutionApplicationTarget (expr, dc, CTP_Unused, Type (),
656
+ /* isDiscarded=*/ false ));
657
+ if (result)
658
+ return result->getAsExpr ();
659
+
660
+ return nullptr ;
661
+ }
662
+
651
663
public:
652
664
// / Retrieve information about a captured statement.
653
665
std::pair<VarDecl *, llvm::TinyPtrVector<Expr *>>
@@ -673,19 +685,21 @@ class BuilderClosureRewriter
673
685
ASTNode initializeTarget (FunctionBuilderTarget target) {
674
686
assert (target.captured .second .size () == 1 );
675
687
auto capturedExpr = target.captured .second .front ();
676
- auto finalCapturedExpr = rewriteExpr (capturedExpr);
677
688
SourceLoc implicitLoc = capturedExpr->getEndLoc ();
678
689
switch (target.kind ) {
679
690
case FunctionBuilderTarget::ReturnValue: {
680
691
// Return the expression.
681
- ConstraintSystem &cs = solution.getConstraintSystem ();
682
692
Type bodyResultType =
683
693
solution.simplifyType (builderTransform.bodyResultType );
684
- finalCapturedExpr = coerceToType (
685
- finalCapturedExpr,
686
- bodyResultType,
687
- cs.getConstraintLocator (capturedExpr));
688
- return new (ctx) ReturnStmt (implicitLoc, finalCapturedExpr);
694
+
695
+ SolutionApplicationTarget returnTarget (
696
+ capturedExpr, dc, CTP_ReturnStmt, bodyResultType,
697
+ /* isDiscarded=*/ false );
698
+ Expr *resultExpr = nullptr ;
699
+ if (auto resultTarget = rewriteTarget (returnTarget))
700
+ resultExpr = resultTarget->getAsExpr ();
701
+
702
+ return new (ctx) ReturnStmt (implicitLoc, resultExpr);
689
703
}
690
704
691
705
case FunctionBuilderTarget::TemporaryVar: {
@@ -696,6 +710,7 @@ class BuilderClosureRewriter
696
710
declRef->setType (LValueType::get (temporaryVar->getType ()));
697
711
698
712
// Load the right-hand side if needed.
713
+ auto finalCapturedExpr = rewriteExpr (capturedExpr);
699
714
if (finalCapturedExpr->getType ()->hasLValueType ()) {
700
715
finalCapturedExpr =
701
716
TypeChecker::addImplicitLoadExpr (ctx, finalCapturedExpr);
@@ -734,12 +749,12 @@ class BuilderClosureRewriter
734
749
const Solution &solution,
735
750
DeclContext *dc,
736
751
const AppliedBuilderTransform &builderTransform,
737
- std::function<Expr *(Expr *)> rewriteExpr,
738
- std::function<Expr *(Expr *, Type, ConstraintLocator *)> coerceToType
752
+ std::function<
753
+ Optional<SolutionApplicationTarget> (SolutionApplicationTarget)>
754
+ rewriteTarget
739
755
) : ctx(solution.getConstraintSystem().getASTContext()),
740
756
solution (solution), dc(dc), builderTransform(builderTransform),
741
- rewriteExpr(rewriteExpr),
742
- coerceToType(coerceToType){ }
757
+ rewriteTarget(rewriteTarget) { }
743
758
744
759
Stmt *visitBraceStmt (BraceStmt *braceStmt, FunctionBuilderTarget target,
745
760
Optional<FunctionBuilderTarget> innerTarget = None) {
@@ -954,9 +969,10 @@ BraceStmt *swift::applyFunctionBuilderTransform(
954
969
AppliedBuilderTransform applied,
955
970
BraceStmt *body,
956
971
DeclContext *dc,
957
- std::function<Expr *(Expr *)> rewriteExpr,
958
- std::function<Expr *(Expr *, Type, ConstraintLocator *)> coerceToType) {
959
- BuilderClosureRewriter rewriter (solution, dc, applied, rewriteExpr, coerceToType);
972
+ std::function<
973
+ Optional<SolutionApplicationTarget> (SolutionApplicationTarget)>
974
+ rewriteTarget) {
975
+ BuilderClosureRewriter rewriter (solution, dc, applied, rewriteTarget);
960
976
auto captured = rewriter.takeCapturedStmt (body);
961
977
return cast<BraceStmt>(
962
978
rewriter.visitBraceStmt (
0 commit comments