Skip to content

Commit 88b7ba6

Browse files
committed
Fix bug when recovering to an accessor introducer
1 parent 5d455f4 commit 88b7ba6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,7 @@ extension Parser {
15811581
var attributes: RawAttributeListSyntax?
15821582
var modifier: RawDeclModifierSyntax?
15831583
var kind: AccessorKind
1584+
var unexpectedBeforeToken: RawUnexpectedNodesSyntax?
15841585
var token: RawTokenSyntax
15851586
}
15861587

@@ -1591,7 +1592,7 @@ extension Parser {
15911592
var look = self.lookahead()
15921593
let _ = look.consumeAttributeList()
15931594
let hasModifier = look.consume(if: .keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming)) != nil
1594-
guard let (kind, handle) = look.at(anyIn: AccessorKind.self) ?? forcedKind else {
1595+
guard let (kind, _) = look.at(anyIn: AccessorKind.self) ?? forcedKind else {
15951596
return nil
15961597
}
15971598

@@ -1612,11 +1613,12 @@ extension Parser {
16121613
modifier = nil
16131614
}
16141615

1615-
let introducer = self.eat(handle)
1616+
let (unexpectedBeforeIntroducer, introducer) = self.expect(kind.spec)
16161617
return AccessorIntroducer(
16171618
attributes: attrs,
16181619
modifier: modifier,
16191620
kind: kind,
1621+
unexpectedBeforeToken: unexpectedBeforeIntroducer,
16201622
token: introducer
16211623
)
16221624
}
@@ -1673,6 +1675,7 @@ extension Parser {
16731675
return RawAccessorDeclSyntax(
16741676
attributes: introducer.attributes,
16751677
modifier: introducer.modifier,
1678+
introducer.unexpectedBeforeToken,
16761679
accessorKind: introducer.token,
16771680
parameter: parameter,
16781681
effectSpecifiers: effectSpecifiers,

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,19 @@ final class StatementTests: XCTestCase {
615615
]
616616
)
617617
}
618+
619+
func testRecoveryInFrontOfAccessorIntroducer() {
620+
AssertParse(
621+
"""
622+
subscript(1️⃣{@2️⃣self _modify3️⃣
623+
""",
624+
diagnostics: [
625+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected type and ')' to end parameter clause"),
626+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '->' and return type in subscript"),
627+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in attribute"),
628+
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected 'self' keyword in accessor"),
629+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '}' to end subscript"),
630+
]
631+
)
632+
}
618633
}

0 commit comments

Comments
 (0)