@@ -874,30 +874,27 @@ class SyntacticElementConstraintGenerator
874
874
}
875
875
876
876
void visitReturnStmt (ReturnStmt *returnStmt) {
877
- auto *closure =
878
- dyn_cast_or_null<ClosureExpr>(context.getAbstractClosureExpr ());
879
-
880
877
// Single-expression closures are effectively a `return` statement,
881
878
// so let's give them a special locator as to indicate that.
882
879
// Return statements might not have a result if we have a closure whose
883
880
// implicit returned value is coerced to Void.
884
- if (closure && closure->hasSingleExpressionBody () &&
885
- returnStmt->hasResult ()) {
881
+ if (isInSingleExpressionClosure () && returnStmt->hasResult ()) {
886
882
auto *expr = returnStmt->getResult ();
887
883
assert (expr && " single expression closure without expression?" );
888
884
889
- expr = cs.generateConstraints (expr, closure ,
885
+ expr = cs.generateConstraints (expr, context. getAsDeclContext () ,
890
886
/* isInputExpression=*/ false );
891
887
if (!expr) {
892
888
hadError = true ;
893
889
return ;
894
890
}
895
891
896
- cs.addConstraint (
897
- ConstraintKind::Conversion, cs.getType (expr), resultType,
898
- cs.getConstraintLocator (
899
- closure, LocatorPathElt::ClosureBody (
900
- /* hasReturn=*/ !returnStmt->isImplicit ())));
892
+ cs.addConstraint (ConstraintKind::Conversion, cs.getType (expr),
893
+ getContextualResultType (),
894
+ cs.getConstraintLocator (
895
+ context.getAbstractClosureExpr (),
896
+ LocatorPathElt::ClosureBody (
897
+ /* hasReturn=*/ !returnStmt->isImplicit ())));
901
898
return ;
902
899
}
903
900
@@ -921,14 +918,40 @@ class SyntacticElementConstraintGenerator
921
918
return ;
922
919
}
923
920
924
- cs.setContextualType (target.getAsExpr (), TypeLoc::withoutLoc (resultType),
921
+ cs.setContextualType (target.getAsExpr (),
922
+ TypeLoc::withoutLoc (getContextualResultType ()),
925
923
CTP_ReturnStmt);
926
924
cs.setSolutionApplicationTarget (returnStmt, target);
927
925
}
928
926
927
+ bool isInSingleExpressionClosure () {
928
+ if (!isExpr<ClosureExpr>(context.getAbstractClosureExpr ()))
929
+ return false ;
930
+
931
+ // Result builder transformed bodies are never single-expression.
932
+ if (cs.getAppliedResultBuilderTransform (context))
933
+ return false ;
934
+
935
+ return context.hasSingleExpressionBody ();
936
+ }
937
+
938
+ Type getContextualResultType () const {
939
+ if (auto transform = cs.getAppliedResultBuilderTransform (context))
940
+ return transform->bodyResultType ;
941
+
942
+ if (auto *closure =
943
+ getAsExpr<ClosureExpr>(context.getAbstractClosureExpr ()))
944
+ return cs.getClosureType (closure)->getResult ();
945
+
946
+ return context.getBodyResultType ();
947
+ }
948
+
929
949
bool isSupportedMultiStatementClosure () const {
950
+ if (cs.getAppliedResultBuilderTransform (context))
951
+ return true ;
952
+
930
953
if (auto *closure =
931
- dyn_cast_or_null <ClosureExpr>(context.getAbstractClosureExpr ())) {
954
+ getAsExpr <ClosureExpr>(context.getAbstractClosureExpr ())) {
932
955
return !closure->hasSingleExpressionBody () &&
933
956
cs.participatesInInference (closure);
934
957
}
0 commit comments