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-6521] Compiler fails to see a custom initializer in a protocol #49071

Closed
ghugues opened this issue Dec 3, 2017 · 4 comments
Closed

[SR-6521] Compiler fails to see a custom initializer in a protocol #49071

ghugues opened this issue Dec 3, 2017 · 4 comments
Assignees
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

@ghugues
Copy link

ghugues commented Dec 3, 2017

Previous ID SR-6521
Radar None
Original Reporter @ghugues
Type Bug
Status Resolved
Resolution Duplicate
Environment

Tested on Xcode 9.1 and Xcode 9.2 beta 2

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

md5: 72ec1dd83dbe17b9474d41b9caf4ddf8

duplicates:

  • SR-6000 Let protocols inherit from class types

Issue Description:

When defining a custom initializer in a protocol with a class constraint on Self, the compiler doesn't see the custom initializer and tries to use one of the initializers of the class constraint.

Conditions for a compilation failure:

  • A protocol with an initializer in it.
  • A constraint on the protocol with a class
  • This class has a parent class

Below is a sample code to reproduce the issue. Change the definition of BaseClass to trigger the compilation issue.

// Works
class BaseClass {
    required init() {}
}
// Doesn't work
//class ParentOfBaseClass {}
//class BaseClass: ParentOfBaseClass {
//    required override init() {}
//}

protocol StringConstructible where Self: BaseClass
{
    init(string: String)
    static func makeFromString(_ string: String) -> Self
}

class ConcreteClass: BaseClass, StringConstructible
{
    required init() {
    }

    required init(string: String) {
    }

    static func makeFromString(_ string: String) -> Self {
        return self.init(string: string)
    }
}

extension StringConstructible
{
    // Doesn't compile if `BaseClass` has a parent class. The compiler
    // doesn't "see" the custom initializer and uses `init()` instead.
    // The error is: "argument passed to call that takes no arguments".
    func failingTest() {
        _ = Self.init(string: "")
    }

    // Compiles in all cases.
    func passingTest() {
        _ = Self.makeFromString("")
    }
}

// Same as above with generics. This was the initially intended construct.
func failingTest<T>(input: T) where T: StringConstructible {
    _ = T.init(string: "")
}
func passingTest<T>(input: T) where T: StringConstructible {
    _ = T.makeFromString("")
}
@belkadan
Copy link
Contributor

belkadan commented Dec 4, 2017

This sounds familiar. @rudkx, @DougGregor, is there a dup for this?

@belkadan
Copy link
Contributor

belkadan commented Dec 4, 2017

Oh, it's a class constraint on a protocol extension. That might be why—we have a handful of bugs about this.

Guillaume, you should be able to swap the extension type and the constraint to work around this. Thanks for the bug report!

@ghugues
Copy link
Author

ghugues commented Dec 8, 2017

@belkadan I'm not sure I understand what you mean by swap the extension type and the constraint. It's not a class constraint on a protocol extension, it's a class constraint on the protocol itself. The extension below is here only to demonstrate the bug and the generics implementation at the end shows it can be reproduced in other contexts as well.
It may be related to this (not sure though): https://bugs.swift.org/browse/SR-1663

@belkadan
Copy link
Contributor

belkadan commented Dec 9, 2017

Ah, I misread the code. Still, this is not something that's really supported yet. cc @slavapestov

@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

2 participants