Skip to content

Commit 71ef7bf

Browse files
committed
Disallow Self as struct names
1 parent 6948083 commit 71ef7bf

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-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: 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)