|
21 | 21 | /// case for `precondition`. |
22 | 22 | @_transparent |
23 | 23 | 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) |
28 | 26 | } |
29 | 27 |
|
30 | 28 | /// The assert function (without a message) to be used in the compiler. |
31 | 29 | /// |
32 | 30 | /// Unforuntately it's not possible to just add a default argument to `message` in the |
33 | 31 | /// other `assert` function. We need to defined this overload. |
| 32 | +/// TODO: For some reason the compiler is not happy when adding a `function` argument. |
34 | 33 | @_transparent |
35 | 34 | 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 | + } |
38 | 54 | } |
39 | 55 | } |
40 | 56 |
|
|
0 commit comments