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

Associated Type inference regression with default types #72190

Open
adam-fowler opened this issue Mar 8, 2024 · 0 comments
Open

Associated Type inference regression with default types #72190

adam-fowler opened this issue Mar 8, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@adam-fowler
Copy link

Description

The following code, with a protocol that specialises another protocol with three associated types by providing a default type for the second associated type in the case the first associated type is a certain value compiles when using Swift 5.10. The nightly from 1st March fails to infer the types and cannot compile the code.

Reproduction

protocol PB<A> {
    associatedtype A
}

protocol PC<B> {
    associatedtype B: PB
}

protocol PD {
    associatedtype A
    associatedtype B: PB<A>
    associatedtype C: PC<B>

    func test() -> C
}

protocol PDD: PD where A == Int {
    associatedtype B = SB<Int>
}

struct SB<A>: PB {}

struct SC<B: PB>: PC {}

struct SDD: PDD {
    func test() -> some PC<B> {
        return SC<B>()
    }
}

In Swift 6 produces the following errors

/tmp/vscode-swift/Untitled-15bef939ba85f8.swift:26:28: error: reference to invalid associated type 'B' of type 'SDD'
24 │ 
25 │ struct SDD: PDD {
26 │     func test() -> some PC<B> {
   │                            ╰─ error: reference to invalid associated type 'B' of type 'SDD'
27 │         return SC<B>()
28 │     }

/tmp/vscode-swift/Untitled-15bef939ba85f8.swift:26:20: error: 'some' types are only permitted in properties, subscripts, and functions
24 │ 
25 │ struct SDD: PDD {
26 │     func test() -> some PC<B> {
   │                    ╰─ error: 'some' types are only permitted in properties, subscripts, and functions
27 │         return SC<B>()
28 │     }

/tmp/vscode-swift/Untitled-15bef939ba85f8.swift:25:8: error: type 'SDD' does not conform to protocol 'PDD'
23 │ struct SC<B: PB>: PC {}
24 │ 
25 │ struct SDD: PDD {
   │        ╰─ error: type 'SDD' does not conform to protocol 'PDD'
26 │     func test() -> some PC<B> {
27 │         return SC<B>()

/tmp/vscode-swift/Untitled-15bef939ba85f8.swift:25:8: error: type 'SDD' does not conform to protocol 'PD'
 8 │ 
 9 │ protocol PD {
10 │     associatedtype A
   │                    ╰─ note: protocol requires nested type 'A'; add nested type 'A' for conformance
11 │     associatedtype B: PB<A>
12 │     associatedtype C: PC<B>
   │                    ╰─ note: protocol requires nested type 'C'; add nested type 'C' for conformance
13 │ 
14 │     func test() -> C
   ┆
16 │ 
17 │ protocol PDD: PD where A == Int {
18 │     associatedtype B = SB<Int>
   │                    ╰─ note: protocol requires nested type 'B'; add nested type 'B' for conformance
19 │ }
20 │ 
   ┆
23 │ struct SC<B: PB>: PC {}
24 │ 
25 │ struct SDD: PDD {
   │        ╰─ error: type 'SDD' does not conform to protocol 'PD'
26 │     func test() -> some PC<B> {
27 │         return SC<B>()

Expected behavior

Code compiles

Environment

Swift version 6.0-dev (LLVM aa5a99efe7c5734, Swift 2f6e9b3)
Target: x86_64-unknown-linux-gnu

Additional information

No response

@adam-fowler adam-fowler added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 8, 2024
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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant