Skip to content

[stdlib] replace uses of assert in the Concurrency module #40116

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 2 commits into from
Nov 11, 2021
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
30 changes: 20 additions & 10 deletions stdlib/public/Concurrency/Deque.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct _Deque<Element> {
}

internal func slot(after slot: Int) -> Int {
assert(slot < capacity)
_internalInvariant(slot < capacity)
let position = slot + 1
if position >= capacity {
return 0
Expand All @@ -51,7 +51,7 @@ struct _Deque<Element> {


internal func slot(_ slot: Int, offsetBy delta: Int) -> Int {
assert(slot <= capacity)
_internalInvariant(slot <= capacity)
let position = slot + delta
if delta >= 0 {
if position >= capacity { return position - capacity }
Expand All @@ -66,13 +66,13 @@ struct _Deque<Element> {
}

internal func uncheckedAppend(_ element: Element) {
assert(count < capacity)
_internalInvariant(count < capacity)
ptr(at: endSlot).initialize(to: element)
count += 1
}

internal func uncheckedRemoveFirst() -> Element {
assert(count > 0)
_internalInvariant(count > 0)
let result = ptr(at: startSlot).move()
startSlot = slot(after: startSlot)
count -= 1
Expand Down Expand Up @@ -101,7 +101,7 @@ struct _Deque<Element> {
) {
self.first = first
self.second = second
assert(first.count > 0 || second == nil)
_internalInvariant(first.count > 0 || second == nil)
}

internal init(
Expand Down Expand Up @@ -135,7 +135,7 @@ struct _Deque<Element> {
) {
self.first = first
self.second = second?.count == 0 ? nil : second
assert(first.count > 0 || second == nil)
_internalInvariant(first.count > 0 || second == nil)
}

internal init(
Expand Down Expand Up @@ -180,7 +180,7 @@ struct _Deque<Element> {
}

func ptr(at slot: Int) -> UnsafeMutablePointer<Element> {
assert(slot >= 0 && slot <= capacity)
_internalInvariant(slot >= 0 && slot <= capacity)
return _elements! + slot
}

Expand All @@ -189,7 +189,7 @@ struct _Deque<Element> {
at start: Int,
from source: UnsafeBufferPointer<Element>
) -> Int {
assert(start + source.count <= capacity)
_internalInvariant(start + source.count <= capacity)
guard source.count > 0 else { return start }
ptr(at: start).initialize(from: source.baseAddress!, count: source.count)
return start + source.count
Expand All @@ -200,7 +200,7 @@ struct _Deque<Element> {
at start: Int,
from source: UnsafeMutableBufferPointer<Element>
) -> Int {
assert(start + source.count <= capacity)
_internalInvariant(start + source.count <= capacity)
guard source.count > 0 else { return start }
ptr(at: start).moveInitialize(from: source.baseAddress!, count: source.count)
return start + source.count
Expand All @@ -224,7 +224,7 @@ struct _Deque<Element> {

internal func moveElements(minimumCapacity: Int) -> _Storage {
let count = self.count
assert(minimumCapacity >= count)
_internalInvariant(minimumCapacity >= count)
let object = _Storage._DequeBuffer.create(
minimumCapacity: minimumCapacity,
makingHeaderWith: {
Expand Down Expand Up @@ -413,3 +413,13 @@ struct _Deque<Element> {
}
}

@_alwaysEmitIntoClient @_transparent
internal func _internalInvariant(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #file, line: UInt = #line
) {
#if INTERNAL_CHECKS_ENABLED
assert(condition(), message(), file: file, line: line)
#endif
}
4 changes: 2 additions & 2 deletions validation-test/stdlib/Assert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ Assert.test("_internalInvariant")
.crashOutputMatches("this should fail")
.code {
var x = 2
_internalInvariant(x * 21 == 42, "should not fail")
Swift._internalInvariant(x * 21 == 42, "should not fail")
expectCrashLater()
_internalInvariant(x == 42, "this should fail")
Swift._internalInvariant(x == 42, "this should fail")
}

Assert.test("_internalInvariantFailure")
Expand Down