[Function builders] Infer function builder from a protocol requirement.

Allow a protocol requirement for a function or property to declare
a function builder. A witness to such a protocol requirement will
infer that function builder when all of the following are two:
* The witness does not explicitly adopt a function builder
* The witness is declared within the same context that conforms to the
protocol requirement (e.g., same extension or primary type declaration)
* The witness's body does not contain a "return" statement (since those
disable the function builder transform).
This commit is contained in:
Doug Gregor
2020-05-24 23:14:19 -07:00
parent 2d25850940
commit bf7eea08da
9 changed files with 313 additions and 19 deletions

View File

@@ -1401,10 +1401,6 @@ BraceStmt *swift::applyFunctionBuilderTransform(
captured.first, captured.second)));
}
/// Find the return statements in the given body, which block the application
/// of a function builder.
static std::vector<ReturnStmt *> findReturnStatements(AnyFunctionRef fn);
Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
FuncDecl *func, Type builderType) {
// Pre-check the body: pre-check any expressions in it and look
@@ -1708,7 +1704,7 @@ PreCheckFunctionBuilderRequest::evaluate(Evaluator &eval,
return PreCheckFunctionBuilderApplication(fn, false).run();
}
std::vector<ReturnStmt *> findReturnStatements(AnyFunctionRef fn) {
std::vector<ReturnStmt *> TypeChecker::findReturnStatements(AnyFunctionRef fn) {
PreCheckFunctionBuilderApplication precheck(fn, true);
(void)precheck.run();
return precheck.getReturnStmts();