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-11506] terrible diagnostic (ambiguous expression type) when required parameter is omitted #53907

Closed
weissi opened this issue Sep 22, 2019 · 5 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

@weissi
Copy link
Member

weissi commented Sep 22, 2019

Previous ID SR-11506
Radar rdar://problem/55603016
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Done

Attachment: Download

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

md5: 65c83ea8b5fa11034876c35b34e4ba5a

Issue Description:

The following program

class Klazz {
    required init(foo: String, bar: String?, buz: String?, qux: String) {
      fatalError()
    }

     convenience init(foo: String, bar: String?, buz: String?) {
        self.init(foo: foo, bar: bar, buz: buz, qux: "")
    }

    func foo() {
        Klazz.init(foo: "", bar: nil)
    }
}

has one problem: The call to Klazz.init(foo: "", bar: nil) is missing the required bar parameter. But instead of telling us that, the compiler claims there's an ambiguity:

$ swift test.swift
test.swift:11:9: error: ambiguous reference to member 'init(foo:bar:buz:qux:)'
        Klazz.init(foo: "", bar: nil)
        ^~~~~
test.swift:2:14: note: found this candidate
    required init(foo: String, bar: String?, buz: String?, qux: String) {
             ^
test.swift:6:18: note: found this candidate
     convenience init(foo: String, bar: String?, buz: String?) {
                 ^

In this example, this is rather easy to see what's wrong. But in a real-world example we had

    init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: APNSwiftBearerToken, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String?, pushType: APNSwiftConnection.PushType) {
        self.configuration = configuration
        self.bearerToken = bearerToken
        self.deviceToken = deviceToken
        self.expiration = expiration
        self.priority = priority
        self.collapseIdentifier = collapseIdentifier
        self.topic = topic
        self.pushType = pushType
    }
    
    convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: APNSwiftBearerToken, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String?) {
        self.init(deviceToken: deviceToken, configuration: configuration, bearerToken: bearerToken, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic, pushType: .alert)

    }

and the call-site in a test was

        let handler: APNSwiftRequestEncoder<BasicNotification> = APNSwiftRequestEncoder<BasicNotification>(deviceToken: deviceToken, configuration: apnsConfig, bearerToken: token, expiration: nil, priority: nil, collapseIdentifier: nil, topic: nil)

where Xcode literally only shows

Expression type 'APNSwiftRequestEncoder<BasicNotification>' is ambiguous without more context

(see screenshot). When using the complete build output or SwiftPM, the message is slightly longer (at least showing the "ambiguity" candidates) but still incredibly unhelpful:

$ swift build --build-tests
/Users/johannes/extsrc/APNSwift/Tests/APNSwiftTests/APNSwiftRequestTests.swift:193:66: error: expression type 'APNSwiftRequestEncoder<BasicNotification>' is ambiguous without more context
        let handler: APNSwiftRequestEncoder<BasicNotification> = APNSwiftRequestEncoder<BasicNotification>(deviceToken: deviceToken, configuration: apnsConfig, bearerToken: token, expiration: nil, priority: nil, collapseIdentifier: nil)
                                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is Xcode 11.0/Swift 5.1

@weissi
Copy link
Member Author

weissi commented Sep 22, 2019

@swift-ci create

@belkadan
Copy link
Contributor

I think your stripped-down test case has the correct error message, other than the use of the full name in the "ambiguous reference to" (tracked elsewhere). The type checker isn't going to assume you're not trying to do a recursive call with different arguments. Do you have the full test case, where it fell all the back to "expression is ambiguous"?

@weissi
Copy link
Member Author

weissi commented Sep 23, 2019

@belkadan oh yes, sorry, the original error was even worse, you can repro it this way:

git clone https://github.com/kylebrowning/APNSwift && cd APNSwift && git checkout apns-push-type && git reset --hard bf3dbff8bca5b8f4ce867cb188f56472e6e7d5b0 && swift build --build-tests

@xedin
Copy link
Member

xedin commented Feb 19, 2020

Looks like this has been fixed in 11.4. @weissi Could you please verify?

@weissi
Copy link
Member Author

weissi commented Feb 20, 2020

indeed, very nice, thanks @xedin. FTR:

/private/tmp/qqq/APNSwift/Tests/APNSwiftTests/APNSwiftRequestTests.swift:193:236: error: missing argument for parameter 'topic' in call
        let handler: APNSwiftRequestEncoder<BasicNotification> = APNSwiftRequestEncoder<BasicNotification>(deviceToken: deviceToken, configuration: apnsConfig, bearerToken: token, expiration: nil, priority: nil, collapseIdentifier: nil)
                                                                                                                                                                                                                                           ^
                                                                                                                                                                                                                                           , topic: <#String?#>
/private/tmp/qqq/APNSwift/Sources/APNSwift/APNSwiftRequestEncoder.swift:50:17: note: 'init(deviceToken:configuration:bearerToken:expiration:priority:collapseIdentifier:topic:)' declared here
    convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: APNSwiftBearerToken, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String?) {
                ^
[623/625] Compiling APNSwiftTests XCTestManifests.swift

@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