Skip to content

Commit d18b675

Browse files
committed
Disallow Self as struct names
1 parent 7b87998 commit d18b675

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
@@ -109,11 +109,29 @@ 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")
116+
]
117+
)
118+
}
119+
120+
func testStructNamedLowercaseAny() {
121+
AssertParse(
122+
"""
123+
struct any {}
124+
"""
125+
)
126+
}
127+
128+
func testStructNamedCapitalAny() {
129+
AssertParse(
130+
"""
131+
struct 1️⃣Any {}
132+
""",
133+
diagnostics: [
134+
DiagnosticSpec(message: "keyword 'Any' cannot be used as an identifier here")
117135
]
118136
)
119137
}

0 commit comments

Comments
 (0)