Skip to content

[Sema] Descriptor for argument of PreCheckFunctionBuilderRequest #33013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,45 @@ class ValueWitnessRequest
void cacheResult(Witness value) const;
};

struct PreCheckFunctionBuilderDescriptor {
AnyFunctionRef Fn;

private:
// NOTE: Since source tooling (e.g. code completion) might replace the body,
// we need to take the body into account to calculate 'hash_value' and '=='.
// Also, we cannot 'getBody()' inside 'hash_value' and '==' because it invokes
// another request (even if it's cached).
BraceStmt *Body;

public:
PreCheckFunctionBuilderDescriptor(AnyFunctionRef Fn)
: Fn(Fn), Body(Fn.getBody()) {}

friend llvm::hash_code
hash_value(const PreCheckFunctionBuilderDescriptor &owner) {
return llvm::hash_combine(owner.Fn, owner.Body);
}

friend bool operator==(const PreCheckFunctionBuilderDescriptor &lhs,
const PreCheckFunctionBuilderDescriptor &rhs) {
return lhs.Fn == rhs.Fn && lhs.Body == rhs.Body;
}

friend bool operator!=(const PreCheckFunctionBuilderDescriptor &lhs,
const PreCheckFunctionBuilderDescriptor &rhs) {
return !(lhs == rhs);
}

friend SourceLoc extractNearestSourceLoc(PreCheckFunctionBuilderDescriptor d) {
return extractNearestSourceLoc(d.Fn);
}

friend void simple_display(llvm::raw_ostream &out,
const PreCheckFunctionBuilderDescriptor &d) {
simple_display(out, d.Fn);
}
};

enum class FunctionBuilderBodyPreCheck : uint8_t {
/// There were no problems pre-checking the closure.
Okay,
Expand All @@ -1836,8 +1875,8 @@ enum class FunctionBuilderBodyPreCheck : uint8_t {

class PreCheckFunctionBuilderRequest
: public SimpleRequest<PreCheckFunctionBuilderRequest,
FunctionBuilderBodyPreCheck(AnyFunctionRef,
BraceStmt *),
FunctionBuilderBodyPreCheck(
PreCheckFunctionBuilderDescriptor),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;
Expand All @@ -1846,8 +1885,8 @@ class PreCheckFunctionBuilderRequest
friend SimpleRequest;

// Evaluation.
FunctionBuilderBodyPreCheck evaluate(Evaluator &evaluator, AnyFunctionRef fn,
BraceStmt *body) const;
FunctionBuilderBodyPreCheck
evaluate(Evaluator &evaluator, PreCheckFunctionBuilderDescriptor owner) const;

public:
// Separate caching.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ SWIFT_REQUEST(TypeChecker, HasUserDefinedDesignatedInitRequest,
SWIFT_REQUEST(TypeChecker, HasMemberwiseInitRequest,
bool(StructDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, PreCheckFunctionBuilderRequest,
FunctionBuilderClosurePreCheck(AnyFunctionRef, BraceStmt *),
FunctionBuilderClosurePreCheck(PreCheckFunctionBuilderDescriptor),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
evaluator::SideEffect(NominalTypeDecl *, ImplicitMemberAction),
Expand Down
23 changes: 8 additions & 15 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
// If we encountered an error or there was an explicit result type,
// bail out and report that to the caller.
auto &ctx = func->getASTContext();
auto request = PreCheckFunctionBuilderRequest{func, func->getBody()};
auto request = PreCheckFunctionBuilderRequest{AnyFunctionRef(func)};
switch (evaluateOrDefault(
ctx.evaluator, request, FunctionBuilderBodyPreCheck::Error)) {
case FunctionBuilderBodyPreCheck::Okay:
Expand Down Expand Up @@ -1592,7 +1592,7 @@ ConstraintSystem::matchFunctionBuilder(

// Pre-check the body: pre-check any expressions in it and look
// for return statements.
auto request = PreCheckFunctionBuilderRequest{fn, fn.getBody()};
auto request = PreCheckFunctionBuilderRequest{fn};
switch (evaluateOrDefault(getASTContext().evaluator, request,
FunctionBuilderBodyPreCheck::Error)) {
case FunctionBuilderBodyPreCheck::Okay:
Expand Down Expand Up @@ -1712,11 +1712,11 @@ class PreCheckFunctionBuilderApplication : public ASTWalker {
if (HasError)
return FunctionBuilderBodyPreCheck::Error;

assert(oldBody == newBody && "pre-check walk wasn't in-place?");

if (hasReturnStmt())
return FunctionBuilderBodyPreCheck::HasReturnStmt;

assert(oldBody == newBody && "pre-check walk wasn't in-place?");

return FunctionBuilderBodyPreCheck::Okay;
}

Expand Down Expand Up @@ -1754,23 +1754,16 @@ class PreCheckFunctionBuilderApplication : public ASTWalker {

}

FunctionBuilderBodyPreCheck
PreCheckFunctionBuilderRequest::evaluate(Evaluator &eval, AnyFunctionRef fn,
BraceStmt *body) const {
// NOTE: 'body' is passed only for the request evaluater caching key.
// Since source tooling (e.g. code completion) might replace the body,
// the function alone is not sufficient for the key.
assert(fn.getBody() == body &&
"body must be the current body of the function");

FunctionBuilderBodyPreCheck PreCheckFunctionBuilderRequest::evaluate(
Evaluator &evaluator, PreCheckFunctionBuilderDescriptor owner) const {
// We don't want to do the precheck if it will already have happened in
// the enclosing expression.
bool skipPrecheck = false;
if (auto closure = dyn_cast_or_null<ClosureExpr>(
fn.getAbstractClosureExpr()))
owner.Fn.getAbstractClosureExpr()))
skipPrecheck = shouldTypeCheckInEnclosingExpression(closure);

return PreCheckFunctionBuilderApplication(fn, false).run();
return PreCheckFunctionBuilderApplication(owner.Fn, false).run();
}

std::vector<ReturnStmt *> TypeChecker::findReturnStatements(AnyFunctionRef fn) {
Expand Down