Skip to content

[SR-2605] Cascading failures trying to parse if <expr> else #45210

@jckarter

Description

@jckarter
Previous ID SR-2605
Radar None
Original Reporter @jckarter
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, Parser, StarterBug
Assignee @modocache
Priority Medium

md5: 4700a3a0330dc628a2226b9d21b6eb4b

Issue Description:

If you're refactoring a guard into an if, it's easy to accidentally end up with invalid code like this in the transition:

if condition else {
}

The parser rightly complains about the else, but has poor recovery afterward, interpreting the following brace as a closure, which may in turn lead to other unhelpful errors after:

/Users/jgroff/src/s/swift/stdlib/public/core/Optional.swift:517:32: error: expected '{' after 'if' condition
    if source === _nilSentinel else {
                               ^
/Users/jgroff/src/s/swift/stdlib/public/core/Optional.swift:517:37: error: braced block of statements is an unused closure
    if source === _nilSentinel else {
                                    ^

It might be friendlier to recognize this pattern and instead recover as if the else wasn't there, or as if there were an empty block before it.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationgood first issueGood for newcomersparserArea → compiler: The legacy C++ parser

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions