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-11388] "Self != Self" - Incorrect error message when passing bad argument type to IME init return from static var in secondary protocol extension #53789

Closed
swift-ci opened this issue Aug 28, 2019 · 8 comments
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 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11388
Radar None
Original Reporter BenLeggiero (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.14.6 (18G87)
Xcode Version 11.0 beta 6 (11M392q)
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.7.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee BenLeggiero (JIRA)
Priority Medium

md5: 5835c876734f442ae5c4fc01cc2b54e9

Issue Description:

Steps to reproduce:

1. Define a protocol Foo with an associated type requirement Bar
2. Write an extension on Foo so that it has a synthesized initializer which takes a Bar
3. Write another extension of Foo so that it defines a static computed var of type Foo, which calls the synthesized initializer as an IME, passing it some wrong type (e.g. integer literal when Bar is not expressible by an integer literal)

Expected Behavior

4. An error message describes that the initializer expects some type, but a wrong type was given

Actual Behavior

4. An error message claims that "Member 'init' in 'Self' produces result of type 'Self', but context expects 'Self'"

Known workarounds

  1. Place the static computed var within the first protocol extension

  2. Call the Foo initializer as a member of Foo instead of as an IME

SSCCE (Incorrect error message)

public protocol Foo {
    associatedtype Bar
}

public extension Foo {
    init(bar: Bar) {
        fatalError()
    }
}

public extension Foo {
    static var baz: Self { return .init(bar: 0) }
}

Error message

/Path/To/main.swift:12:36: error: member 'init' in 'Self' produces result of type 'Self', but context expects 'Self'
    static var baz: Self { return .init(bar: 0) }
                                   ^

SSCCE (Acceptable error message A)

public protocol Foo {
    associatedtype Bar
}

public extension Foo {
    init(bar: Bar) {
        fatalError()
    }



    static var baz: Self { return .init(bar: 0) }
}

Error message

/Path/To/main.swift:12:36: error: type of expression is ambiguous without more context
    static var baz: Self { return .init(bar: 0) }
                                  ~^~~~~~~~~~~~

SSCCE (Acceptable error message B)

public protocol Foo {
    associatedtype Bar
}

public extension Foo {
    init(bar: Bar) {
        fatalError()
    }
}

public extension Foo {
    static var baz: Self { return Self.init(bar: 0) }
}

Error message

/Path/To/main.swift:12:40: error: cannot invoke 'init' with an argument list of type '(bar: Int)'
    static var baz: Self { return Self.init(bar: 0) }
                                       ^
/Path/To/main.swift:12:40: note: expected an argument list of type '(bar: Self.Bar)'
    static var baz: Self { return Self.init(bar: 0) }
                                       ^
@theblixguy
Copy link
Collaborator

cc @xedin

@swift-ci
Copy link
Collaborator Author

Comment by Ky (JIRA)

I verified that this still occurs in this environment:

macOS 10.14.6 (18G87)
Version 11.0 beta 6 (11M392r)
Apple Swift version 5.1 (swiftlang-1100.0.270.6 clang-1100.0.32.1)
Target: x86_64-apple-darwin18.7.0

@xedin
Copy link
Member

xedin commented Sep 8, 2019

Unfortunately we are still pretty bad at properly diagnosing argument-to-parameter mismatches like examples in the description. That said, that's something we are also actively working on so with #27063 diagnostics would be improved and would produce following error in all three cases:

error: cannot convert value of type 'Int' to expected argument type 'Self.Bar'
static var baz: Self { return .init(bar: 0) }
                                         ^

I'll keep you posted when it lands so you can verify.

@xedin
Copy link
Member

xedin commented Sep 16, 2019

Resolved by PR #27063 Please use the next available snapshot of master to verify.

@swift-ci
Copy link
Collaborator Author

Comment by Ky (JIRA)

I can still reproduce this issue with this environment:

macOS 10.14.6 (18G95)
Xcode Version 11.2 beta 2 (11B44)
Apple Swift version 5.1.1 (swiftlang-1100.0.275.2 clang-1100.0.33.9)
Target: x86_64-apple-darwin18.7.0

@theblixguy
Copy link
Collaborator

It’s because the improvements have landed on master, so you’ll have to use a development snapshot to verify (or wait until Swift 5.2 is out). It hasn’t made its way into Xcode 11 (which ships with 5.1) because it was too late in the release cycle do it.

@xedin
Copy link
Member

xedin commented Oct 23, 2019

@theblixguy Thank you! BenLeggiero (JIRA User) That is indeed the case.

@swift-ci
Copy link
Collaborator Author

Comment by Ky (JIRA)

Verified fixed in Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants