swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.52k stars 10.35k forks source link

[SR-7505] Possible name collision circular bugs with autocomplete/Fix-It when defining protocol conformance #50047

Open swift-ci opened 6 years ago

swift-ci commented 6 years ago
Previous ID SR-7505
Radar None
Original Reporter bzamayo (JIRA User)
Type Bug

Attachment: Download

Environment Swift 4.1 or 4.2, observed with Xcode 9.3 and Xcode 9.4
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | CodeCompletion | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 3e98fa5aa6452918412c6b6bee1804f1

Issue Description:

If you have two modules declare the same for a type, say `Result`, and then have a protocol which includes the Result type in one of its arguments, Swift fails to resolve the name conflict when completing the protocol method definition, either with Fix-It or autocomplete. On top of this, the verbalised messages for the build compilation errors are confusing and do not accurately describe the issue. Demo video.

In `ExampleOtherModule`:

public enum Result {
    // result type defined in `ExampleOtherModule`
}

public protocol ExampleProtocol { // protocol that uses the local Result type in a method
    func didFinish(with result: Result)
}

In main module:

enum Result { // result type defined in module

}

class AppDelegate : ExampleProtocol {
    func didFinish(with result: Result) { // this is what Swift/Xcode autocompletes but it is incorrect and will not build
    } 
}

The produced error by following Xcode's lead is: 'Type AppDelegate does not conform to protocol `ExampleProtocol`. This is because `Result` is resolved to be the module-local type, not the type that the protocol obviously refers to.

The correct formulation should be `didFinish(with result: ExampleOtherModule.Result)`. I have made a quick demo video to better show the problem here. Example project is also attached, if it helps.

Debugging this is inscrutable for users not familiar with the modules in a project. I have hit this in the past and this bug report was triggered by an issue posted to GitHub of someone trying to use one of my open-source projects and getting lost when they hit this same problem.

bc7072e8-7d37-4a38-8fee-cab463d443b7 commented 6 years ago

cc @slavapestov

belkadan commented 6 years ago

@benlangmuir, @nkcsgexi, what do you want to do about this?