Skip to content

Commit 95e8c60

Browse files
WIP: Don't store throwsSpecifier
1 parent 099fe0e commit 95e8c60

File tree

9 files changed

+36
-117
lines changed

9 files changed

+36
-117
lines changed

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,13 @@ public let COMMON_NODES: [Node] = [
132132
name: "DeinitEffectSpecifiers",
133133
nameForDiagnostics: "effect specifiers",
134134
kind: "Syntax",
135-
traits: [
136-
"EffectSpecifiers"
137-
],
135+
traits: [],
138136
children: [
139137
Child(
140138
name: "AsyncSpecifier",
141139
kind: .token(choices: [.keyword(text: "async")]),
142140
isOptional: true
143-
),
144-
Child(
145-
name: "ThrowsSpecifier",
146-
kind: .token(choices: []),
147-
isOptional: true
148-
),
141+
)
149142
]
150143
),
151144

Sources/SwiftParser/Specifiers.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ extension RawDeinitEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
488488
}
489489
}
490490

491-
/*
492491
public init(
493492
_ unexpectedBeforeAsyncSpecifier: RawUnexpectedNodesSyntax? = nil,
494493
asyncSpecifier: RawTokenSyntax?,
@@ -497,16 +496,22 @@ extension RawDeinitEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
497496
_ unexpectedAfterThrowsSpecifier: RawUnexpectedNodesSyntax? = nil,
498497
arena: __shared SyntaxArena
499498
) {
500-
assert(throwsSpecifier == nil)
501499
let unexpectedAfterAsync: RawUnexpectedNodesSyntax?
502-
if let afterThrows = unexpectedAfterThrowsSpecifier {
503-
if let beforeThrows = unexpectedBetweenAsyncSpecifierAndThrowsSpecifier {
504-
unexpectedAfterAsync = RawUnexpectedNodesSyntax(beforeThrows.elements + afterThrows.elements, arena: arena)
500+
if let throwsSpecifier = throwsSpecifier, throwsSpecifier.presence == .present {
501+
var unexpected: [RawSyntax] = unexpectedBetweenAsyncSpecifierAndThrowsSpecifier?.elements ?? []
502+
unexpected.append(throwsSpecifier.raw)
503+
unexpected.append(contentsOf: unexpectedAfterThrowsSpecifier?.elements ?? [])
504+
unexpectedAfterAsync = RawUnexpectedNodesSyntax(unexpected, arena: arena)
505+
} else {
506+
if let afterThrows = unexpectedAfterThrowsSpecifier {
507+
if let beforeThrows = unexpectedBetweenAsyncSpecifierAndThrowsSpecifier {
508+
unexpectedAfterAsync = RawUnexpectedNodesSyntax(beforeThrows.elements + afterThrows.elements, arena: arena)
509+
} else {
510+
unexpectedAfterAsync = afterThrows
511+
}
505512
} else {
506-
unexpectedAfterAsync = afterThrows
513+
unexpectedAfterAsync = unexpectedBetweenAsyncSpecifierAndThrowsSpecifier
507514
}
508-
} else {
509-
unexpectedAfterAsync = unexpectedBetweenAsyncSpecifierAndThrowsSpecifier
510515
}
511516
self.init(
512517
unexpectedBeforeAsyncSpecifier,
@@ -519,7 +524,6 @@ extension RawDeinitEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
519524
var unexpectedBetweenAsyncSpecifierAndThrowsSpecifier: SwiftSyntax.RawUnexpectedNodesSyntax? { unexpectedAfterAsyncSpecifier }
520525
var throwsSpecifier: SwiftSyntax.RawTokenSyntax? { nil }
521526
var unexpectedAfterThrowsSpecifier: SwiftSyntax.RawUnexpectedNodesSyntax? { nil }
522-
*/
523527
}
524528

525529
extension TokenConsumer {

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
144144
unexpected: UnexpectedNodesSyntax?,
145145
unexpectedTokenCondition: (TokenSyntax) -> Bool,
146146
correctTokens: [TokenSyntax?],
147-
correctTokenPossible: Bool = true,
148147
message: (_ misplacedTokens: [TokenSyntax]) -> some DiagnosticMessage,
149148
moveFixIt: (_ misplacedTokens: [TokenSyntax]) -> FixItMessage,
150149
removeRedundantFixIt: (_ misplacedTokens: [TokenSyntax]) -> FixItMessage? = { _ in nil }
@@ -182,7 +181,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
182181
if changes.count > 1 {
183182
// Only emit a Fix-It if we are moving a token, i.e. also making a token present.
184183
fixIts.append(FixIt(message: moveFixIt(misplacedTokens), changes: changes))
185-
} else if !correctTokens.isEmpty || !correctTokenPossible, let removeFixIt = removeRedundantFixIt(misplacedTokens) {
184+
} else if !correctTokens.isEmpty, let removeFixIt = removeRedundantFixIt(misplacedTokens) {
186185
fixIts.append(FixIt(message: removeFixIt, changes: changes))
187186
}
188187

@@ -232,27 +231,23 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
232231
}
233232

