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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ ERROR(cannot_infer_closure_parameter_type,none,
(StringRef))
ERROR(cannot_infer_closure_type,none,
"unable to infer closure type in the current context", ())
ERROR(cannot_infer_empty_closure_result_type,none,
"cannot infer return type of empty closure", ())
ERROR(cannot_infer_closure_result_type,none,
"cannot infer return type for closure with multiple statements; "
"add explicit type to disambiguate", ())
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,10 @@ class SpecifyClosureReturnType final : public ConstraintFix {

bool diagnose(const Solution &solution, bool asNote = false) const override;

bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
return diagnose(*commonFixes.front().first);
}

static SpecifyClosureReturnType *create(ConstraintSystem &cs,
ConstraintLocator *locator);

Expand Down
16 changes: 15 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7054,6 +7054,20 @@ bool UnableToInferClosureParameterType::diagnoseAsError() {
bool UnableToInferClosureReturnType::diagnoseAsError() {
auto *closure = castToExpr<ClosureExpr>(getRawAnchor());

auto *body = closure->getBody();
// For empty closures, let's produce a tailored message and suggest
// adding an expression to the body.
if (body->empty()) {
auto diagnostic =
emitDiagnostic(diag::cannot_infer_empty_closure_result_type);

diagnostic.fixItInsertAfter(closure->getInLoc().isValid()
? closure->getInLoc()
: body->getLBraceLoc(),
"<#result#>");
return true;
}

auto diagnostic = emitDiagnostic(diag::cannot_infer_closure_result_type);

// If there is a location for an 'in' token, then the argument list was
Expand All @@ -7068,7 +7082,7 @@ bool UnableToInferClosureReturnType::diagnoseAsError() {
//
// As such, we insert " () -> ReturnType in " right after the '{' that
// starts the closure body.
diagnostic.fixItInsertAfter(closure->getBody()->getLBraceLoc(),
diagnostic.fixItInsertAfter(body->getLBraceLoc(),
diag::insert_closure_return_type_placeholder,
/*argListSpecified=*/true);
}
Expand Down