Skip to content

Commit 3faac59

Browse files
committed
Enable assertions in release builds
This replaces the `assert` function by `syntaxAssert`, which also checks the condition in release builds. Always enabling assertions regresses parsing time of MovieSwiftUI by 12%. I think that’s a price we should be willing to pay to get the additional safety these assertions are giving us. Alternatively, we could consider - Offering a conditional compilation flag to only enable assertions in Release+Assert builds, similar to what we do in the C++ parser - Offering a conditional compilation flag to explicitly disable assertions in cases where performance is of the uttermost importance – I can’t think of any right now. Enabling `RawSyntaxValidation` decreases parsing time by another 40%. That seems a little bit too much to me.
1 parent 5fbdde7 commit 3faac59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+929
-894
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparser/ParserEntryFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let parserEntryFile = SourceFileSyntax {
8484
"""
8585
mutating func parseRemainder<R: RawSyntaxNodeProtocol>(into: R) -> R {
8686
guard !into.raw.kind.isSyntaxCollection, let layout = into.raw.layoutView else {
87-
assertionFailure("Only support parsing of non-collection layout nodes")
87+
syntaxAssertionFailure("Only support parsing of non-collection layout nodes")
8888
return into
8989
}
9090

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: "\(generateCopyrightHea
129129
DeclSyntax(
130130
"""
131131
init(raw: RawSyntax) {
132-
assert(Self.isKindOf(raw))
132+
syntaxAssert(Self.isKindOf(raw))
133133
self.raw = raw
134134
}
135135
"""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: [.blockComment(gener
158158
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
159159
/// is undefined.
160160
internal init(_ data: SyntaxData) {
161-
assert(data.raw.kind == .\(raw: node.swiftSyntaxKind))
161+
syntaxAssert(data.raw.kind == .\(raw: node.swiftSyntaxKind))
162162
self._syntaxNode = Syntax(data)
163163
}
164164
"""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func syntaxNode(emitKind: String) -> SourceFileSyntax {
5959
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
6060
/// is undefined.
6161
internal init(_ data: SyntaxData) {
62-
assert(data.raw.kind == .\(raw: node.swiftSyntaxKind))
62+
syntaxAssert(data.raw.kind == .\(raw: node.swiftSyntaxKind))
6363
self._syntaxNode = Syntax(data)
6464
}
6565
"""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: .docLineComment(generat
321321
// A child node was rewritten. Build the updated node.
322322
323323
// Sanity check, ensure the new children are the same length.
324-
assert(newLayout.count == node.raw.layoutView!.children.count)
324+
syntaxAssert(newLayout.count == node.raw.layoutView!.children.count)
325325
326326
let arena = SyntaxArena()
327327
let newRaw = node.raw.layoutView!.replacingLayout(with: Array(newLayout), arena: arena)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ let tokenKindFile = SourceFileSyntax {
282282
}
283283
} else if token.text != nil {
284284
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
285-
ExprSyntax("assert(text.isEmpty || rawKind.defaultText.map(String.init) == text)")
285+
ExprSyntax("syntaxAssert(text.isEmpty || rawKind.defaultText.map(String.init) == text)")
286286
StmtSyntax("return .\(raw: token.swiftKind)")
287287
}
288288
} else {

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct DiagnosticsError: Error {
8080
public init(diagnostics: [Diagnostic]) {
8181
self.diagnostics = diagnostics
8282

83-
assert(
83+
syntaxAssert(
8484
diagnostics.contains(where: { $0.diagMessage.severity == .error }),
8585
"at least one diagnostic must have severity == .error"
8686
)

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct FixIt {
5757
public let changes: Changes
5858

5959
public init(message: FixItMessage, changes: Changes) {
60-
assert(!changes.changes.isEmpty, "A Fix-It must have at least one change associated with it")
60+
syntaxAssert(!changes.changes.isEmpty, "A Fix-It must have at least one change associated with it")
6161
self.message = message
6262
self.changes = changes
6363
}

Sources/SwiftParser/Declarations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ extension Parser {
458458
)
459459
}
460460

461-
assert(self.currentToken.starts(with: "<"))
461+
syntaxAssert(self.currentToken.starts(with: "<"))
462462
let langle = self.consumeAnyToken(remapping: .leftAngle)
463463
var elements = [RawGenericParameterSyntax]()
464464
do {

Sources/SwiftParser/Expressions.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ extension Parser {
192192
return lastElement
193193
}
194194

195-
assert(
195+
syntaxAssert(
196196
elements.count.isMultiple(of: 2),
197197
"elements must have a even number of elements"
198198
)
@@ -591,7 +591,7 @@ extension Parser {
591591
declNameArgs: RawDeclNameArgumentsSyntax?,
592592
generics: RawGenericArgumentClauseSyntax?
593593
) {
594-
assert(self.at(.period))
594+
syntaxAssert(self.at(.period))
595595
let (unexpectedPeriod, period, skipMemberName) = self.consumeMemberPeriod(previousNode: previousNode)
596596
if skipMemberName {
597597
let missingIdentifier = missingToken(.identifier)
@@ -657,7 +657,7 @@ extension Parser {
657657
_ flavor: ExprFlavor,
658658
forDirective: Bool
659659
) -> RawExprSyntax {
660-
assert(self.at(.poundIfKeyword))
660+
syntaxAssert(self.at(.poundIfKeyword))
661661

662662
let config = self.parsePoundIfDirective { parser -> RawExprSyntax? in
663663
let head: RawExprSyntax
@@ -947,7 +947,7 @@ extension Parser {
947947
if self.currentToken.starts(with: "!") {
948948
questionOrExclaim = self.consumePrefix("!", as: .exclamationMark)
949949
} else {
950-
assert(self.currentToken.starts(with: "?"))
950+
syntaxAssert(self.currentToken.starts(with: "?"))
951951
questionOrExclaim = self.consumePrefix("?", as: .postfixQuestionMark)
952952
}
953953

@@ -1013,7 +1013,7 @@ extension Parser {
10131013
period = nil
10141014
}
10151015

1016-
assert(self.at(.leftSquareBracket))
1016+
syntaxAssert(self.at(.leftSquareBracket))
10171017
let lsquare = self.consumeAnyToken()
10181018
let args: [RawTupleExprElementSyntax]
10191019
if self.at(.rightSquareBracket) {
@@ -2054,7 +2054,7 @@ extension Parser.Lookahead {
20542054
/// handle this by doing some lookahead in common situations. And later, Sema
20552055
/// will emit a diagnostic with a fixit to add wrapping parens.
20562056
mutating func isValidTrailingClosure(_ flavor: Parser.ExprFlavor) -> Bool {
2057-
assert(self.at(.leftBrace), "Couldn't be a trailing closure")
2057+
syntaxAssert(self.at(.leftBrace), "Couldn't be a trailing closure")
20582058

20592059
// If this is the start of a get/set accessor, then it isn't a trailing
20602060
// closure.
@@ -2239,7 +2239,7 @@ extension Parser {
22392239
{ $0.parseSwitchCases(allowStandaloneStmtRecovery: allowStandaloneStmtRecovery) },
22402240
syntax: { parser, cases in
22412241
guard cases.count == 1, let firstCase = cases.first else {
2242-
assert(cases.isEmpty)
2242+
syntaxAssert(cases.isEmpty)
22432243
return .switchCases(RawSwitchCaseListSyntax(elements: [], arena: parser.arena))
22442244
}
22452245
return .switchCases(firstCase)

0 commit comments

Comments
 (0)