swift icon indicating copy to clipboard operation
swift copied to clipboard

Enhancing Diagnostics for Accessing Member Types within Existential Metatypes

Open chiragramani opened this issue 1 year ago • 0 comments

Description

Context: https://forums.swift.org/t/metatype-casting-and-member-access-type-ambiguity-error/71456

protocol Container {
    
}

protocol SpecialisedContainer: Container {
    associatedtype BaseContainer: Container
}

func repro<Root: Container>(root: Root.Type) {
    if let specialised = root as? any SpecialisedContainer.Type {
        let _ = specialised.BaseContainer.self  # Type of expression is ambiguous without a type annotation
        # let _: any Container.Type = specialised.BaseContainer.self # Type of expression is ambiguous without a type annotation
    }
}

Here, no type annotation would make it work:

Type of expression is ambiguous without a type annotation

Thanks to @slavapestov for sharing a helpful workaround and taking his suggestion, creating this issue to advocate for improved diagnostics for the above case.

Reproduction

Context: https://forums.swift.org/t/metatype-casting-and-member-access-type-ambiguity-error/71456

Expected behavior

Diagnostics talking about using a recommended way to open existentials to avoid the above issue.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)

Additional information

No response

chiragramani avatar May 03 '24 18:05 chiragramani