Skip to content

Commit 984ae9e

Browse files
committed
Define our own assert implementation for the swift compiler sources
For two reasons: * We also like to check for assert failures in release builds. Although this could be achieved with `precondition`, it's easy to forget about it and use `assert` instead. * We need to see the error message in crashlogs of release builds. This is even not the case for `precondition`. Also, re-export the "Basic" module in "SIL" so that the new assert implementation is also available in the Optimizer module (where all files import SIL).
1 parent 435af14 commit 984ae9e

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
@_exported import BasicBridging
1414
import std
1515

16+
/// The assert function to be used in the compiler.
17+
///
18+
/// This overrides the standard Swift assert for two reasons:
19+
/// * We also like to check for assert failures in release builds. Although this could be
20+
/// achieved with `precondition`, it's easy to forget about it and use `assert` instead.
21+
/// * We need to see the error message in crashlogs of release builds. This is even not the
22+
/// case for `precondition`.
23+
@_transparent
24+
public func assert(_ condition: Bool, _ message: @autoclosure () -> String,
25+
file: StaticString = #fileID, line: UInt = #line) {
26+
if !condition {
27+
print("### basic")
28+
fatalError(message(), file: file, line: line)
29+
}
30+
}
31+
32+
/// The assert function (without a message) to be used in the compiler.
33+
///
34+
/// Unforuntately it's not possible to just add a default argument to `message` in the
35+
/// other `assert` function. We need to defined this overload.
36+
@_transparent
37+
public func assert(_ condition: Bool, file: StaticString = #fileID, line: UInt = #line) {
38+
if !condition {
39+
fatalError("", file: file, line: line)
40+
}
41+
}
42+
43+
1644
//===----------------------------------------------------------------------===//
1745
// StringRef
1846
//===----------------------------------------------------------------------===//

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Basic
1413
import SIL
1514

1615
/// Removes redundant ObjectiveC <-> Swift bridging calls.

SwiftCompilerSources/Sources/Optimizer/InstructionPasses/SimplifyBeginCOWMutation.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Basic
1413
import SIL
1514

1615
/// Simplify begin_cow_mutation instructions.

SwiftCompilerSources/Sources/SIL/Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
import SILBridging
1414

15+
// Need to export "Basic" to make `Basic.assert` available in the Optimizer module.
16+
// Otherwise The Optimizer would fall back to Swift's assert implementation.
17+
@_exported import Basic
18+
1519
//===----------------------------------------------------------------------===//
1620
// Lists
1721
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)