From 2d10a8a9a0c5ac7f0de5cf8bda980dbad0b5e4e2 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Thu, 30 Jan 2020 09:45:29 -0800 Subject: [PATCH 1/2] [ConstraintSystem] Extend function type conversion mismatch coverage Originally type mismatches associated with conversions between function types were only covered for coercions and assignments but fix coverage grew since then and now it makes sense to remove special case and let `repairFailures` take care of it. --- lib/Sema/CSSimplify.cpp | 17 ++--------------- test/Constraints/enum_cases.swift | 8 ++++---- test/Constraints/tuple_arguments.swift | 3 ++- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index 2a24f5f95d981..fabf1338b2a97 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -4083,21 +4083,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()) - 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(anchor)) - break; - } + if (shouldAttemptFixes() && result.isFailure()) + break; return result; } diff --git a/test/Constraints/enum_cases.swift b/test/Constraints/enum_cases.swift index 5a918dacd6fa1..dbfe34ec5ca59 100644 --- a/test/Constraints/enum_cases.swift +++ b/test/Constraints/enum_cases.swift @@ -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.foo) // Ok let _ = arr.map(G_E.bar) // Ok let _ = arr.map(G_E.two) // expected-error {{cannot convert value of type '(String, String) -> G_E' to expected argument type '(String) throws -> G_E'}} -let _ = arr.map(G_E.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(@escaping ((x: Int, y: Int)) -> G_E)'}} -// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}} +let _ = arr.map(G_E.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> G_E' 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 diff --git a/test/Constraints/tuple_arguments.swift b/test/Constraints/tuple_arguments.swift index 3011132d48d78..0b1852ea221ed 100644 --- a/test/Constraints/tuple_arguments.swift +++ b/test/Constraints/tuple_arguments.swift @@ -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' From 6debe3d3b500420b474e9d86a5e5ec25bc63ed23 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Thu, 30 Jan 2020 09:54:24 -0800 Subject: [PATCH 2/2] [ConstraintSystem] Don't produce conformance fixes for mismatches associated with function result type 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. --- lib/Sema/CSSimplify.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index fabf1338b2a97..09a1c7eda4973 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -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()) + return getTypeMatchFailure(locator); + } else { // There are no elements in the path auto *anchor = locator.getAnchor(); if (!(anchor &&