-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SR-1255: Improve diagnostic when one of the parameters marked as autoclosure #5239
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
Conversation
…marked as autoclosure
/cc @rudkx |
@@ -735,8 +735,7 @@ func testNilCoalescePrecedence(cond: Bool, a: Int?, r: CountableClosedRange<Int> | |||
|
|||
// ?? should have lower precedence than range and arithmetic operators. | |||
let r1 = r ?? (0...42) // ok | |||
let r2 = (r ?? 0)...42 // not ok: expected-error {{binary operator '??' cannot be applied to operands of type 'CountableClosedRange<Int>?' and 'Int'}} | |||
// expected-note @-1 {{overloads for '??' exist with these partially matching parameter lists:}} | |||
let r2 = (r ?? 0)...42 // not ok: expected-error {{cannot convert value of type 'Int' to expected argument type 'CountableClosedRange<Int>'}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't really seem like an improvement, but I'm not sure if it's really that bad if we have the caret in the right place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is complete output of the diagnostic (caret is at the right place):
(swift) func testNilCoalescePrecedence(cond: Bool, a: Int?, r: CountableClosedRange<Int>?) {
let r1 = r ?? (0...42)
let r2 = (r ?? 0)...42
}
<REPL Input>:3:18: error: cannot convert value of type 'Int' to expected argument type 'CountableClosedRange<Int>'
let r2 = (r ?? 0)...42
^
@@ -288,7 +288,7 @@ func rdar19836341(_ ns: NSString?, vns: NSString?) { | |||
// <rdar://problem/20029786> Swift compiler sometimes suggests changing "as!" to "as?!" | |||
func rdar20029786(_ ns: NSString?) { | |||
var s: String = ns ?? "str" as String as String // expected-error{{cannot convert value of type 'NSString?' to expected argument type 'String?'}} | |||
var s2 = ns ?? "str" as String as String // expected-error {{binary operator '??' cannot be applied to operands of type 'NSString?' and 'String'}} expected-note{{}} | |||
var s2 = ns ?? "str" as String as String // expected-error {{cannot convert value of type 'String' to expected argument type 'NSString'}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with the example in expr/expressions.swift, this seems like a regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a user I think it makes more sense to get an error about "str" as String
producing unexpected type of "String" instead of "NSString" which would much an "NSString?" in that case, because original error is not every helpful to figure out what is actually wrong...
Here is the output of the new diagnostic:
<REPL Input>:2:44: error: cannot convert value of type 'String' to expected argument type 'NSString'
var s2 = ns ?? "str" as String as String
~~~~~~~~~~~~~~~~^~~~~~~~~
as NSString
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, looking at it in context I think this is an overall improvement, if not perfect.
@swift-ci Please smoke test |
@xedin Thanks for the patch! |
Fixes situation (and as a result improves diagnostics) when one of the function application
arguments was resolved into "autoclosure" by the solver. Currently in such situation, when
evaluating closeness of the candidate, function would try to match AnyFunctionType (produced by "autoclosure") to the corresponding parameter type and fail, where correct behavior would be to try and match resulting type of the closure to the parameter type.
Resolves SR-1255.