8000 Improve diagnostics for 'let…else' without 'guard' · Issue #81728 · swiftlang/swift · GitHub
[go: up one dir, main page]

Skip to content

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

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

Open
jrose-signal opened this issue May 23, 2025 · 4 comments
Open

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

jrose-signal opened this issue May 23, 2025 · 4 comments
Labels
8000 compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation feature A feature request or implementation parser Area → compiler: The legacy C++ parser swift 6.2

Comments

@jrose-signal
Copy link

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

@jrose-signal jrose-signal added feature A feature request or implementation triage needed This issue needs more specific labels labels May 23, 2025
@AnthonyLatsis AnthonyLatsis added parser Area → compiler: The legacy C++ parser compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation swift 6.2 and removed triage needed This issue needs more specific labels labels May 26, 2025
@AnthonyLatsis
Copy link
Collaborator

New parser:

stdin:1:37: error: extraneous code at top level
1 | let specific = general as? Specific else {
  |                                     `- error: extraneous code at top level
2 |   return nil
3 | }

@jrose-signal
Copy link
Author

Ah, I meant in a function body, at top level in a non-script file that's probably reasonable.

@AnthonyLatsis
Copy link
Collaborator
2 | func f() {
3 |   let specific = general as? Specific else {
  |                                       `- error: unexpected code in function
4 |     return nil
5 |   }

@hamishknight Does the new parser have a parse-as-library setting?

@hamishknight
Copy link
Contributor

Does the new parser have a parse-as-library setting?

Not AFAIK, parsing a SourceFileSyntax always just allows top-level code currently. The "extraneous code at top level" error is because it's failing to parse else as anything useful and just treating it as unexpected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation feature A feature request or implementation parser Area → compiler: The legacy C++ parser swift 6.2
Projects
None yet
Development

No branches or pull requests

3 participants
0