Skip to content

Associated type is lost when using opaque return type #81036

@jeremyabannister

Description

@jeremyabannister

Description

The code I've included does not compile, though I believe it should. Switching some to any in the return type of losesTypeInfoButShouldnt() causes the code to compile.

Reproduction

protocol Foo<T> {
    associatedtype T
    var content: T { get }
}

final class Bar<T>: Foo {
    init() throws { }
    var content: T { fatalError() }
}

extension Foo where T: Sequence {
    // If you switch `some` to `any` in the return type then it compiles
    func losesTypeInfoButShouldnt() -> some Foo<T.Element> {
        try! Bar<T.Element>()
    }
}

func demo(bar: Bar<[Int]>) {
    let _: Int = bar.losesTypeInfoButShouldnt().content
}

Expected behavior

I expect it to compile

Environment

Xcode 16.3, Swift 6.1

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions