Skip to content

Commit 7f507c3

Browse files
committed
Don't guard assertions behind #if DEBUG
Some assertions were still guarded by a `#if DEBUG`. Since we are now also enabling assertions in release builds if `SWIFTSYNTAX_ENABLE_ASSERTIONS` is set, this is no longer correct. Convert most of them to plain preconditions. I have measured and could not detect a performance difference when parsing MovieSwiftU.
1 parent 2d5575b commit 7f507c3

File tree

4 files changed

+28
-52
lines changed

4 files changed

+28
-52
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,39 +137,29 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
137137
internal init(_ data: SyntaxData)
138138
"""
139139
) {
140-
IfConfigDeclSyntax(
141-
clauses: IfConfigClauseListSyntax {
142-
IfConfigClauseSyntax(
143-
poundKeyword: .poundIfKeyword(),
144-
condition: ExprSyntax("DEBUG"),
145-
elements: .statements(
146-
CodeBlockItemListSyntax {
147-
try! SwitchExprSyntax("switch data.raw.kind") {
148-
SwitchCaseSyntax(
149-
label: .case(
150-
SwitchCaseLabelSyntax {
151-
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
152-
CaseItemSyntax(
153-
pattern: ExpressionPatternSyntax(
154-
expression: ExprSyntax(".\(raw: childNode.swiftSyntaxKind)")
155-
)
156-
)
157-
}
158-
}
140+
CodeBlockItemListSyntax {
141+
try! SwitchExprSyntax("switch data.raw.kind") {
142+
SwitchCaseSyntax(
143+
label: .case(
144+
SwitchCaseLabelSyntax {
145+
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
146+
CaseItemSyntax(
147+
pattern: ExpressionPatternSyntax(
148+
expression: ExprSyntax(".\(raw: childNode.swiftSyntaxKind)")
159149
)
160-
) {
161-
BreakStmtSyntax()
162-
}
163-
164-
SwitchCaseSyntax("default:") {
165-
ExprSyntax("fatalError(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")")
166-
}
150+
)
167151
}
168152
}
169153
)
170-
)
154+
) {
155+
BreakStmtSyntax()
156+
}
157+
158+
SwitchCaseSyntax("default:") {
159+
ExprSyntax("preconditionFailure(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")")
160+
}
171161
}
172-
)
162+
}
173163

174164
ExprSyntax("self._syntaxNode = Syntax(data)")
175165
}

Sources/SwiftSyntax/SyntaxArena.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SyntaxArena {
1919
/// are retained in `addChild()` and are released in `deinit`.
2020
private var childRefs: Set<SyntaxArenaRef>
2121

22-
#if DEBUG
22+
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
2323
/// Whether or not this arena has been added to other arenas as a child.
2424
/// Used to make sure we don’t introduce retain cycles between arenas.
2525
private var hasParent: Bool
@@ -32,7 +32,7 @@ public class SyntaxArena {
3232
fileprivate init(slabSize: Int) {
3333
self.allocator = BumpPtrAllocator(slabSize: slabSize)
3434
self.childRefs = []
35-
#if DEBUG
35+
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
3636
self.hasParent = false
3737
#endif
3838
}
@@ -107,7 +107,7 @@ public class SyntaxArena {
107107
func addChild(_ otherRef: SyntaxArenaRef) {
108108
if SyntaxArenaRef(self) == otherRef { return }
109109

110-
#if DEBUG
110+
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
111111
precondition(
112112
!self.hasParent,
113113
"an arena can't have a new child once it's owned by other arenas"
@@ -116,7 +116,7 @@ public class SyntaxArena {
116116

117117
if childRefs.insert(otherRef).inserted {
118118
otherRef.retain()
119-
#if DEBUG
119+
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
120120
// FIXME: This may trigger a data race warning in Thread Sanitizer.
121121
// Can we use atomic bool here?
122122
otherRef.value.hasParent = true

Sources/SwiftSyntax/SyntaxChildren.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,9 @@ struct NonNilRawSyntaxChildren: BidirectionalCollection {
370370
{
371371
return reversedIndex
372372
}
373-
#if DEBUG
374373
// Reversing any further would result in undefined behaviour of
375374
// index(before:)
376-
if reversedIndex == children.startIndex {
377-
fatalError("presentIndex(before:) must not be called if there is no " + "present index before the given one")
378-
}
379-
#endif
375+
precondition(reversedIndex != children.startIndex, "presentIndex(before:) must not be called if there is no " + "present index before the given one")
380376
reversedIndex = children.index(before: reversedIndex)
381377
}
382378
}

Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,12 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
8282
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
8383
/// is undefined.
8484
internal init(_ data: SyntaxData) {
85-
#if DEBUG
8685
switch data.raw.kind {
8786
case .accessorDecl, .actorDecl, .associatedtypeDecl, .classDecl, .deinitializerDecl, .editorPlaceholderDecl, .enumCaseDecl, .enumDecl, .extensionDecl, .functionDecl, .ifConfigDecl, .importDecl, .initializerDecl, .macroDecl, .macroExpansionDecl, .missingDecl, .operatorDecl, .poundSourceLocation, .precedenceGroupDecl, .protocolDecl, .structDecl, .subscriptDecl, .typealiasDecl, .variableDecl:
8887
break
8988
default:
90-
fatalError("Unable to create DeclSyntax from \(data.raw.kind)")
89+
preconditionFailure("Unable to create DeclSyntax from \(data.raw.kind)")
9190
}
92-
#endif
9391
self._syntaxNode = Syntax(data)
9492
}
9593

@@ -227,14 +225,12 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
227225
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
228226
/// is undefined.
229227
internal init(_ data: SyntaxData) {
230-
#if DEBUG
231228
switch data.raw.kind {
232229
case .arrayExpr, .arrowExpr, .asExpr, .assignmentExpr, .awaitExpr, .binaryOperatorExpr, .booleanLiteralExpr, .borrowExpr, .closureExpr, .dictionaryExpr, .discardAssignmentExpr, .editorPlaceholderExpr, .floatLiteralExpr, .forcedValueExpr, .functionCallExpr, .identifierExpr, .ifExpr, .inOutExpr, .infixOperatorExpr, .integerLiteralExpr, .isExpr, .keyPathExpr, .macroExpansionExpr, .memberAccessExpr, .missingExpr, .moveExpr, .nilLiteralExpr, .optionalChainingExpr, .packElementExpr, .packExpansionExpr, .postfixIfConfigExpr, .postfixUnaryExpr, .prefixOperatorExpr, .regexLiteralExpr, .sequenceExpr, .specializeExpr, .stringLiteralExpr, .subscriptExpr, .superRefExpr, .switchExpr, .ternaryExpr, .tryExpr, .tupleExpr, .typeExpr, .unresolvedAsExpr, .unresolvedIsExpr, .unresolvedPatternExpr, .unresolvedTernaryExpr:
233230
break
234231
default:
235-
fatalError("Unable to create ExprSyntax from \(data.raw.kind)")
232+
preconditionFailure("Unable to create ExprSyntax from \(data.raw.kind)")
236233
}
237-
#endif
238234
self._syntaxNode = Syntax(data)
239235
}
240236

@@ -396,14 +392,12 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
396392
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
397393
/// is undefined.
398394
internal init(_ data: SyntaxData) {
399-
#if DEBUG
400395
switch data.raw.kind {
401396
case .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
402397
break
403398
default:
404-
fatalError("Unable to create PatternSyntax from \(data.raw.kind)")
399+
preconditionFailure("Unable to create PatternSyntax from \(data.raw.kind)")
405400
}
406-
#endif
407401
self._syntaxNode = Syntax(data)
408402
}
409403

@@ -524,14 +518,12 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
524518
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
525519
/// is undefined.
526520
internal init(_ data: SyntaxData) {
527-
#if DEBUG
528521
switch data.raw.kind {
529522
case .breakStmt, .continueStmt, .deferStmt, .doStmt, .expressionStmt, .fallthroughStmt, .forInStmt, .forgetStmt, .guardStmt, .labeledStmt, .missingStmt, .repeatWhileStmt, .returnStmt, .throwStmt, .whileStmt, .yieldStmt:
530523
break
531524
default:
532-
fatalError("Unable to create StmtSyntax from \(data.raw.kind)")
525+
preconditionFailure("Unable to create StmtSyntax from \(data.raw.kind)")
533526
}
534-
#endif
535527
self._syntaxNode = Syntax(data)
536528
}
537529

@@ -661,14 +653,12 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
661653
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
662654
/// is undefined.
663655
internal init(_ data: SyntaxData) {
664-
#if DEBUG
665656
switch data.raw.kind {
666657
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
667658
break
668659
default:
669-
fatalError("Unable to create TypeSyntax from \(data.raw.kind)")
660+
preconditionFailure("Unable to create TypeSyntax from \(data.raw.kind)")
670661
}
671-
#endif
672662
self._syntaxNode = Syntax(data)
673663
}
674664

0 commit comments

Comments
 (0)