Skip to content

Commit 9a6b16b

Browse files
authored
Merge pull request #1342 from ahoppen/ahoppen/always-assert
Enable assertions in CI even if we are building in release builds
2 parents f8681e6 + e001fb1 commit 9a6b16b

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ if ProcessInfo.processInfo.environment["SWIFT_BUILD_SCRIPT_ENVIRONMENT"] != nil
1212
.appendingPathComponent("utils")
1313
.appendingPathComponent("group.json")
1414
swiftSyntaxSwiftSettings = [
15+
.define("SWIFTSYNTAX_ENABLE_ASSERTIONS"),
1516
.unsafeFlags([
1617
"-Xfrontend", "-group-info-path",
1718
"-Xfrontend", groupFile.path,
1819
// Enforcing exclusivity increases compile time of release builds by 2 minutes.
1920
// Disable it when we're in a controlled CI environment.
2021
"-enforce-exclusivity=unchecked",
21-
])
22+
]),
2223
]
2324
} else {
2425
swiftSyntaxSwiftSettings = []

Sources/SwiftSyntax/Assert.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// How to choose `assert` vs. `precondition`:
14+
/// - Wherever possible, it is preferrable to emit a diagnostic instead of
15+
/// using `precondition`. This way the parser won't crash if the condition is
16+
/// violated.
17+
/// - If you think the diagnostic added above should never be emitted, it is
18+
/// fine to also emit an `assertionFailure` in debug builds to make it easier
19+
/// to debug the unexpected diagnostic.
20+
/// - If in doubt always use `precondition`
21+
/// - `assert` should only be used if checking the assertion has a non-trivial
22+
/// cost and provides little benefit in terms of safety in release builds.
23+
24+
// MARK: - Assert
25+
26+
/// An assertion that is active in DEBUG builds, just like `Swift.assert` and
27+
/// additionally if assertions are explicitly requested by setting the
28+
/// `SWIFTSYNTAX_ENABLE_ASSERTIONS` conditional compilation flag.
29+
/// Use this instead of `precondition` in places where the assertion has a
30+
/// non-trivial cost but provides little value in release builds.
31+
@_transparent
32+
public func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
33+
#if SWIFTSYNTAX_ENABLE_ASSERTIONS
34+
if !_fastPath(condition()) {
35+
fatalError(message(), file: file, line: line)
36+
}
37+
#else
38+
Swift.assert(condition(), message(), file: file, line: line)
39+
#endif
40+
}
41+
42+
/// An assertion that is active in DEBUG builds, just like
43+
/// `Swift.assertionFailure` and additionally if assertions are explicitly
44+
/// requested by setting the `SWIFTSYNTAX_ENABLE_ASSERTIONS` conditional
45+
/// compilation flag.
46+
@_transparent
47+
public func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
48+
#if SWIFTSYNTAX_ENABLE_ASSERTIONS
49+
fatalError(message(), file: file, line: line)
50+
#else
51+
Swift.assertionFailure(message(), file: file, line: line)
52+
#endif
53+
}

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
add_swift_host_library(SwiftSyntax
1010
AbsolutePosition.swift
11+
Assert.swift
1112
BumpPtrAllocator.swift
1213
CommonAncestor.swift
1314
IncrementalParseTransition.swift

utils/group.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"TokenDiagnostic.swift",
5656
],
5757
"Internal": [
58+
"Assert.swift",
5859
"SyntaxArena.swift",
5960
"SyntaxVerifier.swift",
6061
"BumpPtrAllocator.swift",

0 commit comments

Comments
 (0)