Skip to content

Don't guard assertions behind #if DEBUG #1526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,29 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
internal init(_ data: SyntaxData)
"""
) {
IfConfigDeclSyntax(
clauses: IfConfigClauseListSyntax {
IfConfigClauseSyntax(
poundKeyword: .poundIfKeyword(),
condition: ExprSyntax("DEBUG"),
elements: .statements(
CodeBlockItemListSyntax {
try! SwitchExprSyntax("switch data.raw.kind") {
SwitchCaseSyntax(
label: .case(
SwitchCaseLabelSyntax {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
CaseItemSyntax(
pattern: ExpressionPatternSyntax(
expression: ExprSyntax(".\(raw: childNode.swiftSyntaxKind)")
)
)
}
}
CodeBlockItemListSyntax {
try! SwitchExprSyntax("switch data.raw.kind") {
SwitchCaseSyntax(
label: .case(
SwitchCaseLabelSyntax {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
CaseItemSyntax(
pattern: ExpressionPatternSyntax(
expression: ExprSyntax(".\(raw: childNode.swiftSyntaxKind)")
)
) {
BreakStmtSyntax()
}

SwitchCaseSyntax("default:") {
ExprSyntax("fatalError(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")")
}
)
}
}
)
)
) {
BreakStmtSyntax()
}

SwitchCaseSyntax("default:") {
ExprSyntax("preconditionFailure(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")")
}
}
)
}

ExprSyntax("self._syntaxNode = Syntax(data)")
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftSyntax/SyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SyntaxArena {
/// are retained in `addChild()` and are released in `deinit`.
private var childRefs: Set<SyntaxArenaRef>

#if DEBUG
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
/// Whether or not this arena has been added to other arenas as a child.
/// Used to make sure we don’t introduce retain cycles between arenas.
private var hasParent: Bool
Expand All @@ -32,7 +32,7 @@ public class SyntaxArena {
fileprivate init(slabSize: Int) {
self.allocator = BumpPtrAllocator(slabSize: slabSize)
self.childRefs = []
#if DEBUG
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
self.hasParent = false
#endif
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public class SyntaxArena {
func addChild(_ otherRef: SyntaxArenaRef) {
if SyntaxArenaRef(self) == otherRef { return }

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

if childRefs.insert(otherRef).inserted {
otherRef.retain()
#if DEBUG
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
// FIXME: This may trigger a data race warning in Thread Sanitizer.
// Can we use atomic bool here?
otherRef.value.hasParent = true
Expand Down
6 changes: 1 addition & 5 deletions Sources/SwiftSyntax/SyntaxChildren.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,9 @@ struct NonNilRawSyntaxChildren: BidirectionalCollection {
{
return reversedIndex
}
#if DEBUG
// Reversing any further would result in undefined behaviour of
// index(before:)
if reversedIndex == children.startIndex {
fatalError("presentIndex(before:) must not be called if there is no " + "present index before the given one")
}
#endif
precondition(reversedIndex != children.startIndex, "presentIndex(before:) must not be called if there is no " + "present index before the given one")
reversedIndex = children.index(before: reversedIndex)
}
}
Expand Down
20 changes: 5 additions & 15 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,12 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
/// is undefined.
internal init(_ data: SyntaxData) {
#if DEBUG
switch data.raw.kind {
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:
break
default:
fatalError("Unable to create DeclSyntax from \(data.raw.kind)")
preconditionFailure("Unable to create DeclSyntax from \(data.raw.kind)")
}
#endif
self._syntaxNode = Syntax(data)
}

Expand Down Expand Up @@ -227,14 +225,12 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
/// is undefined.
internal init(_ data: SyntaxData) {
#if DEBUG
switch data.raw.kind {
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:
break
default:
fatalError("Unable to create ExprSyntax from \(data.raw.kind)")
preconditionFailure("Unable to create ExprSyntax from \(data.raw.kind)")
}
#endif
self._syntaxNode = Syntax(data)
}

Expand Down Expand Up @@ -396,14 +392,12 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
/// is undefined.
internal init(_ data: SyntaxData) {
#if DEBUG
switch data.raw.kind {
case .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
break
default:
fatalError("Unable to create PatternSyntax from \(data.raw.kind)")
preconditionFailure("Unable to create PatternSyntax from \(data.raw.kind)")
}
#endif
self._syntaxNode = Syntax(data)
}

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

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

Expand Down