Skip to content

Commit 21ee134

Browse files
authored
Merge pull request #937 from ahoppen/ahoppen/review-comments-2
Address review comments from merged PRs
2 parents ee10cb1 + 75c50f8 commit 21ee134

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,14 @@ extension Parser {
15921592
_ handle: RecoveryConsumptionHandle
15931593
) -> RawSubscriptDeclSyntax {
15941594
let (unexpectedBeforeSubscriptKeyword, subscriptKeyword) = self.eat(handle)
1595+
1596+
let unexpectedName: RawTokenSyntax?
1597+
if self.at(.identifier) && self.peek().starts(with: "<") || self.peek().tokenKind == .leftParen {
1598+
unexpectedName = self.consumeAnyToken()
1599+
} else {
1600+
unexpectedName = nil
1601+
}
1602+
15951603
let genericParameterClause: RawGenericParameterClauseSyntax?
15961604
if self.currentToken.starts(with: "<") {
15971605
genericParameterClause = self.parseGenericParameters()
@@ -1624,6 +1632,7 @@ extension Parser {
16241632
modifiers: attrs.modifiers,
16251633
unexpectedBeforeSubscriptKeyword,
16261634
subscriptKeyword: subscriptKeyword,
1635+
RawUnexpectedNodesSyntax([unexpectedName], arena: self.arena),
16271636
genericParameterClause: genericParameterClause,
16281637
indices: indices,
16291638
result: result,

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
305305
if shouldSkip(node) {
306306
return .skipChildren
307307
}
308+
if let unexpected = node.unexpectedBetweenSubscriptKeywordAndGenericParameterClause,
309+
let nameTokens = unexpected.onlyTokens(satisfying: { !$0.tokenKind.isKeyword }) {
310+
addDiagnostic(unexpected, .subscriptsCannotHaveNames, fixIts: [
311+
FixIt(message: RemoveTokensFixIt(tokensToRemove: nameTokens), changes: .makeMissing(tokens: nameTokens))
312+
], handledNodes: [unexpected.id])
313+
}
308314
if let unexpected = node.indices.unexpectedBeforeLeftParen,
309315
let nameTokens = unexpected.onlyTokens(satisfying: { !$0.tokenKind.isKeyword }) {
310316
addDiagnostic(unexpected, .subscriptsCannotHaveNames, fixIts: [

Sources/SwiftParser/Expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ extension Parser {
11621162
public mutating func parseIdentifierExpression() -> RawExprSyntax {
11631163
let (name, args) = self.parseDeclNameRef(.compoundNames)
11641164
guard self.lookahead().canParseAsGenericArgumentList() else {
1165-
if name.tokenText.hasPrefix("<#") && name.tokenText.hasSuffix("#>") && args == nil {
1165+
if name.tokenText.isEditorPlaceholder && args == nil {
11661166
return RawExprSyntax(
11671167
RawEditorPlaceholderExprSyntax(
11681168
identifier: name,

Sources/SwiftParser/Lexer.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public struct Lexer {
5252
}
5353

5454
var isEditorPlaceholder: Bool {
55-
return self.tokenKind == .identifier &&
56-
self.tokenText.starts(with: SyntaxText("<#")) &&
57-
self.tokenText.hasSuffix(SyntaxText("#>"))
55+
return self.tokenKind == .identifier && self.tokenText.isEditorPlaceholder
5856
}
5957

6058
@_spi(RawSyntax)

Sources/SwiftParser/SyntaxUtils.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,26 @@ extension RawUnexpectedNodesSyntax {
2424
})
2525
}
2626

27-
/// If `tokens` is not empty, construct a `RawUnexpectedNodesSyntax`
27+
/// If `nodes` is not empty, construct a `RawUnexpectedNodesSyntax`
2828
/// containing those tokens, otherwise return `nil`.
29-
init?(_ tokens: [RawTokenSyntax], arena: SyntaxArena) {
30-
if tokens.isEmpty {
29+
init?<SyntaxType: RawSyntaxNodeProtocol>(_ nodes: [SyntaxType], arena: SyntaxArena) {
30+
if nodes.isEmpty {
3131
return nil
3232
} else {
33-
self.init(elements: tokens.map(RawSyntax.init), arena: arena)
33+
self.init(elements: nodes.map(RawSyntax.init), arena: arena)
3434
}
3535
}
36+
37+
/// If `nodes` contains non-`nil` values, construct a `RawUnexpectedNodesSyntax`
38+
/// containing those tokens, otherwise return `nil`.
39+
init?<SyntaxType: RawSyntaxNodeProtocol>(_ nodes: [SyntaxType?], arena: SyntaxArena) {
40+
self.init(nodes.compactMap({ $0 }), arena: arena)
41+
}
42+
}
43+
44+
extension SyntaxText {
45+
var isEditorPlaceholder: Bool {
46+
return self.starts(with: SyntaxText("<#")) &&
47+
self.hasSuffix(SyntaxText("#>"))
48+
}
3649
}

Tests/SwiftParserTest/translated/SubscriptingTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,13 @@ final class SubscriptingTests: XCTestCase {
422422
""",
423423
diagnostics: [
424424
DiagnosticSpec(locationMarker: "1️⃣", message: "subscripts cannot have a name", fixIts: ["remove 'x'"]),
425-
DiagnosticSpec(locationMarker: "2️⃣", message: "subscripts cannot have a name", fixIts: ["remove 'x<T>'"]),
425+
DiagnosticSpec(locationMarker: "2️⃣", message: "subscripts cannot have a name", fixIts: ["remove 'x'"]),
426426
], fixedSource: """
427427
struct A10 {
428428
subscript (i: Int) -> Int {
429429
return 0
430430
}
431-
subscript (i: T) -> Int {
431+
subscript <T>(i: T) -> Int {
432432
return 0
433433
}
434434
}
@@ -440,7 +440,7 @@ final class SubscriptingTests: XCTestCase {
440440
AssertParse(
441441
"""
442442
struct A11 {
443-
subscript 1️⃣x y : 2️⃣Int 3️⃣-> Int 4️⃣{
443+
subscript 1️⃣x y : 2️⃣Int 3️⃣-> Int 4️⃣{
444444
return 0
445445
}
446446
}

0 commit comments

Comments
 (0)