Skip to content

Improve diagnostics for 'let…else' without 'guard' #81728

Open
@jrose-signal

Description

@jrose-signal

Motivation

The following code produces a generic "consecutive statements on a line must be separated by ';'" error:

let specific = general as? Specific else {
  return nil
}

However, there's a reason somewhat might write this: this is the Rust syntax for pattern-match-or-exit-scope (though in Rust it's closer to guard case than guard let). Even without that justification, else is not normally the start of a statement anyway, so better recovery is possible.

Proposed solution

If a let or var decl is followed by else, ideally recover as if the user had written guard let instead. If that's not possible, at least add a dedicated diagnostic for the else rather than just "consecutive statements…" followed by "expected expression".

Alternatives considered

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationfeatureA feature request or implementationparserArea → compiler: The legacy C++ parserswift 6.2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions