Skip to content

Cannot witness a subscript with a typed error #78379

Closed
@ctxppc

Description

@ctxppc

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

No one assigned

    Labels

    accessorsFeature → declarations: Variable (property) accessorsbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesdeclarationsFeature: declarationsswift 6.1throws & rethrowsFeature → error handling: throws & rethrowstype checkerArea → compiler: Semantic analysistyped throwsFeature → error handling → throws & rethrows: Typed throwsunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions