Skip to content

Commit c751b62

Browse files
authored
Merge pull request #68654 from kubamracek/embedded-preconditions
[embedded] Make StaticString versions assert/precondition/fatalError available in embedded Swift
2 parents 5cdff63 + a56a487 commit c751b62

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

stdlib/public/core/Assert.swift

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ public func assert(
5353
}
5454
}
5555

56+
#if $Embedded
57+
@_transparent
58+
public func assert(
59+
_ condition: @autoclosure () -> Bool,
60+
_ message: @autoclosure () -> StaticString = StaticString(),
61+
file: StaticString = #file, line: UInt = #line
62+
) {
63+
// Only assert in debug mode.
64+
if _isDebugAssertConfiguration() {
65+
if !_fastPath(condition()) {
66+
_assertionFailure("Assertion failed", message(), file: file, line: line,
67+
flags: _fatalErrorFlags())
68+
}
69+
}
70+
}
71+
#endif
72+
5673
/// Checks a necessary condition for making forward progress.
5774
///
5875
/// Use this function to detect conditions that must prevent the program from
@@ -100,6 +117,27 @@ public func precondition(
100117
}
101118
}
102119

120+
#if $Embedded
121+
@_transparent
122+
public func precondition(
123+
_ condition: @autoclosure () -> Bool,
124+
_ message: @autoclosure () -> StaticString = StaticString(),
125+
file: StaticString = #file, line: UInt = #line
126+
) {
127+
// Only check in debug and release mode. In release mode just trap.
128+
if _isDebugAssertConfiguration() {
129+
if !_fastPath(condition()) {
130+
_assertionFailure("Precondition failed", message(), file: file, line: line,
131+
flags: _fatalErrorFlags())
132+
}
133+
} else if _isReleaseAssertConfiguration() {
134+
let error = !condition()
135+
Builtin.condfail_message(error._value,
136+
StaticString("precondition failure").unsafeRawPointer)
137+
}
138+
}
139+
#endif
140+
103141
/// Indicates that an internal sanity check failed.
104142
///
105143
/// This function's effect varies depending on the build flag used:
@@ -137,6 +175,23 @@ public func assertionFailure(
137175
}
138176
}
139177

178+
#if $Embedded
179+
@inlinable
180+
@inline(__always)
181+
public func assertionFailure(
182+
_ message: @autoclosure () -> StaticString = StaticString(),
183+
file: StaticString = #file, line: UInt = #line
184+
) {
185+
if _isDebugAssertConfiguration() {
186+
_assertionFailure("Fatal error", message(), file: file, line: line,
187+
flags: _fatalErrorFlags())
188+
}
189+
else if _isFastAssertConfiguration() {
190+
_conditionallyUnreachable()
191+
}
192+
}
193+
#endif
194+
140195
/// Indicates that a precondition was violated.
141196
///
142197
/// Use this function to stop the program when control flow can only reach the
@@ -164,7 +219,6 @@ public func assertionFailure(
164219
/// where `preconditionFailure(_:file:line:)` is called.
165220
/// - line: The line number to print along with `message`. The default is the
166221
/// line number where `preconditionFailure(_:file:line:)` is called.
167-
#if !$Embedded
168222
@_transparent
169223
@_unavailableInEmbedded
170224
public func preconditionFailure(
@@ -181,7 +235,8 @@ public func preconditionFailure(
181235
}
182236
_conditionallyUnreachable()
183237
}
184-
#else
238+
239+
#if $Embedded
185240
@_transparent
186241
public func preconditionFailure(
187242
_ message: @autoclosure () -> StaticString = StaticString(),
@@ -207,7 +262,6 @@ public func preconditionFailure(
207262
/// where `fatalError(_:file:line:)` is called.
208263
/// - line: The line number to print along with `message`. The default is the
209264
/// line number where `fatalError(_:file:line:)` is called.
210-
#if !$Embedded
211265
@_transparent
212266
@_unavailableInEmbedded
213267
public func fatalError(
@@ -217,7 +271,8 @@ public func fatalError(
217271
_assertionFailure("Fatal error", message(), file: file, line: line,
218272
flags: _fatalErrorFlags())
219273
}
220-
#else
274+
275+
#if $Embedded
221276
@_transparent
222277
public func fatalError(
223278
_ message: @autoclosure () -> StaticString = StaticString(),

test/embedded/stdlib-basic.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public func staticstring() -> StaticString {
2323
return "hello"
2424
}
2525

26+
public func checks(n: Int) {
27+
precondition(n > 0)
28+
precondition(n > 0, "message")
29+
assert(n > 0, "message")
30+
if n < 0 { fatalError() }
31+
if n < 0 { fatalError("with message") }
32+
if n < 0 { preconditionFailure() }
33+
if n < 0 { preconditionFailure("with message") }
34+
if n < 0 { assertionFailure() }
35+
if n < 0 { assertionFailure("with message") }
36+
}
37+
2638
// CHECK: define {{.*}}i32 @main(i32 %0, ptr %1)
2739
// CHECK: define {{.*}}i1 @"$s4main4boolSbyF"()
2840
// CHECK: define {{.*}}i1 @"$sSb22_builtinBooleanLiteralSbBi1__tcfC"(i1 %0)
@@ -33,3 +45,4 @@ public func staticstring() -> StaticString {
3345
// CHECK: define {{.*}}{ {{i32|i64}}, i8 } @"$s4main8optionalSiSgyF"()
3446
// CHECK: define {{.*}}{ {{i32|i64}}, {{i32|i64}}, i8 } @"$s4main12staticstrings12StaticStringVyF"()
3547
// CHECK: define {{.*}}{ {{i32|i64}}, {{i32|i64}}, i8 } @"$ss12StaticStringV08_builtinB7Literal17utf8CodeUnitCount7isASCIIABBp_BwBi1_tcfC"(ptr %0, {{i32|i64}} %1, i1 %2)
48+
// CHECK: define {{.*}}void @"$s4main6checks1nySi_tF"({{i32|i64}} %0)

0 commit comments

Comments
 (0)