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-11166] Confusing fixit when protocol requirement has masked type name #53563

Open
swift-ci opened this issue Jul 19, 2019 · 4 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11166
Radar rdar://problem/53503285
Original Reporter erikstrottmann (JIRA User)
Type Bug
Status In Progress
Resolution
Environment

Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.6.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee owenvoorhees (JIRA)
Priority Medium

md5: 1643cef766ac65aad555ced43c795fe9

is duplicated by:

  • SR-11326 Can't conform to Objective-C protocol

relates to:

  • SR-8668 Give a sensible warning for protocol namespace issues

Issue Description:

Potentially fixed when https://bugs.swift.org/browse/SR-11024 was fixed, but I wanted to make sure.

When a protocol defined in one file references a type defined in a separate module, but a type defined in another file that doesn’t import the separate module tries to conform to the protocol, the Swift compiler gives a confusing fixit that doesn’t fix the error, but causes a new error.

// Module1/Result.swift

public enum Result<Success, Failure: Error> {
    case success(Success)
    case failure(Failure)
}
// Module2/WidgetCounting.swift

import Module1

protocol WidgetCounting {
    func countWidgets() -> Result<Int, Error> // Module1.Result
}
// Module2/WidgetCounter.swift

// Without this import, WidgetCounter cannot conform to WidgetCounting:
// import Module1

struct WidgetCounter: WidgetCounting {
    func countWidgets() -> Result<Int, Error> { // Swift.Result, not Module1.Result
        return .success(42)
    }
}

The compiler gives the following diagnostic and confusing fixit:

error: type 'WidgetCounter' does not conform to protocol 'WidgetCounting'
note: candidate has non-matching type '() -> Result<Int, Error>'
note: protocol requires function 'countWidgets()' with type '() -> Result<Int, Error>'; do you want to add a stub?

When you apply the fixit, WidgetCounter now has two countWidgets() methods with type () -> Result<Int, Error>, so you get a new diagnostic, error: invalid redeclaration of 'countWidgets()'.

WidgetCounting is using Module1.Result, but WidgetCounter doesn’t import Module1, so it’s using Swift.Result. The initial diagnostic should make this distinction clear, and the fixit should either add import Module1 or explicitly specify Module1.Result instead of adding the unqualified Result.

@belkadan
Copy link
Contributor

cc owenvoorhees (JIRA User)

@swift-ci
Copy link
Collaborator Author

Comment by Owen Voorhees (JIRA)

The SR-11024 fix isn't able to help in this case because it only disambiguates types which appear together in a diagnostic message. It looks like this bug still exists on master, it'll probably require a tailored fix to handle the stub insertion fixit properly.

@belkadan
Copy link
Contributor

Drat, right. If only notes really were attached to parent diagnostics, instead of just being juxtaposed.

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Owen Voorhees (JIRA)

I'm focusing on fixing this closely related issue first which offers an unqualified fix-it:

struct String {}
let x: Any = ""
let y: Swift.String = x
//                     ^ as! String

Once that's been resolved I think most of the pieces will be in place to fix this

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants