Skip to content

[Diagnostics] Port explicit closure result contextual mismatch #28240

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 2 commits into from
Nov 14, 2019
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
29 changes: 0 additions & 29 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3243,35 +3243,6 @@ bool FailureDiagnosis::diagnoseClosureExpr(
return true;
}

// If the body of the closure looked ok, then look for a contextual type
// error. This is necessary because FailureDiagnosis::diagnoseExprFailure
// doesn't do this for closures.
if (contextualType) {
auto fnType = contextualType->getAs<AnyFunctionType>();
if (!fnType || fnType->isEqual(CS.getType(CE)))
return false;

auto contextualResultType = fnType->getResult();
// If the result type was unknown, it doesn't really make
// sense to diagnose from expected to unknown here.
if (isInvalidClosureResultType(contextualResultType))
return false;

// If the closure had an explicitly written return type incompatible with
// the contextual type, diagnose that.
if (CE->hasExplicitResultType() &&
CE->getExplicitResultTypeLoc().getTypeRepr()) {
auto explicitResultTy = CE->getExplicitResultTypeLoc().getType();
if (fnType && !explicitResultTy->isEqual(contextualResultType)) {
auto repr = CE->getExplicitResultTypeLoc().getTypeRepr();
diagnose(repr->getStartLoc(), diag::incorrect_explicit_closure_result,
explicitResultTy, fnType->getResult())
.fixItReplace(repr->getSourceRange(),fnType->getResult().getString());
return true;
}
}
}

// Otherwise, we can't produce a specific diagnostic.
return false;
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,17 @@ bool ContextualFailure::diagnoseAsError() {
Diag<Type, Type> diagnostic;
switch (path.back().getKind()) {
case ConstraintLocator::ClosureResult: {
auto *closure = cast<ClosureExpr>(getRawAnchor());
if (closure->hasExplicitResultType() &&
closure->getExplicitResultTypeLoc().getTypeRepr()) {
auto resultRepr = closure->getExplicitResultTypeLoc().getTypeRepr();
emitDiagnostic(resultRepr->getStartLoc(),
diag::incorrect_explicit_closure_result, getFromType(),
getToType())
.fixItReplace(resultRepr->getSourceRange(), getToType().getString());
return true;
}

diagnostic = diag::cannot_convert_closure_result;
break;
}
Expand Down
19 changes: 18 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3265,8 +3265,25 @@ bool ConstraintSystem::repairFailures(
break;
}

case ConstraintLocator::FunctionResult: {
auto *loc = getConstraintLocator(anchor, {path.begin(), path.end() - 1});
// If this is a mismatch between contextual type and (trailing)
// closure with explicitly specified result type let's record it
// as contextual type mismatch.
if (loc->isLastElement<LocatorPathElt::ContextualType>() ||
loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
auto *argExpr = simplifyLocatorToAnchor(loc);
if (argExpr && isa<ClosureExpr>(argExpr)) {
conversionsOrFixes.push_back(ContextualMismatch::create(
*this, lhs, rhs,
getConstraintLocator(argExpr, ConstraintLocator::ClosureResult)));
break;
}
}
LLVM_FALLTHROUGH;
}

case ConstraintLocator::Member:
case ConstraintLocator::FunctionResult:
case ConstraintLocator::DynamicLookupResult: {
// Most likely this is an attempt to use get-only subscript as mutating,
// or assign a value of a result of function/member ref e.g. `foo() = 42`
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ overloaded { print("hi"); print("bye") } // multiple expression closure without
func not_overloaded(_ handler: () -> Int) {}

not_overloaded { } // empty body
// expected-error@-1 {{cannot convert value of type '() -> ()' to expected argument type '() -> Int'}}
// expected-error@-1 {{cannot convert value of type '()' to closure result type 'Int'}}

not_overloaded { print("hi") } // single-expression closure
// expected-error@-1 {{cannot convert value of type '()' to closure result type 'Int'}}
Expand All @@ -786,7 +786,7 @@ func test() -> Int? {
}

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

fn = {}
// expected-error@-1 {{cannot assign value of type '() -> ()' to type '() -> [Int]'}}
Expand Down
4 changes: 2 additions & 2 deletions test/Sema/immutability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ func sr4214() {
let closure = { val in val.x = 7 } as (inout MutableSubscripts) -> () // Ok
var v = MutableSubscripts()
closure(&v)
// FIXME: This diagnostic isn't really all that much better
// expected-error@+1 {{cannot convert value of type '(inout MutableSubscripts) -> ()' to expected argument type '(_) -> _'}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

// expected-error@+2 {{declared closure result '()' is incompatible with contextual type 'MutableSubscripts'}}
// expected-error@+1 {{cannot convert value of type '(inout MutableSubscripts) -> ()' to expected argument type '(MutableSubscripts) -> MutableSubscripts'}}
sequence(v) { (state : inout MutableSubscripts) -> () in
_ = MutableSubscripts.initialize(from: &state)
return ()
Expand Down
2 changes: 1 addition & 1 deletion test/Sema/substring_to_string_conversion_swift4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ do {

// CTP_ClosureResult
do {
[ss].map { (x: Substring) -> String in x } // expected-error {{cannot convert value of type 'Substring' to closure result type 'String'}} {{42-42=String(}} {{43-43=)}}
[ss].map { (x: Substring) -> String in x } // expected-error {{declared closure result 'Substring' is incompatible with contextual type 'String'}}
}

// CTP_ArrayElement
Expand Down