Skip to content

Method returning throws -> Never should be callable as throw method(), not try method() #69490

Open
@MahdiBM

Description

@MahdiBM

Method returning throws -> Never should be callable as throw method(), not try method().
This is not the case right now:

struct Err: Error { }

func logThenThrow() throws -> Never {
    ...
    throw Err()
}

throw logThenThrow() /// Error: Call can throw but is not marked with 'try'

func saveInDBThenThrow() async throws -> Never {
    ...
    throw Err()
}

throw await saveInDBThenThrow() /// Error: Call can throw but is not marked with 'try'

Motivation
This is useful in situations where you want to perform some work and only then throw.
Is is still possible right now using the try keyword instead of throw, but that syntax is not as clear as it should be on call-sites.

Solution
If a method returns Never but is still declared as throws, such as throws -> Never, swift compiler should require users to use the throw keyword behind the method name, instead of try.

Alternatives considered

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itselferror handlingfeatureA feature request or implementationstatementsFeature: statementsswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposalthrowFeature → error handling: Throw statements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions