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-15263] Function cannot infer associated type #57585

Closed
swift-ci opened this issue Sep 30, 2021 · 3 comments
Closed

[SR-15263] Function cannot infer associated type #57585

swift-ci opened this issue Sep 30, 2021 · 3 comments
Labels
associated type inference compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances feature A feature request or implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-15263
Radar None
Original Reporter acamcor (JIRA User)
Type Bug
Environment

Xcode 13.0 (13A233)

macOS Big Sur 11.6

Can provide more information if needed

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

md5: f079fc0caaa241678ef59a15f716b35a

Issue Description:

Error Description

Class that conforms to protocol with associated type and has as function with generic requirement doesn't compile, altough the compiler knows the exact type but doesn't use it to automatically infer the associated type.

The code to reproduce the error is:

protocol GenericPAT {
    associatedtype T
    func f<U>(u: U) -> T
}

// error: type 'NotWorking' does not conform to protocol 'GenericPAT'
// struct NotWorking: GenericPAT {
//        ^
// note: do you want to add protocol stubs?
// struct NotWorking: GenericPAT {
//        ^
struct NotWorking: GenericPAT {
    func f<U>(u: U) -> some Collection { [0] }
}

Next code compiles as expected because type is explicit:

struct Working1: GenericPAT {
    func f<U>(u: U) -> [Int] { [0] }
}

On the forums I discuss this topic and @groue verified it but with a different error note that I don't see in my Xcode 13 (different version maybe?). And just at the beginning there is a simplified implementation where I got the error and to reproduce it too.


Working code

This is some code to verify that the compiler infers the type in other similar cases:

struct Working2 {
    func f<U>(u: U) -> some Collection { [0] }
}

protocol NonGenericPAT {
    associatedtype T
    func f() -> T
}

extension Working2: NonGenericPAT {
    func f() -> some Collection { [0] }
}
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

It is stated in SE-0244 that associated type inference can only infer an opaque result type for a non-generic requirement, because the opaque type is parameterized by the function's own generic arguments. Here's an analogy without opaque types:

protocol P {
  associatedtype A
  func foo<T>(t: T) -> A
}

struct S: P {
  // This will not work because 'T' only exists in the context of 'foo'. 
  // Note that because of this, neither would you be able to explicitly
  // implement 'A' in terms of 'T' using a typealias.
  func foo<T>(t: T) -> T { t }
}

@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. labels Jun 6, 2022
@AnthonyLatsis
Copy link
Collaborator

@slavapestov Should we close this as not planned or leave it open as a feature request?

@slavapestov
Copy link
Member

Not planned, because it would be unsound to do so.

@slavapestov slavapestov closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
associated type inference compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

3 participants