Skip to content

Commit 1e61cc3

Browse files
authored
Merge pull request #1124 from ahoppen/ahoppen/more-diags
Implement a few more diagnostics in SwiftParser
2 parents f9985dd + 71ef7bf commit 1e61cc3

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed

Sources/SwiftParser/Nominals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ extension Parser {
150150
introucerHandle: RecoveryConsumptionHandle
151151
) -> T where T: NominalTypeDeclarationTrait {
152152
let (unexpectedBeforeIntroducerKeyword, introducerKeyword) = self.eat(introucerHandle)
153-
let (unexpectedBeforeName, name) = self.expectIdentifier(keywordRecovery: true)
153+
let (unexpectedBeforeName, name) = self.expectIdentifier(allowIdentifierLikeKeywords: false, keywordRecovery: true)
154154
if unexpectedBeforeName == nil && name.isMissing && self.currentToken.isAtStartOfLine {
155155
return T.init(
156156
attributes: attrs.attributes,

Tests/SwiftParserTest/Assertions.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ func AssertParse(
443443
_ markedSource: String,
444444
substructure expectedSubstructure: Syntax? = nil,
445445
substructureAfterMarker: String = "START",
446+
substructureCheckTrivia: Bool = false,
446447
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
447448
applyFixIts: [String]? = nil,
448449
fixedSource expectedFixedSource: String? = nil,
@@ -454,6 +455,7 @@ func AssertParse(
454455
{ SourceFileSyntax.parse(from: &$0) },
455456
substructure: expectedSubstructure,
456457
substructureAfterMarker: substructureAfterMarker,
458+
substructureCheckTrivia: substructureCheckTrivia,
457459
diagnostics: expectedDiagnostics,
458460
applyFixIts: applyFixIts,
459461
fixedSource: expectedFixedSource,
@@ -470,6 +472,7 @@ func AssertParse<S: SyntaxProtocol>(
470472
_ parse: (inout Parser) -> S,
471473
substructure expectedSubstructure: Syntax? = nil,
472474
substructureAfterMarker: String = "START",
475+
substructureCheckTrivia: Bool = false,
473476
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
474477
applyFixIts: [String]? = nil,
475478
fixedSource expectedFixedSource: String? = nil,
@@ -484,6 +487,7 @@ func AssertParse<S: SyntaxProtocol>(
484487
},
485488
substructure: expectedSubstructure,
486489
substructureAfterMarker: substructureAfterMarker,
490+
substructureCheckTrivia: substructureCheckTrivia,
487491
diagnostics: expectedDiagnostics,
488492
applyFixIts: applyFixIts,
489493
fixedSource: expectedFixedSource,
@@ -515,6 +519,7 @@ func AssertParse<S: SyntaxProtocol>(
515519
_ parse: (String) -> S,
516520
substructure expectedSubstructure: Syntax? = nil,
517521
substructureAfterMarker: String = "START",
522+
substructureCheckTrivia: Bool = false,
518523
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
519524
applyFixIts: [String]? = nil,
520525
fixedSource expectedFixedSource: String? = nil,
@@ -545,7 +550,7 @@ func AssertParse<S: SyntaxProtocol>(
545550
if let expectedSubstructure = expectedSubstructure {
546551
let subtreeMatcher = SubtreeMatcher(Syntax(tree), markers: markerLocations)
547552
do {
548-
try subtreeMatcher.assertSameStructure(afterMarker: substructureAfterMarker, Syntax(expectedSubstructure), file: file, line: line)
553+
try subtreeMatcher.assertSameStructure(afterMarker: substructureAfterMarker, Syntax(expectedSubstructure), includeTrivia: substructureCheckTrivia, file: file, line: line)
549554
} catch {
550555
XCTFail("Matching for a subtree failed with error: \(error)", file: file, line: line)
551556
}

Tests/SwiftParserTest/translated/DollarIdentifierTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ final class DollarIdentifierTests: XCTestCase {
4949
}
5050
""",
5151
diagnostics: [
52-
DiagnosticSpec(message: "'$' is not a valid identifier")
53-
// TODO: Old parser expected error on line 3: '$' is not an identifier; use backticks to escape it, Fix-It replacements: 9 - 10 = '`$`'
52+
DiagnosticSpec(message: "'$' is not a valid identifier", fixIts: ["if this name is unavoidable, use backticks to escape it"])
5453
]
5554
)
5655
}
@@ -118,7 +117,6 @@ final class DollarIdentifierTests: XCTestCase {
118117
$(1️⃣$: 24)
119118
""",
120119
diagnostics: [
121-
// TODO: Old parser expected error on line 3: '$' is not an identifier; use backticks to escape it, Fix-It replacements: 3 - 4 = '`$`'
122120
DiagnosticSpec(message: "'$' is not a valid identifier")
123121
]
124122
)
@@ -171,7 +169,6 @@ final class DollarIdentifierTests: XCTestCase {
171169
}
172170
""",
173171
diagnostics: [
174-
// TODO: Old parser expected error on line 3: expected expression
175172
DiagnosticSpec(message: "unexpected code in function")
176173
]
177174
)

Tests/SwiftParserTest/translated/HashbangLibraryTests.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,47 @@
1212

1313
// This test file has been translated from swift/test/Parse/hashbang_library.swift
1414

15+
import SwiftSyntax
16+
1517
import XCTest
1618

1719
final class HashbangLibraryTests: XCTestCase {
1820
func testHashbangLibrary1() {
21+
// Check that we diagnose and skip the hashbang at the beginning of the file
22+
// when compiling in library mode.
1923
AssertParse(
2024
"""
21-
#!/usr/bin/swift
25+
#!/usr/bin/swift
2226
class Foo {}
23-
// Check that we diagnose and skip the hashbang at the beginning of the file
24-
// when compiling in library mode.
2527
""",
26-
diagnostics: [
27-
// TODO: Old parser expected error on line 1: hashbang line is allowed only in the main file
28-
]
28+
substructure: Syntax(
29+
SourceFileSyntax(
30+
statements: CodeBlockItemListSyntax([
31+
CodeBlockItemSyntax(
32+
item: .decl(
33+
DeclSyntax(
34+
ClassDeclSyntax(
35+
classKeyword: .keyword(
36+
.class,
37+
leadingTrivia: [
38+
.shebang("#!/usr/bin/swift"),
39+
.newlines(1),
40+
],
41+
trailingTrivia: .space
42+
),
43+
identifier: .identifier("Foo", trailingTrivia: .space),
44+
members: MemberDeclBlockSyntax(
45+
members: MemberDeclListSyntax([])
46+
)
47+
)
48+
)
49+
)
50+
)
51+
]),
52+
eofToken: .eof()
53+
)
54+
),
55+
substructureCheckTrivia: true
2956
)
3057
}
31-
3258
}

Tests/SwiftParserTest/translated/IdentifiersTests.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,29 @@ final class IdentifiersTests: XCTestCase {
110110
func testIdentifiers8b() {
111111
AssertParse(
112112
"""
113-
struct Self {}
113+
struct 1️⃣Self {}
114114
""",
115115
diagnostics: [
116-
// TODO: Old parser expected error on line 3: keyword 'Self' cannot be used as an identifier here
117-
// TODO: Old parser expected note on line 3: if this name is unavoidable, use backticks to escape it, Fix-It replacements: 8 - 12 = '`Self`'
116+
DiagnosticSpec(message: "keyword 'Self' cannot be used as an identifier here")
117+
]
118+
)
119+
}
120+
121+
func testStructNamedLowercaseAny() {
122+
AssertParse(
123+
"""
124+
struct any {}
125+
"""
126+
)
127+
}
128+
129+
func testStructNamedCapitalAny() {
130+
AssertParse(
131+
"""
132+
struct 1️⃣Any {}
133+
""",
134+
diagnostics: [
135+
DiagnosticSpec(message: "keyword 'Any' cannot be used as an identifier here")
118136
]
119137
)
120138
}

0 commit comments

Comments
 (0)