Skip to content

Commit 81005c1

Browse files
committed
SwiftCompilerSources: forward assertion and precondition failures to the assertion-handling in the C++ code base.
1 parent 718ea4b commit 81005c1

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,36 @@
2121
/// case for `precondition`.
2222
@_transparent
2323
public func assert(_ condition: Bool, _ message: @autoclosure () -> String,
24-
file: StaticString = #fileID, line: UInt = #line) {
25-
if !condition {
26-
fatalError(message(), file: file, line: line)
27-
}
24+
file: StaticString = #fileID, line: UInt = #line, function: StaticString = #function) {
25+
precondition(condition, message(), file: file, line: line, function: function)
2826
}
2927

3028
/// The assert function (without a message) to be used in the compiler.
3129
///
3230
/// Unforuntately it's not possible to just add a default argument to `message` in the
3331
/// other `assert` function. We need to defined this overload.
32+
/// TODO: For some reason the compiler is not happy when adding a `function` argument.
3433
@_transparent
3534
public func assert(_ condition: Bool, file: StaticString = #fileID, line: UInt = #line) {
36-
if !condition {
37-
fatalError("", file: file, line: line)
35+
precondition(condition, "", file: file, line: line, function: "")
36+
}
37+
38+
/// The assert function to be used in the compiler.
39+
///
40+
/// This overrides the standard Swift precondition and forwards an assertion failure
41+
/// to the assertion-handling in the C++ code base.
42+
@_transparent
43+
public func precondition(_ condition: Bool, _ message: @autoclosure () -> String,
44+
file: StaticString = #fileID, line: UInt = #line, function: StaticString = #function) {
45+
if !_fastPath(condition) {
46+
let msg = message()
47+
msg.withCString { msgStr in
48+
file.withUTF8Buffer { fileBytes in
49+
function.withUTF8Buffer { functionBytes in
50+
assertFail(msgStr, fileBytes.baseAddress!, line, functionBytes.baseAddress!)
51+
}
52+
}
53+
}
3854
}
3955
}
4056

include/swift/Basic/BasicBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ typedef uintptr_t SwiftUInt;
107107
#define BRIDGING_WRAPPER_NULLABLE(Node, Name) \
108108
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, _Nullable)
109109

110+
void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
111+
SwiftUInt line, const char * _Nonnull function);
112+
110113
//===----------------------------------------------------------------------===//
111114
// MARK: ArrayRef
112115
//===----------------------------------------------------------------------===//

lib/Basic/BasicBridging.cpp

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

13+
#include "swift/Basic/Assertions.h"
1314
#include "swift/Basic/BasicBridging.h"
1415
#include "llvm/Support/JSON.h"
1516
#include "llvm/Support/raw_ostream.h"
@@ -22,6 +23,11 @@
2223

2324
using namespace swift;
2425

26+
void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
27+
SwiftUInt line, const char * _Nonnull function) {
28+
ASSERT_failure(msg, file, line, function);
29+
}
30+
2531
//===----------------------------------------------------------------------===//
2632
// MARK: BridgedStringRef
2733
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)