Skip to content
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

[SR-6939] Swift 4.1 type-checker regression: cannot convert value of type 'Double' to expected argument type '_?' #49487

Closed
NachoSoto opened this issue Feb 6, 2018 · 10 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.1 type checker Area → compiler: Semantic analysis

Comments

@NachoSoto
Copy link
Contributor

Previous ID SR-6939
Radar rdar://problem/37291371
Original Reporter @NachoSoto
Type Bug
Status Resolved
Resolution Done
Environment

Swift version 4.1 (swiftlang-902.0.38 clang-902.0.30)
Target: x86_64-apple-darwin17.4.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.1Regression, TypeChecker
Assignee @xedin
Priority Medium

md5: f7d715104f73ab7429cf6e14e955bb49

Issue Description:

This is a reduction from my project (thanks to @belkadan for helping me reduce it).
The following code compiles on Swift 4.0.3:

extension Collection where Element: Numeric {
    var sum: Element {
        return self.reduce(0, +)
    }
}

struct Expectation<T> {}
func expect<T>(_ expression: @autoclosure @escaping () throws -> T?, file: String = #file, line: UInt = #line) -> Expectation<T> { 
    return Expectation() 
}
func expect<T>(_ file: String = #file, line: UInt = #line, expression: @escaping () throws -> T?) -> Expectation<T> {
    return Expectation()
}

func ==<T: Equatable>(lhs: Expectation<T>, rhs: T?) {}

expect([Double(1.0)].sum) == Double(1.0)

But fails to compile on Swift 4.1 (as of Xcode 9.3 beta 2):

error: cannot convert value of type 'Double' to expected argument type '_?'
expect([Double(1.0)].sum) == Double(1.0)
       ~~~~~~~~~~~~~~^~~
@belkadan
Copy link
Contributor

belkadan commented Feb 6, 2018

@swift-ci create

@belkadan
Copy link
Contributor

belkadan commented Feb 6, 2018

@rudkx, another autoclosure one. :-(

@NachoSoto
Copy link
Contributor Author

BTW ([1.0] as [Double]).sum like you suggested @belkadan does compile, so I'm changing it to that (which is probably cleaner anyway), but this regression can likely surface in other contexts?

@airspeedswift
Copy link
Member

I think might be because Optional is now Equatable.

@rudkx
Copy link
Member

rudkx commented Feb 6, 2018

Pavel, can you see if this is covered by #14441

@xedin
Copy link
Member

xedin commented Feb 6, 2018

@rudkx it doesn't look like the problem here is related to function attributes because second `expect` can't match since it requires a label `expression` to properly form that call. So much changes didn't fix this one.

@airspeedswift
Copy link
Member

Getting rid of the autoclosures produces the same problem.

expect(.some([Double(1.0)].sum)) == Double(1.0) typechecks ok.

@NachoSoto
Copy link
Contributor Author

Interesting. This might be because the expression is ambiguous now (I got some combination of this code to also produce the same error in Swift 4.0.x). This needs a better diagnostic too, whatever the problem actually is.

@xedin
Copy link
Member

xedin commented Feb 7, 2018

It looks like the problem here is actually related to initializer of the Double and how we attempt the bindings, probably we loose implicit conversion to optional on the way which results in no solutions produces for this expression and bad diagnostics...

@belkadan
Copy link
Contributor

Pavel fixed this and cherry-picked the fix to the 4.1 branch in #14490

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.1 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

6 participants