Closed
Description
Description
A protocol containing a subscript
requirement with a get throws(…)
accessor cannot be implemented. The compiler diagnoses a “Type ‘X’ does not conform to protocol ‘Y’” error, even if the implementing subscript
provides the exact same accessor as in the requirement.
Reproduction
protocol InfiniteCollectionA {
associatedtype Element
associatedtype Failure : Error
subscript (index: Int) -> Element {
get throws(Failure)
}
}
struct IdentityCollectionA : InfiniteCollectionA { // 🛑 Type 'IdentityCollectionA' does not conform to protocol 'InfiniteCollectionA'
typealias Failure = any Error // or any concrete Error type
subscript (index: Int) -> Int {
get throws(Failure) {
index
}
}
}
protocol InfiniteCollectionB {
associatedtype Element
associatedtype Failure : Error
func element(at index: Int) throws(Failure) -> Element
}
struct IdentityCollectionB : InfiniteCollectionB {
typealias Failure = any Error // or any concrete Error type
func element(at index: Int) throws(Failure) -> Int { // ✅ compiles fine
index
}
}
Expected behavior
Preferably, the compiler should accept the implementing accessor as a suitable witness for the requirement.
Alternatively, the compiler should report that subscript accessor requirements cannot use typed errors (and use untyped errors instead).
Environment
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0
Additional information
Typed throws were proposed in SE-0413.
Metadata
Metadata
Assignees
Labels
Feature → declarations: Variable (property) accessorsA deviation from expected or documented behavior. Also: expected but undesirable behavior.The Swift compiler itselfFeature → protocol: protocol conformancesFeature: declarationsFeature → error handling: throws & rethrowsArea → compiler: Semantic analysisFeature → error handling → throws & rethrows: Typed throwsBug: Unexpected error