Skip to content

Special case error messages if a single keyword is unexpected #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Diagnostics/SyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ extension SyntaxProtocol {
} else {
return "braces"
}
} else if let token = Syntax(self).as(UnexpectedNodesSyntax.self)?.onlyTokens(satisfying: { $0.tokenKind.isKeyword })?.only {
return "'\(token.text)' keyword"
} else if let token = Syntax(self).as(TokenSyntax.self) {
return "'\(token.text)' keyword"
} else if contentWithoutTrivia.contains("\n") || contentWithoutTrivia.count > 100 {
return "code"
} else {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftParserTest/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class TypeTests: XCTestCase {
print("closure with empty capture list")
}
""")

AssertParse("""
{ ()
throws -> Void in }
Expand All @@ -71,7 +71,7 @@ final class TypeTests: XCTestCase {
{ $0.parseClosureExpression() },
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in closure capture item"),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'class' in closure capture signature"),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected 'class' keyword in closure capture signature"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end closure"),
])

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/translated/EnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ final class EnumTests: XCTestCase {
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "keyword 'Self' cannot be used as an identifier here"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'Self' in enum"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected 'Self' keyword in enum"),
]
)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftParserTest/translated/ErrorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ final class ErrorsTests: XCTestCase {
diagnostics: [
// TODO: Old parser expected error on line 1: 'rethrows' may only occur before '->', Fix-It replacements: 43 - 43 = 'rethrows ', 46 - 55 = ''
DiagnosticSpec(locationMarker: "1️⃣", message: "expected 'rethrows' in function signature"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'rethrows' in function signature"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected 'rethrows' keyword in function signature"),
]
)
}
Expand Down Expand Up @@ -263,7 +263,7 @@ final class ErrorsTests: XCTestCase {
diagnostics: [
// TODO: Old parser expected error on line 3: consecutive statements on a line must be separated by ';'
// TODO: Old parser expected error on line 3: expected expression
DiagnosticSpec(message: "unexpected code 'throws' in function"),
DiagnosticSpec(message: "unexpected 'throws' keyword in function"),
]
)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/translated/IfconfigExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ final class IfconfigExprTests: XCTestCase {
}
""",
diagnostics: [
DiagnosticSpec(message: "unexpected code 'return' in conditional compilation block"),
DiagnosticSpec(message: "unexpected 'return' keyword in conditional compilation block"),
]
)
}
Expand Down