Skip to content

Swift 5.7: incorrect compiler error with opaque result types and primary associated types #59391

Closed
@groue

Description

@groue

Describe the bug

The compiler produces an error when it should not, in a context that involves opaque result types and primary associated types

To Reproduce

Steps to reproduce the behavior:

  1. Compile this Swift snippet with Xcode 14 beta:

    protocol Cursor<Element> {
        associatedtype Element
    }
    
    extension Cursor {
        func flatMap1<SegmentOfResult>(_ transform: @escaping (Element) throws -> SegmentOfResult)
        -> some Cursor<SegmentOfResult.Element>
        where SegmentOfResult: Cursor
        {
            // ❌ Return type of instance method 'flatMap1' requires that
            // 'SegmentOfResult.Element' conform to '(some Cursor).Element'
            map(transform).joined()
        }
    
        func flatMap2<T>(_ transform: @escaping (Element) throws -> some Cursor<T>)
        -> some Cursor<T>
        {
            // ❌ Return type of instance method 'flatMap2' requires that 'T' conform
            // to '(some Cursor).Element'
            map(transform).joined()
        }
    
        func map<T>(_ transform: @escaping (Element) throws -> T) -> some Cursor<T> {
            fatalError("not implemented")
        }
    }
    
    extension Cursor where Element: Cursor {
        func joined() -> some Cursor<Element.Element> {
            fatalError("not implemented")
        }
    }
  2. See the erroneous compiler errors on the flatMap1 and flatMap2 methods.

Expected behavior

  1. flatMap1 should compile
  2. flatMap2 should probably compile as well (but I'm less comfortable with closure arguments that return some, so I'm less sure).

Environment (please complete the following information):

  • OS: macOS 12.4
  • Xcode Version 14.0 beta (14A5228q)

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfgenericsFeature: generic declarations and typesopaque result typesFeature → types → opaque types: opaque result typesopaque typesFeature → types: opaque typesparameterized protocolsFeature → protocol: protocols with primary associated typesprotocolFeature → type declarations: Protocol declarationsswift 6.2type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions