Skip to content

Commit 4fa50f6

Browse files
committed
Disallow Self as struct names
1 parent bca0994 commit 4fa50f6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
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/translated/IdentifiersTests.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,22 @@ final class IdentifiersTests: XCTestCase {
109109
func testIdentifiers8b() {
110110
AssertParse(
111111
"""
112-
struct Self {}
112+
struct 1️⃣Self {}
113113
""",
114114
diagnostics: [
115-
// TODO: Old parser expected error on line 3: keyword 'Self' cannot be used as an identifier here
116-
// TODO: Old parser expected note on line 3: if this name is unavoidable, use backticks to escape it, Fix-It replacements: 8 - 12 = '`Self`'
115+
DiagnosticSpec(message: "keyword 'Self' cannot be used as an identifier here")
117116
]
118117
)
119118
}
120119

120+
func testStructNamedAny() {
121+
AssertParse(
122+
"""
123+
struct any {}
124+
"""
125+
)
126+
}
127+
121128
func testIdentifiers8c() {
122129
AssertParse(
123130
"""

0 commit comments

Comments
 (0)