Skip to content

Sema: Contextualize closures for function builder bodies also #30757

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
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
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ Expr *DefaultArgumentExprRequest::evaluate(Evaluator &evaluator,

// Walk the checked initializer and contextualize any closures
// we saw there.
(void)TypeChecker::contextualizeInitializer(dc, initExpr);
TypeChecker::contextualizeInitializer(dc, initExpr);
return initExpr;
}

Expand Down Expand Up @@ -1582,7 +1582,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (initContext) {
// Check safety of error-handling in the declaration, too.
TypeChecker::checkInitializerErrorHandling(initContext, init);
(void)TypeChecker::contextualizeInitializer(initContext, init);
TypeChecker::contextualizeInitializer(initContext, init);
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ namespace {
unsigned nextDiscriminator = 0)
: ParentDC(parent), NextDiscriminator(nextDiscriminator) {}

bool hasAutoClosures() const {
return NextDiscriminator != 0;
}

std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
// Autoclosures need to be numbered and potentially reparented.
// Reparenting is required with:
Expand Down Expand Up @@ -228,14 +224,9 @@ namespace {
};
} // end anonymous namespace

static void setAutoClosureDiscriminators(DeclContext *DC, Stmt *S) {
S->walk(ContextualizeClosures(DC));
}

bool TypeChecker::contextualizeInitializer(Initializer *DC, Expr *E) {
void TypeChecker::contextualizeInitializer(Initializer *DC, Expr *E) {
ContextualizeClosures CC(DC);
E->walk(CC);
return CC.hasAutoClosures();
}

void TypeChecker::contextualizeTopLevelCode(TopLevelCodeDecl *TLCD) {
Expand Down Expand Up @@ -422,7 +413,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
/// Type-check an entire function body.
bool typeCheckBody(BraceStmt *&S) {
bool HadError = typeCheckStmt(S);
setAutoClosureDiscriminators(DC, S);
S->walk(ContextualizeClosures(DC));
return HadError;
}

Expand Down Expand Up @@ -1920,6 +1911,8 @@ TypeCheckFunctionBodyUntilRequest::evaluate(Evaluator &evaluator,

body = *optBody;
alreadyTypeChecked = true;

body->walk(ContextualizeClosures(AFD));
}
} else if (func->hasSingleExpressionBody() &&
func->getResultInterfaceType()->isVoid()) {
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,7 @@ void checkPatternBindingCaptures(IterableDeclContext *DC);

/// Change the context of closures in the given initializer
/// expression to the given context.
///
/// \returns true if any closures were found
bool contextualizeInitializer(Initializer *DC, Expr *init);
void contextualizeInitializer(Initializer *DC, Expr *init);
void contextualizeTopLevelCode(TopLevelCodeDecl *TLCD);

/// Retrieve the default type for the given protocol.
Expand Down
33 changes: 33 additions & 0 deletions test/SILGen/function_builder_curry_thunks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-swift-emit-silgen %s | %FileCheck %s

@_functionBuilder
struct Builder {
static func buildBlock<T1>(_ t1: T1) -> (T1) {
return (t1)
}
}

struct Handler {
var firstHandler: () -> ()
var secondHandler: () -> ()
}

// We were neglecting to assign discriminators and re-parent
// autoclosures, which would manifest as curried method references
// producing a bogus diagnostic about captures from inside a
// nested type.
class Outer {
struct Inner {
@Builder
var build: Handler {
Handler(firstHandler: self.handler, secondHandler: self.handler)
}

private func handler() {}
}
}

// CHECK-LABEL: sil private [ossa] @$s29function_builder_curry_thunks5OuterC5InnerV5buildAA7HandlerVvgyycAEcfu_ : $@convention(thin) (Outer.Inner) -> @owned @callee_guaranteed () -> ()
// CHECK-LABEL: sil private [ossa] @$s29function_builder_curry_thunks5OuterC5InnerV5buildAA7HandlerVvgyycAEcfu_yycfu0_ : $@convention(thin) (Outer.Inner) -> ()
// CHECK-LABEL: sil private [ossa] @$s29function_builder_curry_thunks5OuterC5InnerV7handler33_DC254A3F89F9C7E65D25434E199F17A4LLyyF : $@convention(method) (Outer.Inner) -> ()
// CHECK-LABEL: sil private [ossa] @$s29function_builder_curry_thunks5OuterC5InnerV5buildAA7HandlerVvgyycAEcfu1_ : $@convention(thin) (Outer.Inner) -> @owned @callee_guaranteed () -> ()