Skip to content

[embedded] Make StaticString versions assert/precondition/fatalError available in embedded Swift #68654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions stdlib/public/core/Assert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public func assert(
}
}

#if $Embedded
@_transparent
public func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> StaticString = StaticString(),
file: StaticString = #file, line: UInt = #line
) {
// Only assert in debug mode.
if _isDebugAssertConfiguration() {
if !_fastPath(condition()) {
_assertionFailure("Assertion failed", message(), file: file, line: line,
flags: _fatalErrorFlags())
}
}
}
#endif

/// Checks a necessary condition for making forward progress.
///
/// Use this function to detect conditions that must prevent the program from
Expand Down Expand Up @@ -100,6 +117,27 @@ public func precondition(
}
}

#if $Embedded
@_transparent
public func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> StaticString = StaticString(),
file: StaticString = #file, line: UInt = #line
) {
// Only check in debug and release mode. In release mode just trap.
if _isDebugAssertConfiguration() {
if !_fastPath(condition()) {
_assertionFailure("Precondition failed", message(), file: file, line: line,
flags: _fatalErrorFlags())
}
} else if _isReleaseAssertConfiguration() {
let error = !condition()
Builtin.condfail_message(error._value,
StaticString("precondition failure").unsafeRawPointer)
}
}
#endif

/// Indicates that an internal sanity check failed.
///
/// This function's effect varies depending on the build flag used:
Expand Down Expand Up @@ -137,6 +175,23 @@ public func assertionFailure(
}
}

#if $Embedded
@inlinable
@inline(__always)
public func assertionFailure(
_ message: @autoclosure () -> StaticString = StaticString(),
file: StaticString = #file, line: UInt = #line
) {
if _isDebugAssertConfiguration() {
_assertionFailure("Fatal error", message(), file: file, line: line,
flags: _fatalErrorFlags())
}
else if _isFastAssertConfiguration() {
_conditionallyUnreachable()
}
}
#endif

/// Indicates that a precondition was violated.
///
/// Use this function to stop the program when control flow can only reach the
Expand Down Expand Up @@ -164,7 +219,6 @@ public func assertionFailure(
/// where `preconditionFailure(_:file:line:)` is called.
/// - line: The line number to print along with `message`. The default is the
/// line number where `preconditionFailure(_:file:line:)` is called.
#if !$Embedded
@_transparent
@_unavailableInEmbedded
public func preconditionFailure(
Expand All @@ -181,7 +235,8 @@ public func preconditionFailure(
}
_conditionallyUnreachable()
}
#else

#if $Embedded
@_transparent
public func preconditionFailure(
_ message: @autoclosure () -> StaticString = StaticString(),
Expand All @@ -207,7 +262,6 @@ public func preconditionFailure(
/// where `fatalError(_:file:line:)` is called.
/// - line: The line number to print along with `message`. The default is the
/// line number where `fatalError(_:file:line:)` is called.
#if !$Embedded
@_transparent
@_unavailableInEmbedded
public func fatalError(
Expand All @@ -217,7 +271,8 @@ public func fatalError(
_assertionFailure("Fatal error", message(), file: file, line: line,
flags: _fatalErrorFlags())
}
#else

#if $Embedded
@_transparent
public func fatalError(
_ message: @autoclosure () -> StaticString = StaticString(),
Expand Down
13 changes: 13 additions & 0 deletions test/embedded/stdlib-basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public func staticstring() -> StaticString {
return "hello"
}

public func checks(n: Int) {
precondition(n > 0)
precondition(n > 0, "message")
assert(n > 0, "message")
if n < 0 { fatalError() }
if n < 0 { fatalError("with message") }
if n < 0 { preconditionFailure() }
if n < 0 { preconditionFailure("with message") }
if n < 0 { assertionFailure() }
if n < 0 { assertionFailure("with message") }
}

// CHECK: define {{.*}}i32 @main(i32 %0, ptr %1)
// CHECK: define {{.*}}i1 @"$s4main4boolSbyF"()
// CHECK: define {{.*}}i1 @"$sSb22_builtinBooleanLiteralSbBi1__tcfC"(i1 %0)
Expand All @@ -33,3 +45,4 @@ public func staticstring() -> StaticString {
// CHECK: define {{.*}}{ {{i32|i64}}, i8 } @"$s4main8optionalSiSgyF"()
// CHECK: define {{.*}}{ {{i32|i64}}, {{i32|i64}}, i8 } @"$s4main12staticstrings12StaticStringVyF"()
// CHECK: define {{.*}}{ {{i32|i64}}, {{i32|i64}}, i8 } @"$ss12StaticStringV08_builtinB7Literal17utf8CodeUnitCount7isASCIIABBp_BwBi1_tcfC"(ptr %0, {{i32|i64}} %1, i1 %2)
// CHECK: define {{.*}}void @"$s4main6checks1nySi_tF"({{i32|i64}} %0)