Skip to content

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

Merged
merged 1 commit into from
Oct 14, 2016

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Oct 11, 2016

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.

@xedin
Copy link
Contributor Author

xedin commented Oct 11, 2016

/cc @rudkx

@rudkx rudkx self-assigned this Oct 11, 2016
@@ -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>'}}
Copy link
Contributor

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.

Copy link
Contributor Author

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'}}
Copy link
Contributor

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.

Copy link
Contributor Author

@xedin xedin Oct 11, 2016

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

Copy link
Contributor

@rudkx rudkx left a 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.

@rudkx
Copy link
Contributor

rudkx commented Oct 14, 2016

@swift-ci Please smoke test

@rudkx rudkx merged commit 32634b1 into swiftlang:master Oct 14, 2016
@rudkx
Copy link
Contributor

rudkx commented Oct 14, 2016

@xedin Thanks for the patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants