Skip to content
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
12 changes: 4 additions & 8 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,14 +2159,10 @@ class SyntacticElementSolutionApplication
if (resultType->isVoid())
return returnStmt;

// It's possible to infer e.g. `Void?` for cases where
// `return` doesn't have an expression. If contextual
// type is `Void` wrapped into N optional types, let's
// add an implicit `()` expression and let it be injected
// into optional required number of times.

assert(resultType->getOptionalObjectType() &&
resultType->lookThroughAllOptionalTypes()->isVoid());
// Constraint generation injects an implicit `()` expresion
// into return statements without one. This helps to match
// cases like `Void` and `Any` that could be wrapped into a
// number of optional types.

auto target = *cs.getTargetFor(returnStmt);
returnStmt->setResult(target.getAsExpr());
Expand Down
23 changes: 23 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1392,3 +1392,26 @@ func test_generic_closure_parameter_requirement_failure<Item: Idable>(
Container(data: { (input: TestInput) in payload(input.value) })
// expected-error@-1 {{generic struct 'TestInput' requires that 'Item.ID' conform to 'Collection'}}
}

// Since implicit result implies `()` it should be allowed to be converted to e.g. `Void` and `Any`
func test_implicit_result_conversions() {
func test_optional(_ x: Int) {
let _: Any? = {
switch x {
case 0:
return 1
default:
return
}
}()
}

func testAny(_: () -> Any) {}

testAny { } // Ok
testAny { return } // Ok
testAny {
_ = 42
return // Ok
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// {"kind":"typecheck","signature":"(anonymous namespace)::SyntacticElementSolutionApplication::visitReturnStmt(swift::ReturnStmt*)"}
// RUN: not --crash %target-swift-frontend -typecheck %s
// RUN: not %target-swift-frontend -typecheck %s
enum a func b(c : a) {
let:
()->Copyable = { c return