Skip to content

[ConstraintSystem] Don't bind result type of an empty closure too early #30838

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 1 commit into from
Apr 7, 2020
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
7 changes: 1 addition & 6 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,6 @@ namespace {
closure->getExplicitResultTypeLoc().getType()) {
resultTy = closure->getExplicitResultTypeLoc().getType();
} else {
auto &ctx = CS.getASTContext();
auto *resultLoc =
CS.getConstraintLocator(closure, ConstraintLocator::ClosureResult);

Expand All @@ -2196,11 +2195,7 @@ namespace {
return Type();
};

if (closure->hasEmptyBody()) {
// Closures with empty bodies should be inferred to return
// ().
resultTy = ctx.TheEmptyTupleType;
} else if (auto contextualResultTy = getContextualResultType()) {
if (auto contextualResultTy = getContextualResultType()) {
resultTy = contextualResultTy;
} else {
// If no return type was specified, create a fresh type
Expand Down
18 changes: 12 additions & 6 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7048,7 +7048,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
}

bool hasReturn = hasExplicitResult(closure);

auto &ctx = getASTContext();
// If this is a multi-statement closure its body doesn't participate
// in type-checking.
if (closure->hasSingleExpressionBody()) {
Expand All @@ -7061,12 +7061,18 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
closureType->getResult(),
getConstraintLocator(closure, LocatorPathElt::ClosureBody(hasReturn)));
} else if (!hasReturn) {
// If multi-statement closure doesn't have an explicit result
// (no `return` statements) let's default it to `Void`.
auto &ctx = getASTContext();
bool hasExplicitResult = closure->hasExplicitResultType() &&
closure->getExplicitResultTypeLoc().getType();

// If this closure has an empty body and no explicit result type
// let's bind result type to `Void` since that's the only type empty body
// can produce. Otherwise, if (multi-statement) closure doesn't have
// an explicit result (no `return` statements) let's default it to `Void`.
auto constraintKind = (closure->hasEmptyBody() && !hasExplicitResult)
? ConstraintKind::Bind
: ConstraintKind::Defaultable;
addConstraint(
ConstraintKind::Defaultable, inferredClosureType->getResult(),
ctx.TheEmptyTupleType,
constraintKind, inferredClosureType->getResult(), ctx.TheEmptyTupleType,
getConstraintLocator(closure, ConstraintLocator::ClosureResult));
}

Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ func test() -> Int? {
}

var fn: () -> [Int] = {}
// expected-error@-1 {{cannot convert value of type '()' to closure result type '[Int]'}}
// expected-error@-1 {{cannot convert value of type '[Int]' to closure result type '()'}}

fn = {}
// expected-error@-1 {{cannot assign value of type '() -> ()' to type '() -> [Int]'}}
Expand Down
18 changes: 18 additions & 0 deletions test/Constraints/function_builder_diags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,21 @@ func testWrapperBuilder() {

let _: Int = x // expected-error{{cannot convert value of type 'Wrapper<(Double, String)>' to specified type 'Int'}}
}

// rdar://problem/61347993 - empty function builder doesn't compile
func rdar61347993() {
struct Result {}

@_functionBuilder
struct Builder {
static func buildBlock() -> Result {
Result()
}
}

func test_builder<T>(@Builder _: () -> T) {}
test_builder {} // Ok

func test_closure(_: () -> Result) {}
test_closure {} // expected-error {{cannot convert value of type '()' to closure result type 'Result'}}
}