Skip to content

Commit da77040

Browse files
committed
Fix bug when recovering to an accessor introducer
# Conflicts: # Sources/SwiftParser/Declarations.swift # Tests/SwiftParserTest/StatementTests.swift
1 parent bc36c5a commit da77040

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@ extension Parser {
15201520
var attributes: RawAttributeListSyntax?
15211521
var modifier: RawDeclModifierSyntax?
15221522
var kind: AccessorKind
1523+
var unexpectedBeforeToken: RawUnexpectedNodesSyntax?
15231524
var token: RawTokenSyntax
15241525
}
15251526

@@ -1528,7 +1529,7 @@ extension Parser {
15281529
var look = self.lookahead()
15291530
let _ = look.consumeAttributeList()
15301531
let hasModifier = look.consume(ifAny: [], contextualKeywords: ["mutating", "nonmutating", "__consuming"]) != nil
1531-
guard let (kind, handle) = look.at(anyIn: AccessorKind.self) else {
1532+
guard let (kind, _) = look.at(anyIn: AccessorKind.self) else {
15321533
return nil
15331534
}
15341535

@@ -1547,11 +1548,12 @@ extension Parser {
15471548
modifier = nil
15481549
}
15491550

1550-
let introducer = self.eat(handle)
1551+
let (unexpectedBeforeIntroducer, introducer) = self.expect(kind.rawTokenKind)
15511552
return AccessorIntroducer(
15521553
attributes: attrs,
15531554
modifier: modifier,
15541555
kind: kind,
1556+
unexpectedBeforeToken: unexpectedBeforeIntroducer,
15551557
token: introducer
15561558
)
15571559
}
@@ -1703,6 +1705,7 @@ extension Parser {
17031705
RawAccessorDeclSyntax(
17041706
attributes: introducer.attributes,
17051707
modifier: introducer.modifier,
1708+
introducer.unexpectedBeforeToken,
17061709
accessorKind: introducer.token,
17071710
parameter: parameter,
17081711
asyncKeyword: asyncKeyword,

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ final class DeclarationTests: XCTestCase {
759759
}
760760
""",
761761
diagnostics: [
762-
DiagnosticSpec(message: "unexpected code 'bogus rethrows set' in variable")
762+
DiagnosticSpec(message: "found an unexpected second identifier in accessor")
763763
]
764764
)
765765
}

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,4 +603,20 @@ final class StatementTests: XCTestCase {
603603
)
604604
)
605605
}
606+
607+
func testRecoveryInFrontOfAccessorIntroducer() {
608+
AssertParse(
609+
"""
610+
subscript(1️⃣{@2️⃣self3️⃣ _modify
611+
""",
612+
diagnostics: [
613+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected type and ')' to end parameter clause"),
614+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '->' and return type in subscript"),
615+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in attribute"),
616+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in accessor"),
617+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end subscript"),
618+
DiagnosticSpec(locationMarker: "3️⃣", message: "consecutive statements on a line must be separated by ';'"),
619+
]
620+
)
621+
}
606622
}

Tests/SwiftParserTest/translated/EffectfulPropertiesTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ final class EffectfulPropertiesTests: XCTestCase {
271271
""",
272272
diagnostics: [
273273
// TODO: Old parser expected error on line 2: expected '{' to start getter definition
274-
DiagnosticSpec(message: "unexpected code 'bogus rethrows {}' in variable")
274+
DiagnosticSpec(message: "found an unexpected second identifier in accessor")
275275
]
276276
)
277277
}
@@ -331,7 +331,7 @@ final class EffectfulPropertiesTests: XCTestCase {
331331
""",
332332
diagnostics: [
333333
// TODO: Old parser expected error on line 2: expected get or set in a protocol property
334-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'bogus rethrows set' in variable"),
334+
DiagnosticSpec(locationMarker: "1️⃣", message: "found an unexpected second identifier in accessor"),
335335
// TODO: Old parser expected error on line 3: only function declarations may be marked 'rethrows'; did you mean 'throws'?
336336
// TODO: Old parser expected error on line 3: expected get or set in a protocol property
337337
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'bogus set' in variable"),

0 commit comments

Comments
 (0)