Skip to content

Commit dfab212

Browse files
committed
[Parser] @unknown default at statement level
1 parent 229d5dc commit dfab212

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Sources/SwiftParser/TopLevel.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,6 @@ extension Parser {
188188
} else if self.atStartOfExpression() {
189189
item = .expr(self.parseExpression(flavor: .basic, pattern: .none))
190190
attachSemi = true
191-
} else if (self.at(.atSign) && peek(isAt: .identifier)) || self.at(anyIn: DeclarationModifier.self) != nil {
192-
// Force parsing '@<identifier>' as a declaration, as there's no valid
193-
// expression or statement starting with an attribute.
194-
item = .decl(self.parseDeclaration())
195-
attachSemi = true
196191
} else if self.withLookahead({ $0.atStartOfSwitchCase() }) {
197192
// 'case' and 'default' are invalid in code block items.
198193
// Parse them and put them in their own CodeBlockItem but as an unexpected node.
@@ -203,6 +198,11 @@ extension Parser {
203198
semicolon: nil,
204199
arena: self.arena
205200
)
201+
} else if (self.at(.atSign) && peek(isAt: .identifier)) || self.at(anyIn: DeclarationModifier.self) != nil {
202+
// Force parsing '@<identifier>' as a declaration, as there's no valid
203+
// expression or statement starting with an attribute.
204+
item = .decl(self.parseDeclaration())
205+
attachSemi = true
206206
} else {
207207
// Otherwise, eat the unexpected tokens into an "decl".
208208
item = .decl(

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ final class StatementTests: ParserTestCase {
257257
)
258258
}
259259

260+
func testUnknownDefaultAtStatement() {
261+
assertParse(
262+
"""
263+
func test() {
264+
1️⃣@unknown default:
265+
return
266+
}
267+
""",
268+
diagnostics: [
269+
DiagnosticSpec(message: "'default' label can only appear inside a 'switch' statement")
270+
]
271+
)
272+
}
273+
260274
func testMissingIfClauseIntroducer() {
261275
assertParse("if _ = 42 {}")
262276
}

0 commit comments

Comments
 (0)