Skip to content

Commit 8cc0760

Browse files
authored
Merge pull request #40116 from glessard/concurrency-assertions
[stdlib] replace uses of assert in the Concurrency module
2 parents 71cc766 + 71f2d8e commit 8cc0760

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

stdlib/public/Concurrency/Deque.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct _Deque<Element> {
4141
}
4242

4343
internal func slot(after slot: Int) -> Int {
44-
assert(slot < capacity)
44+
_internalInvariant(slot < capacity)
4545
let position = slot + 1
4646
if position >= capacity {
4747
return 0
@@ -51,7 +51,7 @@ struct _Deque<Element> {
5151

5252

5353
internal func slot(_ slot: Int, offsetBy delta: Int) -> Int {
54-
assert(slot <= capacity)
54+
_internalInvariant(slot <= capacity)
5555
let position = slot + delta
5656
if delta >= 0 {
5757
if position >= capacity { return position - capacity }
@@ -66,13 +66,13 @@ struct _Deque<Element> {
6666
}
6767

6868
internal func uncheckedAppend(_ element: Element) {
69-
assert(count < capacity)
69+
_internalInvariant(count < capacity)
7070
ptr(at: endSlot).initialize(to: element)
7171
count += 1
7272
}
7373

7474
internal func uncheckedRemoveFirst() -> Element {
75-
assert(count > 0)
75+
_internalInvariant(count > 0)
7676
let result = ptr(at: startSlot).move()
7777
startSlot = slot(after: startSlot)
7878
count -= 1
@@ -101,7 +101,7 @@ struct _Deque<Element> {
101101
) {
102102
self.first = first
103103
self.second = second
104-
assert(first.count > 0 || second == nil)
104+
_internalInvariant(first.count > 0 || second == nil)
105105
}
106106

107107
internal init(
@@ -135,7 +135,7 @@ struct _Deque<Element> {
135135
) {
136136
self.first = first
137137
self.second = second?.count == 0 ? nil : second
138-
assert(first.count > 0 || second == nil)
138+
_internalInvariant(first.count > 0 || second == nil)
139139
}
140140

141141
internal init(
@@ -180,7 +180,7 @@ struct _Deque<Element> {
180180
}
181181

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

@@ -189,7 +189,7 @@ struct _Deque<Element> {
189189
at start: Int,
190190
from source: UnsafeBufferPointer<Element>
191191
) -> Int {
192-
assert(start + source.count <= capacity)
192+
_internalInvariant(start + source.count <= capacity)
193193
guard source.count > 0 else { return start }
194194
ptr(at: start).initialize(from: source.baseAddress!, count: source.count)
195195
return start + source.count
@@ -200,7 +200,7 @@ struct _Deque<Element> {
200200
at start: Int,
201201
from source: UnsafeMutableBufferPointer<Element>
202202
) -> Int {
203-
assert(start + source.count <= capacity)
203+
_internalInvariant(start + source.count <= capacity)
204204
guard source.count > 0 else { return start }
205205
ptr(at: start).moveInitialize(from: source.baseAddress!, count: source.count)
206206
return start + source.count
@@ -224,7 +224,7 @@ struct _Deque<Element> {
224224

225225
internal func moveElements(minimumCapacity: Int) -> _Storage {
226226
let count = self.count
227-
assert(minimumCapacity >= count)
227+
_internalInvariant(minimumCapacity >= count)
228228
let object = _Storage._DequeBuffer.create(
229229
minimumCapacity: minimumCapacity,
230230
makingHeaderWith: {
@@ -413,3 +413,13 @@ struct _Deque<Element> {
413413
}
414414
}
415415

416+
@_alwaysEmitIntoClient @_transparent
417+
internal func _internalInvariant(
418+
_ condition: @autoclosure () -> Bool,
419+
_ message: @autoclosure () -> String = String(),
420+
file: StaticString = #file, line: UInt = #line
421+
) {
422+
#if INTERNAL_CHECKS_ENABLED
423+
assert(condition(), message(), file: file, line: line)
424+
#endif
425+
}

validation-test/stdlib/Assert.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ Assert.test("_internalInvariant")
203203
.crashOutputMatches("this should fail")
204204
.code {
205205
var x = 2
206-
_internalInvariant(x * 21 == 42, "should not fail")
206+
Swift._internalInvariant(x * 21 == 42, "should not fail")
207207
expectCrashLater()
208-
_internalInvariant(x == 42, "this should fail")
208+
Swift._internalInvariant(x == 42, "this should fail")
209209
}
210210

211211
Assert.test("_internalInvariantFailure")

0 commit comments

Comments
 (0)