@@ -918,13 +918,9 @@ std::optional<BraceStmt *>
918918TypeChecker::applyResultBuilderBodyTransform (FuncDecl *func, Type builderType) {
919919 // First look for any return statements, and bail if we have any.
920920 auto &ctx = func->getASTContext ();
921- if (evaluateOrDefault (ctx.evaluator , BraceHasReturnRequest{func->getBody ()},
922- false )) {
921+ if (auto returnStmts = findReturnStatements (func); !returnStmts.empty ()) {
923922 // One or more explicit 'return' statements were encountered, which
924923 // disables the result builder transform. Warn when we do this.
925- auto returnStmts = findReturnStatements (func);
926- assert (!returnStmts.empty ());
927-
928924 ctx.Diags .diagnose (
929925 returnStmts.front ()->getReturnLoc (),
930926 diag::result_builder_disabled_by_return_warn, builderType);
@@ -1126,8 +1122,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11261122 // not apply the result builder transform if it contained an explicit return.
11271123 // To maintain source compatibility, we still need to check for HasReturnStmt.
11281124 // https://github.com/apple/swift/issues/64332.
1129- if (evaluateOrDefault (getASTContext ().evaluator ,
1130- BraceHasReturnRequest{fn.getBody ()}, false )) {
1125+ if (fn.bodyHasExplicitReturnStmt ()) {
11311126 // Diagnostic mode means that solver couldn't reach any viable
11321127 // solution, so let's diagnose presence of a `return` statement
11331128 // in the closure body.
@@ -1276,7 +1271,21 @@ bool BraceHasReturnRequest::evaluate(Evaluator &evaluator,
12761271 return !ReturnStmtFinder::find (BS).empty ();
12771272}
12781273
1274+ bool AnyFunctionRef::bodyHasExplicitReturnStmt () const {
1275+ auto *body = getBody ();
1276+ if (!body) {
1277+ return false ;
1278+ }
1279+
1280+ auto &ctx = getAsDeclContext ()->getASTContext ();
1281+ return evaluateOrDefault (ctx.evaluator , BraceHasReturnRequest{body}, false );
1282+ }
1283+
12791284std::vector<ReturnStmt *> TypeChecker::findReturnStatements (AnyFunctionRef fn) {
1285+ if (!fn.bodyHasExplicitReturnStmt ()) {
1286+ return std::vector<ReturnStmt *>();
1287+ }
1288+
12801289 return ReturnStmtFinder::find (fn.getBody ());
12811290}
12821291
0 commit comments