[Diagnostics] Diagnose use of one or more return statements in a result builder body

This commit is contained in:
Pavel Yaskevich
2020-11-18 11:30:36 -08:00
parent da8159c1b4
commit 03ede7427c
3 changed files with 41 additions and 1 deletions

View File

@@ -7052,3 +7052,22 @@ bool ReferenceToInvalidDeclaration::diagnoseAsError() {
emitDiagnosticAt(decl, diag::decl_declared_here, decl->getName());
return true;
}
bool InvalidReturnInResultBuilderBody::diagnoseAsError() {
auto *closure = castToExpr<ClosureExpr>(getAnchor());
auto returnStmts = TypeChecker::findReturnStatements(closure);
assert(!returnStmts.empty());
auto loc = returnStmts.front()->getReturnLoc();
emitDiagnosticAt(loc, diag::result_builder_disabled_by_return, BuilderType);
// Note that one can remove all of the return statements.
{
auto diag = emitDiagnosticAt(loc, diag::result_builder_remove_returns);
for (auto returnStmt : returnStmts)
diag.fixItRemove(returnStmt->getReturnLoc());
}
return true;
}