234233
private func handleEffectSpecifiers(
235-
_ node: some EffectSpecifiersSyntax,
236-
asyncIsPossible: Bool = true,
237-
misspelledAsync: StaticParserError = .misspelledAsync,
238-
throwsIsPossible: Bool = true,
239-
misspelledThrows: StaticParserError = .misspelledThrows
234+
_ node: some EffectSpecifiersSyntax
240235
) -> SyntaxVisitorContinueKind {
241236
if shouldSkip(node) {
242237
return .skipChildren
243238
}
244239

245240
let specifierInfo = [
246-
(node.asyncSpecifier, { AsyncEffectSpecifier(token: $0) != nil }, asyncIsPossible, misspelledAsync),
247-
(node.throwsSpecifier, { ThrowsEffectSpecifier(token: $0) != nil }, throwsIsPossible, misspelledThrows),
241+
(node.asyncSpecifier, { AsyncEffectSpecifier(token: $0) != nil }, StaticParserError.misspelledAsync),
242+
(node.throwsSpecifier, { ThrowsEffectSpecifier(token: $0) != nil }, StaticParserError.misspelledThrows),
248243
]
249244

250245
let unexpectedNodes = [node.unexpectedBeforeAsyncSpecifier, node.unexpectedBetweenAsyncSpecifierAndThrowsSpecifier, node.unexpectedAfterThrowsSpecifier]
251246

252247
// Diagnostics that are emitted later silence previous diagnostics, so check
253248
// for the most contextual (and thus helpful) diagnostics last.
254249

255-
for (specifier, isOfSameKind, isPossible, misspelledError) in specifierInfo {
250+
for (specifier, isOfSameKind, misspelledError) in specifierInfo {
256251
guard let specifier = specifier else {
257252
continue
258253
}
@@ -261,7 +256,6 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
261256
unexpected: unexpected,
262257
unexpectedTokenCondition: isOfSameKind,
263258
correctTokens: [specifier],
264-
correctTokenPossible: isPossible,
265259
message: { _ in misspelledError },
266260
moveFixIt: { ReplaceTokensFixIt(replaceTokens: $0, replacements: [specifier]) },
267261
removeRedundantFixIt: { RemoveRedundantFixIt(removeTokens: $0) }
@@ -280,7 +274,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
280274
)
281275
}
282276

283-
for (specifier, isOfSameKind, _, _) in specifierInfo {
277+
for (specifier, isOfSameKind, _) in specifierInfo {
284278
guard let specifier = specifier else {
285279
continue
286280
}
@@ -760,7 +754,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
760754
let isOfAsyncKind = { AsyncEffectSpecifier(token: $0) != nil }
761755
let isOfThrowsKind = { ThrowsEffectSpecifier(token: $0) != nil }
762756

763-
let unexpectedNodes = [node.unexpectedBeforeAsyncSpecifier, node.unexpectedBetweenAsyncSpecifierAndThrowsSpecifier, node.unexpectedAfterThrowsSpecifier]
757+
let unexpectedNodes = [node.unexpectedBeforeAsyncSpecifier, node.unexpectedAfterAsyncSpecifier]
764758

765759
// Diagnostics that are emitted later silence previous diagnostics, so check
766760
// for the most contextual (and thus helpful) diagnostics last.
@@ -801,7 +795,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
801795

802796
if let throwsKw = unexpectedThrows.first {
803797
exchangeTokens(
804-
unexpected: node.unexpectedAfterThrowsSpecifier,
798+
unexpected: node.unexpectedAfterAsyncSpecifier,
805799
unexpectedTokenCondition: isOfAsyncKind,
806800
correctTokens: [node.asyncSpecifier],
807801
message: { AsyncMustPrecedeThrows(asyncKeywords: $0, throwsKeyword: throwsKw) },

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -859,12 +859,8 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
859859
return "unexpectedBeforeAsyncSpecifier"
860860
case \DeinitEffectSpecifiersSyntax.asyncSpecifier:
861861
return "asyncSpecifier"
862-
case \DeinitEffectSpecifiersSyntax.unexpectedBetweenAsyncSpecifierAndThrowsSpecifier:
863-
return "unexpectedBetweenAsyncSpecifierAndThrowsSpecifier"
864-
case \DeinitEffectSpecifiersSyntax.throwsSpecifier:
865-
return "throwsSpecifier"
866-
case \DeinitEffectSpecifiersSyntax.unexpectedAfterThrowsSpecifier:
867-
return "unexpectedAfterThrowsSpecifier"
862+
case \DeinitEffectSpecifiersSyntax.unexpectedAfterAsyncSpecifier:
863+
return "unexpectedAfterAsyncSpecifier"
868864
case \DeinitializerDeclSyntax.unexpectedBeforeAttributes:
869865
return "unexpectedBeforeAttributes"
870866
case \DeinitializerDeclSyntax.attributes:

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,6 @@ extension DeclNameArgumentsSyntax: ParenthesizedSyntax {}
493493

494494
extension DeferStmtSyntax: WithCodeBlockSyntax {}
495495

496-
extension DeinitEffectSpecifiersSyntax: EffectSpecifiersSyntax {}
497-
498496
extension DeinitializerDeclSyntax: AttributedSyntax {}
499497

500498
extension DictionaryElementSyntax: WithTrailingCommaSyntax {}

Sources/SwiftSyntax/generated/raw/RawSyntaxNodes.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6013,19 +6013,15 @@ public struct RawDeinitEffectSpecifiersSyntax: RawSyntaxNodeProtocol {
60136013
public init(
60146014
_ unexpectedBeforeAsyncSpecifier: RawUnexpectedNodesSyntax? = nil,
60156015
asyncSpecifier: RawTokenSyntax?,
6016-
_ unexpectedBetweenAsyncSpecifierAndThrowsSpecifier: RawUnexpectedNodesSyntax? = nil,
6017-
throwsSpecifier: RawTokenSyntax?,
6018-
_ unexpectedAfterThrowsSpecifier: RawUnexpectedNodesSyntax? = nil,
6016+
_ unexpectedAfterAsyncSpecifier: RawUnexpectedNodesSyntax? = nil,
60196017
arena: __shared SyntaxArena
60206018
) {
60216019
let raw = RawSyntax.makeLayout(
6022-
kind: .deinitEffectSpecifiers, uninitializedCount: 5, arena: arena) { layout in
6020+
kind: .deinitEffectSpecifiers, uninitializedCount: 3, arena: arena) { layout in
60236021
layout.initialize(repeating: nil)
60246022
layout[0] = unexpectedBeforeAsyncSpecifier?.raw
60256023
layout[1] = asyncSpecifier?.raw
6026-
layout[2] = unexpectedBetweenAsyncSpecifierAndThrowsSpecifier?.raw
6027-
layout[3] = throwsSpecifier?.raw
6028-
layout[4] = unexpectedAfterThrowsSpecifier?.raw
6024+
layout[2] = unexpectedAfterAsyncSpecifier?.raw
60296025
}
60306026
self.init(unchecked: raw)
60316027
}
@@ -6038,17 +6034,9 @@ public struct RawDeinitEffectSpecifiersSyntax: RawSyntaxNodeProtocol {
60386034
layoutView.children[1].map(RawTokenSyntax.init(raw:))
60396035
}
60406036

6041-
public var unexpectedBetweenAsyncSpecifierAndThrowsSpecifier: RawUnexpectedNodesSyntax? {
6037+
public var unexpectedAfterAsyncSpecifier: RawUnexpectedNodesSyntax? {
60426038
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
60436039
}
6044-
6045-
public var throwsSpecifier: RawTokenSyntax? {
6046-
layoutView.children[3].map(RawTokenSyntax.init(raw:))
6047-
}
6048-
6049-
public var unexpectedAfterThrowsSpecifier: RawUnexpectedNodesSyntax? {
6050-
layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:))
6051-
}
60526040
}
60536041

60546042
@_spi(RawSyntax)

Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,10 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
877877
assertNoError(kind, 3, verify(layout[3], as: RawCodeBlockSyntax.self))
878878
assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self))
879879
case .deinitEffectSpecifiers:
880-
assert(layout.count == 5)
880+
assert(layout.count == 3)
881881
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
882882
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax?.self, tokenChoices: [.keyword("async")]))
883883
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
884-
assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: []))
885-
assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self))
886884
case .deinitializerDecl:
887885
assert(layout.count == 11)
888886
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodes.swift

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5864,28 +5864,14 @@ public struct DeinitEffectSpecifiersSyntax: SyntaxProtocol, SyntaxHashable {
58645864
leadingTrivia: Trivia? = nil,
58655865
_ unexpectedBeforeAsyncSpecifier: UnexpectedNodesSyntax? = nil,
58665866
asyncSpecifier: TokenSyntax? = nil,
5867-
_ unexpectedBetweenAsyncSpecifierAndThrowsSpecifier: UnexpectedNodesSyntax? = nil,
5868-
throwsSpecifier: TokenSyntax? = nil,
5869-
_ unexpectedAfterThrowsSpecifier: UnexpectedNodesSyntax? = nil,
5867+
_ unexpectedAfterAsyncSpecifier: UnexpectedNodesSyntax? = nil,
58705868
trailingTrivia: Trivia? = nil
58715869

58725870
) {
58735871
// Extend the lifetime of all parameters so their arenas don't get destroyed
58745872
// before they can be added as children of the new arena.
5875-
let data: SyntaxData = withExtendedLifetime((SyntaxArena(), (
5876-
unexpectedBeforeAsyncSpecifier,
5877-
asyncSpecifier,
5878-
unexpectedBetweenAsyncSpecifierAndThrowsSpecifier,
5879-
throwsSpecifier,
5880-
unexpectedAfterThrowsSpecifier
5881-
))) {(arena, _) in
5882-
let layout: [RawSyntax?] = [
5883-
unexpectedBeforeAsyncSpecifier?.raw,
5884-
asyncSpecifier?.raw,
5885-
unexpectedBetweenAsyncSpecifierAndThrowsSpecifier?.raw,
5886-
throwsSpecifier?.raw,
5887-
unexpectedAfterThrowsSpecifier?.raw
5888-
]
5873+
let data: SyntaxData = withExtendedLifetime((SyntaxArena(), (unexpectedBeforeAsyncSpecifier, asyncSpecifier, unexpectedAfterAsyncSpecifier))) {(arena, _) in
5874+
let layout: [RawSyntax?] = [unexpectedBeforeAsyncSpecifier?.raw, asyncSpecifier?.raw, unexpectedAfterAsyncSpecifier?.raw]
58895875
let raw = RawSyntax.makeLayout(
58905876
kind: SyntaxKind.deinitEffectSpecifiers,
58915877
from: layout,
@@ -5917,7 +5903,7 @@ public struct DeinitEffectSpecifiersSyntax: SyntaxProtocol, SyntaxHashable {
59175903
}
59185904
}
59195905

5920-
public var unexpectedBetweenAsyncSpecifierAndThrowsSpecifier: UnexpectedNodesSyntax? {
5906+
public var unexpectedAfterAsyncSpecifier: UnexpectedNodesSyntax? {
59215907
get {
59225908
return data.child(at: 2, parent: Syntax(self)).map(UnexpectedNodesSyntax.init)
59235909
}
@@ -5926,32 +5912,8 @@ public struct DeinitEffectSpecifiersSyntax: SyntaxProtocol, SyntaxHashable {
59265912
}
59275913
}
59285914

5929-
public var throwsSpecifier: TokenSyntax? {
5930-
get {
5931-
return data.child(at: 3, parent: Syntax(self)).map(TokenSyntax.init)
5932-
}
5933-
set(value) {
5934-
self = DeinitEffectSpecifiersSyntax(data.replacingChild(at: 3, with: value?.raw, arena: SyntaxArena()))
5935-
}
5936-
}
5937-
5938-
public var unexpectedAfterThrowsSpecifier: UnexpectedNodesSyntax? {
5939-
get {
5940-
return data.child(at: 4, parent: Syntax(self)).map(UnexpectedNodesSyntax.init)
5941-
}
5942-
set(value) {
5943-
self = DeinitEffectSpecifiersSyntax(data.replacingChild(at: 4, with: value?.raw, arena: SyntaxArena()))
5944-
}
5945-
}
5946-
59475915
public static var structure: SyntaxNodeStructure {
5948-
return .layout([
5949-
\Self.unexpectedBeforeAsyncSpecifier,
5950-
\Self.asyncSpecifier,
5951-
\Self.unexpectedBetweenAsyncSpecifierAndThrowsSpecifier,
5952-
\Self.throwsSpecifier,
5953-
\Self.unexpectedAfterThrowsSpecifier
5954-
])
5916+
return .layout([\Self.unexpectedBeforeAsyncSpecifier, \Self.asyncSpecifier, \Self.unexpectedAfterAsyncSpecifier])
59555917
}
59565918
}
59575919

Tests/SwiftParserTest/translated/AsyncTests.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,7 @@ final class AsyncTests: XCTestCase {
150150
""",
151151
diagnostics: [
152152
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'async' in subscript"),
153-
],
154-
fixedSource: """
155-
class X {
156-
init() async { }
157-
deinit async { }
158-
func f() async { }
159-
subscript(x: Int) async -> Int {
160-
get {
161-
return 0
162-
}
163-
set async {
164-
}
165-
}
166-
}
167-
"""
153+
]
168154
)
169155
}
170156

0 commit comments

Comments
 (0)