Skip to content

Typed throws syntax fails to parse without explicit return type in protocol method #84148

@yikesboy

Description

@yikesboy

Description

When using typed throws syntax in a protocol method without an explicit return type, the Swift compiler fails to resolve the generic type parameter. When compiling it reports cannot find type 'E' in scope even though E is properly declared as a generic parameter.

Reproduction

The issue arises in the Stmt protocol.

protocol StmtVisitor<ErrorType> {
    associatedtype ErrorType: Error
    func visit(_ expression: Expression) throws(ErrorType)
    func visit(_ print: Print) throws(ErrorType)
}

protocol Stmt {
    func accept<E: Error>(_ visitor: any StmtVisitor<E>) throws(E)
}

Expected behavior

The code should compile successfully with the return type implicitly being Void

Environment

Swift version 6.1.2 (swift-6.1.2-RELEASE)
Target: x86_64-unknown-linux-gnu

Arch Linux

Additional information

Workaround
Explicitly set the return type of the protocol method as Void.

protocol Stmt {
    func accept<E: Error>(_ visitor: any StmtVisitor<E>) throws(E) -> Void
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions