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-10831] Associated type inference doesn't work when type is fully constrained #53221

Closed
swift-ci opened this issue Jun 4, 2019 · 4 comments
Assignees
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

swift-ci commented Jun 4, 2019

Previous ID SR-10831
Radar None
Original Reporter marcpalmer (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: fddfa1e975a38ccef890a5c04e65bc5f

relates to:

  • SR-11671 Assoc == Self not treated as an abstract type witness upon conformance

Issue Description:

struct G<T> {}
  
protocol P {
  associatedtype T
  associatedtype U
}

protocol Q: P where T == G<U> {}

protocol R: Q where U == Int {}

struct X: R {}
@swift-ci
Copy link
Collaborator Author

Comment by Marc Palmer (JIRA)

This was created at WWDC 2019 in the Swift Labs with @slavapestov - with an indicative if not definitive code example. It was borne out of discussion of problems in my open source framework Flint:

public protocol DismissingUIAction: UIAction {
    associatedtype InputType = DismissUIInput
    associatedtype PresenterType = UIViewController
}

public extension DismissingUIAction where InputType == DismissUIInput, PresenterType == UIViewController {
    static func perform(context: ActionContext<DismissUIInput>, presenter: UIViewController, completion: Completion) -> Completion.Status {
        presenter.dismiss(animated: context.input.animated)
        return completion.completedSync(.successWithFeatureTermination)
    }
}

This code used to use `typealias` instead of `associatedtype` but this produced "false" warnings and advice to constrain the protocol instead. However the above code "worked" but the constraint was required on the extension. Slava advised this:

public protocol DismissingUIAction: UIAction where InputType == DismissUIInput, PresenterType == UIViewController {
}

public extension DismissingUIAction {
    static func perform(context: ActionContext<DismissUIInput>, presenter: UIViewController, completion: Completion) -> Completion.Status {
        presenter.dismiss(animated: context.input.animated)
        return completion.completedSync(.successWithFeatureTermination)
    }
}

However this would not allow conforming types to compile unless they also defined the associated types and this is the bug in question. Slava therefore recommended this workaround which seems to satisfy it, albeit oversatisfying the types:

public protocol DismissingUIAction: UIAction where InputType == DismissUIInput, PresenterType == UIViewController {
    associatedtype InputType = DismissUIInput
    associatedtype PresenterType = UIViewController
}

public extension DismissingUIAction {
    static func perform(context: ActionContext<DismissUIInput>, presenter: UIViewController, completion: Completion) -> Completion.Status {
        presenter.dismiss(animated: context.input.animated)
        return completion.completedSync(.successWithFeatureTermination)
    }
}

@slavapestov
Copy link
Member

The test case works but here is one that does not:

struct G<T> {}
  
protocol P {
  associatedtype T
  associatedtype U
}

protocol Q: P where T == G<U> {}

protocol R: Q where U == Int {}

struct X: R {}

@AnthonyLatsis
Copy link
Collaborator

#27425

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@slavapestov
Copy link
Member

This works in 5.9 and main.

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

3 participants