-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
accessorsFeature → declarations: Variable (property) accessorsFeature → declarations: Variable (property) accessorsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesFeature → protocol: protocol conformancesdeclarationsFeature: declarationsFeature: declarationsswift 6.1throws & rethrowsFeature → error handling: throws & rethrowsFeature → error handling: throws & rethrowstype checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysistyped throwsFeature → error handling → throws & rethrows: Typed throwsFeature → error handling → throws & rethrows: Typed throwsunexpected errorBug: Unexpected errorBug: Unexpected error
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
accessorsFeature → declarations: Variable (property) accessorsFeature → declarations: Variable (property) accessorsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesFeature → protocol: protocol conformancesdeclarationsFeature: declarationsFeature: declarationsswift 6.1throws & rethrowsFeature → error handling: throws & rethrowsFeature → error handling: throws & rethrowstype checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysistyped throwsFeature → error handling → throws & rethrows: Typed throwsFeature → error handling → throws & rethrows: Typed throwsunexpected errorBug: Unexpected errorBug: Unexpected error