Skip to content

Commit 17e41b6

Browse files
authored
Merge pull request #1526 from ahoppen/ahoppen/no-debug-guards
Don't guard assertions behind `#if DEBUG`
2 parents c4e9877 + 7f507c3 commit 17e41b6

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

@@ -219,14 +217,12 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
219217
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
220218
/// is undefined.
221219
internal init(_ data: SyntaxData) {
222-
#if DEBUG
223220
switch data.raw.kind {
224221
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:
225222
break
226223
default:
227-
fatalError("Unable to create ExprSyntax from \(data.raw.kind)")
224+
preconditionFailure("Unable to create ExprSyntax from \(data.raw.kind)")
228225
}
229-
#endif
230226
self._syntaxNode = Syntax(data)
231227
}
232228

@@ -380,14 +376,12 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
380376
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
381377
/// is undefined.
382378
internal init(_ data: SyntaxData) {
383-
#if DEBUG
384379
switch data.raw.kind {
385380
case .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
386381
break
387382
default:
388-
fatalError("Unable to create PatternSyntax from \(data.raw.kind)")
383+
preconditionFailure("Unable to create PatternSyntax from \(data.raw.kind)")
389384
}
390-
#endif
391385
self._syntaxNode = Syntax(data)
392386
}
393387

@@ -500,14 +494,12 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
500494
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
501495
/// is undefined.
502496
internal init(_ data: SyntaxData) {
503-
#if DEBUG
504497
switch data.raw.kind {
505498
case .breakStmt, .continueStmt, .deferStmt, .doStmt, .expressionStmt, .fallthroughStmt, .forInStmt, .forgetStmt, .guardStmt, .labeledStmt, .missingStmt, .repeatWhileStmt, .returnStmt, .throwStmt, .whileStmt, .yieldStmt:
506499
break
507500
default:
508-
fatalError("Unable to create StmtSyntax from \(data.raw.kind)")
501+
preconditionFailure("Unable to create StmtSyntax from \(data.raw.kind)")
509502
}
510-
#endif
511503
self._syntaxNode = Syntax(data)
512504
}
513505

@@ -629,14 +621,12 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
629621
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
630622
/// is undefined.
631623
internal init(_ data: SyntaxData) {
632-
#if DEBUG
633624
switch data.raw.kind {
634625
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .constrainedSugarType, .dictionaryType, .functionType, .implicitlyUnwrappedOptionalType, .memberTypeIdentifier, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packExpansionType, .packReferenceType, .simpleTypeIdentifier, .tupleType:
635626
break
636627
default:
637-
fatalError("Unable to create TypeSyntax from \(data.raw.kind)")
628+
preconditionFailure("Unable to create TypeSyntax from \(data.raw.kind)")
638629
}
639-
#endif
640630
self._syntaxNode = Syntax(data)
641631
}
642632

0 commit comments

Comments
 (0)