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-7009] Required Initializer of Generic Type unavailable in Protocol Extension #49557

Closed
swift-ci opened this issue Feb 15, 2018 · 6 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7009
Radar None
Original Reporter benpious (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Swift 4.0, Xcode 9.2.0.

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 05857787b8520078ff71fba1bad937e2

duplicates:

  • SR-2190 Failing to constrain generic type with protocol

Issue Description:

If I write a class C with a generic T constrained to UIView, then write an extension where T is further constrained to a protocol P with a required initializer, I expect that I should be able to call that required initializer in that generic. However, Swift fails to find it, and offers me init(frame:) and required init(coder:) from UIView instead. init(frame:) shouldn't even be safe to call, since T could be any UIView} subclass.

import UIKit

protocol P: AnyObject {
    init(a: String)
}

class C<T> where T: UIView {
    let p: T
    init(p: T) {
        self.p = p
    }
}

extension C where T: P {
    convenience init() {
        self.init(p: T.init(a: "a"))
    }
}

class D: UIView, P {
    required init(a: String) {
    }
    required init?(coder aDecoder: NSCoder) {
    }
}

Gives the following errors:

error: ProtocolInits.playground:18:24: error: argument labels '(a:)' do not match any available overloads
        self.init(p: T.init(a: "a"))
                       ^   ~~~~~~~~

ProtocolInits.playground:18:24: note: overloads for 'T.Type.init' exist with these partially matching parameter lists: (frame: CGRect), (coder: NSCoder)
        self.init(p: T.init(a: "a"))

This also happens in a normal Xcode project.

I tried to repro this without UIKit, but I wasn't able to figure out what's special about UIView.

@belkadan
Copy link
Contributor

@rudkx, @xedin, we have a dup of this already, right?

@xedin
Copy link
Member

xedin commented Feb 17, 2018

/cc @DougGregor, I wonder what is so special about `UIView` in this case?

@belkadan
Copy link
Contributor

I don't think there's anything specific to UIView. It's just a class bound that has initializers of its own.

@xedin
Copy link
Member

xedin commented Feb 19, 2018

I'm not so sure what is going on here then because following code compiles on 4.0/4.1 and master nightly builds:

import Foundation
@objc class A : NSObject {
  init(foo: Int) {}
}

protocol P: AnyObject {
    init(a: String)
}

class C<T> where T: A {
    let p: T
    init(p: T) {
        self.p = p
    }
}

extension C where T: P {
    convenience init() {
        self.init(p: T.init(a: "a"))
    }
}

class D: A, P {
    required init(a: String) {
      super.init(foo: 42)
    }
}

@swift-ci
Copy link
Collaborator Author

Comment by Ben Pious (JIRA)

Looked into it a little more. This occurs with any class that's declared in Objective-C. Attached a sample project demonstrating this. Inits.zip

@xedin
Copy link
Member

xedin commented Feb 19, 2018

Thanks, benpious (JIRA User)! I'll take a look

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

No branches or pull requests

3 participants