Skip to content

[ConstraintSystem] Extend function type conversion mismatch coverage #29549

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
Jan 30, 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
28 changes: 13 additions & 15 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,17 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,

break;
}

// TODO(diagnostics): If there are any requirement failures associated
// with result types which are part of a function type conversion,
// let's record general conversion mismatch in order for it to capture
// and display complete function types.
//
// Once either reacher locators or better diagnostic presentation for
// nested type failures is available this check could be removed.
if (last->is<LocatorPathElt::FunctionResult>())
return getTypeMatchFailure(locator);

} else { // There are no elements in the path
auto *anchor = locator.getAnchor();
if (!(anchor &&
Expand Down Expand Up @@ -4083,21 +4094,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,

auto result = matchFunctionTypes(func1, func2, kind, flags, locator);

if (shouldAttemptFixes() && result.isFailure()) {
// If this is a contextual type mismatch failure
// let's give the solver a chance to "fix" it.
if (auto last = locator.last()) {
if (last->is<LocatorPathElt::ContextualType>())
break;
}

// If this is a type mismatch in assignment, we don't really care
// (yet) was it argument or result type mismatch, let's produce a
// diagnostic which mentions both function types.
auto *anchor = locator.getAnchor();
if (anchor && isa<AssignExpr>(anchor))
break;
}
if (shouldAttemptFixes() && result.isFailure())
break;

return result;
}
Expand Down
8 changes: 4 additions & 4 deletions test/Constraints/enum_cases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ let _ = arr.map(E.bar) // Ok
let _ = arr.map(E.two) // expected-error {{cannot invoke 'map' with an argument list of type '(@escaping (Int, Int) -> E)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}

let _ = arr.map(E.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(@escaping ((x: Int, y: Int)) -> E)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
let _ = arr.map(E.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> E' to expected argument type '(String) throws -> T'}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}

let _ = arr.map(G_E<String>.foo) // Ok
let _ = arr.map(G_E<String>.bar) // Ok
let _ = arr.map(G_E<String>.two) // expected-error {{cannot convert value of type '(String, String) -> G_E<String>' to expected argument type '(String) throws -> G_E<String>'}}
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(@escaping ((x: Int, y: Int)) -> G_E<Int>)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> G_E<Int>' to expected argument type '(String) throws -> T'}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}

let _ = E.foo("hello") // expected-error {{missing argument label 'bar:' in call}}
let _ = E.bar("hello") // Ok
Expand Down
3 changes: 2 additions & 1 deletion test/Constraints/tuple_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,8 @@ _ = x.map { (_: ()) in () }
// https://bugs.swift.org/browse/SR-9470
do {
func f(_: Int...) {}
let _ = [(1, 2, 3)].map(f) // expected-error {{cannot invoke 'map' with an argument list of type '(@escaping (Int...) -> ())'}}
let _ = [(1, 2, 3)].map(f) // expected-error {{cannot convert value of type '(Int...) -> ()' to expected argument type '((Int, Int, Int)) throws -> T'}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
}

// rdar://problem/48443263 - cannot convert value of type '() -> Void' to expected argument type '(_) -> Void'
Expand Down