Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot witness a subscript with a typed error #78379

Open
ctxppc opened this issue Dec 29, 2024 · 0 comments
Open

Cannot witness a subscript with a typed error #78379

ctxppc opened this issue Dec 29, 2024 · 0 comments
Labels
accessors Feature → declarations: Variable (property) accessors bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself conformances Feature → protocol: protocol conformances declarations Feature: declarations swift 6.1 throws & rethrows Feature → error handling: throws & rethrows type checker Area → compiler: Semantic analysis typed throws Feature → error handling → throws & rethrows: Typed throws unexpected error Bug: Unexpected error

Comments

@ctxppc
Copy link

ctxppc commented Dec 29, 2024

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.

@ctxppc ctxppc added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Dec 29, 2024
ctxppc added a commit to ctxppc/DepthKit that referenced this issue Dec 29, 2024
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis conformances Feature → protocol: protocol conformances accessors Feature → declarations: Variable (property) accessors declarations Feature: declarations throws & rethrows Feature → error handling: throws & rethrows typed throws Feature → error handling → throws & rethrows: Typed throws swift 6.1 unexpected error Bug: Unexpected error and removed triage needed This issue needs more specific labels labels Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessors Feature → declarations: Variable (property) accessors bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself conformances Feature → protocol: protocol conformances declarations Feature: declarations swift 6.1 throws & rethrows Feature → error handling: throws & rethrows type checker Area → compiler: Semantic analysis typed throws Feature → error handling → throws & rethrows: Typed throws unexpected error Bug: Unexpected error
Projects
None yet
Development

No branches or pull requests

2 participants