Skip to content

Add a conditional compilation flag to disable preconditions #1620

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
Apr 29, 2023
Merged
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
33 changes: 31 additions & 2 deletions Sources/SwiftSyntax/Assert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
/// Use this instead of `precondition` in places where the assertion has a
/// non-trivial cost but provides little value in release builds.
@_transparent
public func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
public func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #file,
line: UInt = #line
) {
#if SWIFTSYNTAX_ENABLE_ASSERTIONS
if !_fastPath(condition()) {
fatalError(message(), file: file, line: line)
Expand All @@ -44,10 +49,34 @@ public func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure
/// requested by setting the `SWIFTSYNTAX_ENABLE_ASSERTIONS` conditional
/// compilation flag.
@_transparent
public func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
public func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #file,
line: UInt = #line
) {
#if SWIFTSYNTAX_ENABLE_ASSERTIONS
fatalError(message(), file: file, line: line)
#else
Swift.assertionFailure(message(), file: file, line: line)
#endif
}

// MARK: - Precondition

/// Override Swift’s `precondition` with slightly changed semantics.
/// In release builds, it also emits the error message upon failure, like `fatalError`.
/// It can also be disabled by setting the `SWIFTSYNTAX_DISABLE_PRECONDITION` conditional compilation flag.
/// Note that `SWIFTSYNTAX_DISABLE_PRECONDITION` does not disable `preconditionFailure`.
@_transparent
public func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #file,
line: UInt = #line
) {
#if !SWIFTSYNTAX_DISABLE_PRECONDITIONS
if !_fastPath(condition()) {
fatalError(message(), file: file, line: line)
}
#endif
}