Navigation Menu

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-11137] Type inference compiler error #53533

Closed
Agarunov opened this issue Jul 15, 2019 · 7 comments
Closed

[SR-11137] Type inference compiler error #53533

Agarunov opened this issue Jul 15, 2019 · 7 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@Agarunov
Copy link

Previous ID SR-11137
Radar rdar://problem/53646997
Original Reporter @Agarunov
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 11.0 beta 3 (11M362v)

Toolchain: Swift 5.1 Snapshot 2019-07-14 (a)

Toolchain: Swift Development Snapshot 2019-07-13 (a)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee @Agarunov
Priority Medium

md5: cd3e80734b2d62928e4a81f70923c31e

relates to:

  • SR-7666 Compiler incorrectly infers generic parameter type of methods used within conditionally conformed extensions for that type

Issue Description:

This code produces error:

public protocol MySelectionManager {
    
    associatedtype SelectionValue : Hashable
    
    mutating func select(_ value: Self.SelectionValue)
    mutating func deselect(_ value: Self.SelectionValue)
    func isSelected(_ value: Self.SelectionValue) -> Bool
}


struct MyBinding<Value> {
    
    var wrappedValue: Value {
        get { getValue() }
        nonmutating set { setValue(newValue) }
    }
    
    let getValue: () -> Value
    let setValue: (Value) -> Void    
}


extension MyBinding where Value: MySelectionManager {


    func isSelected(_ value: Value.SelectionValue) -> MyBinding<Bool> {
        MyBinding(
            getValue: { self.wrappedValue.isSelected(value) }, // error: Cannot convert value of type 'Bool' to closure result type '_'
            setValue: { isSelected in
                if isSelected {
                    self.wrappedValue.select(value)
                } else {
                    self.wrappedValue.deselect(value)
                }
            }
        )
    }


}

But if I explicitly add generic type in code error disappears:

extension MyBinding where Value: MySelectionManager {


    func isSelected(_ value: Value.SelectionValue) -> MyBinding<Bool> {
        MyBinding<Bool>( // add <Bool> explicitly
            getValue: { self.wrappedValue.isSelected(value) },
            setValue: { isSelected in
                if isSelected {
                    self.wrappedValue.select(value)
                } else {
                    self.wrappedValue.deselect(value)
                }
            }
        )
    }


}
@belkadan
Copy link
Contributor

Yeah, it's a known issue, but it would source-breaking to change. :-(

@Agarunov
Copy link
Author

Ok, but better error message will be very helpful here

@belkadan
Copy link
Contributor

Fair point. @xedin?

@xedin
Copy link
Member

xedin commented Jul 29, 2019

Improving single statement closure diagnostics is something we are actively working towards. Thank you for reporting!

@xedin
Copy link
Member

xedin commented Jul 29, 2019

@swift-ci create

@Agarunov
Copy link
Author

So with Swift 5.2 toolchain new error messages:

Cannot convert return expression of type 'MyBinding<Value>' to return type 'MyBinding<Bool>'
Cannot convert value of type 'Bool' to closure result type 'Value'

With Xcode 11.3 default toolchain:

Cannot convert value of type 'Bool' to closure result type '_'

It looks like new diagnostics system produce better messages for this case. So I can Resolve this task?

@xedin
Copy link
Member

xedin commented Jan 11, 2020

This issue has been fixed by #25060 which is merged into master. We usually ask to validate using the next nightly toolchain of the master because we can't speak to which version of Xcode this is going to end up in.

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

3 participants