Skip to content

[DO NOT MERGE][stdlib] Make assert(), _debugPreconditions() noops #35065

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 0 additions & 3 deletions stdlib/public/core/ArrayShared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ extension _ArrayBufferProtocol {
/// always guaranteed by measuring `s` once and re-using that value.
@inlinable
internal func _expectEnd<C: Collection>(of s: C, is i: C.Index) {
_debugPrecondition(
i == s.endIndex,
"invalid Collection: count differed in successive traversals")
}

@inlinable
Expand Down
28 changes: 0 additions & 28 deletions stdlib/public/core/Assert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ public func assert(
_ message: @autoclosure () -> String = String(),
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())
}
}
}

/// Checks a necessary condition for making forward progress.
Expand Down Expand Up @@ -249,27 +242,6 @@ public func _overflowChecked<T>(
}


/// Debug library precondition checks.
///
/// Debug library precondition checks are only on in debug mode. In release and
/// in fast mode they are disabled. In debug mode they print an error message
/// and abort.
/// They are meant to be used when the check is not comprehensively checking for
/// all possible errors.
@usableFromInline @_transparent
internal func _debugPrecondition(
_ condition: @autoclosure () -> Bool, _ message: StaticString = StaticString(),
file: StaticString = #file, line: UInt = #line
) {
// Only check in debug mode.
if _slowPath(_isDebugAssertConfiguration()) {
if !_fastPath(condition()) {
_fatalErrorMessage("Fatal error", message, file: file, line: line,
flags: _fatalErrorFlags())
}
}
}

@usableFromInline @_transparent
internal func _debugPreconditionFailure(
_ message: StaticString = StaticString(),
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/BridgeObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ internal var _orphanedFoundationSubclassesReparented: Bool = false
/// in order to bridge Swift Strings, Arrays, Dictionarys, Sets, or Enumerators to ObjC.
internal func _connectOrphanedFoundationSubclassesIfNeeded() -> Void {
let bridgeWorks = _bridgeInitializedSuccessfully
_debugPrecondition(bridgeWorks)
_orphanedFoundationSubclassesReparented = true
}

Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ public func _unsafeReferenceCast<T, U>(_ x: T, to: U.Type) -> U {
/// - Returns: The instance `x`, cast to type `T`.
@_transparent
public func unsafeDowncast<T: AnyObject>(_ x: AnyObject, to type: T.Type) -> T {
_debugPrecondition(x is T, "invalid unsafeDowncast")
return Builtin.castReference(x)
}

Expand Down
2 changes: 0 additions & 2 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ extension Character :
public init(_ s: String) {
_precondition(!s.isEmpty,
"Can't form a Character from an empty String")
_debugPrecondition(s.index(after: s.startIndex) == s.endIndex,
"Can't form a Character from a String containing more than one extended grapheme cluster")

if _fastPath(s._guts._object.isPreferredRepresentation) {
self.init(unchecked: s)
Expand Down
2 changes: 0 additions & 2 deletions stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,6 @@ internal func _copyCollectionToContiguousArray<
count: count)
var (itr, end) = source._copyContents(initializing: p)

_debugPrecondition(itr.next() == nil,
"invalid Collection: more than 'count' elements in collection")
// We also have to check the evil shrink case in release builds, because
// it can result in uninitialized array elements and therefore undefined
// behavior.
Expand Down
2 changes: 0 additions & 2 deletions stdlib/public/core/DiscontiguousSlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ extension MutableCollection {
set {
var indexOfReplacement = newValue.startIndex
for range in subranges.ranges {
_debugPrecondition(!range.isEmpty, "Empty range in a range set")

var indexToReplace = range.lowerBound
repeat {
_precondition(
Expand Down
13 changes: 0 additions & 13 deletions stdlib/public/core/EmptyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,9 @@ extension EmptyCollection: RandomAccessCollection, MutableCollection {
@inlinable // trivial-implementation
public subscript(bounds: Range<Index>) -> SubSequence {
get {
_debugPrecondition(bounds.lowerBound == 0 && bounds.upperBound == 0,
"Index out of range")
return self
}
set {
_debugPrecondition(bounds.lowerBound == 0 && bounds.upperBound == 0,
"Index out of range")
}
}

Expand All @@ -128,39 +124,30 @@ extension EmptyCollection: RandomAccessCollection, MutableCollection {

@inlinable // trivial-implementation
public func index(_ i: Index, offsetBy n: Int) -> Index {
_debugPrecondition(i == startIndex && n == 0, "Index out of range")
return i
}

@inlinable // trivial-implementation
public func index(
_ i: Index, offsetBy n: Int, limitedBy limit: Index
) -> Index? {
_debugPrecondition(i == startIndex && limit == startIndex,
"Index out of range")
return n == 0 ? i : nil
}

/// The distance between two indexes (always zero).
@inlinable // trivial-implementation
public func distance(from start: Index, to end: Index) -> Int {
_debugPrecondition(start == 0, "From must be startIndex (or endIndex)")
_debugPrecondition(end == 0, "To must be endIndex (or startIndex)")
return 0
}

@inlinable // trivial-implementation
public func _failEarlyRangeCheck(_ index: Index, bounds: Range<Index>) {
_debugPrecondition(index == 0, "out of bounds")
_debugPrecondition(bounds == indices, "invalid bounds for an empty collection")
}

@inlinable // trivial-implementation
public func _failEarlyRangeCheck(
_ range: Range<Index>, bounds: Range<Index>
) {
_debugPrecondition(range == indices, "invalid range for an empty collection")
_debugPrecondition(bounds == indices, "invalid bounds for an empty collection")
}
}

Expand Down
12 changes: 0 additions & 12 deletions stdlib/public/core/ManagedBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,6 @@ extension ManagedBufferPointer {
internal static func _checkValidBufferClass(
_ bufferClass: AnyClass, creating: Bool = false
) {
_debugPrecondition(
_class_getInstancePositiveExtentSize(bufferClass) == MemoryLayout<_HeapObject>.size
|| (
(!creating || bufferClass is ManagedBuffer<Header, Element>.Type)
&& _class_getInstancePositiveExtentSize(bufferClass)
== _headerOffset + MemoryLayout<Header>.size),
"ManagedBufferPointer buffer class has illegal stored properties"
)
_debugPrecondition(
_usesNativeSwiftReferenceCounting(bufferClass),
"ManagedBufferPointer buffer class must be non-@objc"
)
}

@inlinable
Expand Down
6 changes: 0 additions & 6 deletions stdlib/public/core/MutableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,6 @@ extension MutableCollection {
internal mutating func _swapNonemptySubrangePrefixes(
_ lhs: Range<Index>, _ rhs: Range<Index>
) -> (Index, Index) {
assert(!lhs.isEmpty)
assert(!rhs.isEmpty)

var p = lhs.lowerBound
var q = rhs.lowerBound
repeat {
Expand Down Expand Up @@ -435,9 +432,6 @@ public func swap<T>(_ a: inout T, _ b: inout T) {
// Microoptimized to avoid retain/release traffic.
let p1 = Builtin.addressof(&a)
let p2 = Builtin.addressof(&b)
_debugPrecondition(
p1 != p2,
"swapping a location with itself is not supported")

// Take from P1.
let tmp: T = Builtin.take(p1)
Expand Down
2 changes: 0 additions & 2 deletions stdlib/public/core/Pointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ func _convertMutableArrayToPointerArgument<

// Call reserve to force contiguous storage.
a.reserveCapacity(0)
_debugPrecondition(a._baseAddressIfContiguous != nil || a.isEmpty)

return _convertConstArrayToPointerArgument(a)
}

Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ where Bound: Strideable, Bound.Stride: SignedInteger
@inlinable
public subscript(position: Index) -> Element {
// FIXME: swift-3-indexing-model: tests for the range check.
_debugPrecondition(self.contains(position), "Index out of range")
return position
}
}
Expand Down
6 changes: 0 additions & 6 deletions stdlib/public/core/RangeSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ public struct RangeSet<Bound: Comparable> {
/// ranges share an upper and lower bound — `[0..<5, 5..<10]` is ill-formed,
/// and would instead be represented as `[0..<10]`.
internal func _checkInvariants() {
for (a, b) in zip(ranges, ranges.dropFirst()) {
_debugPrecondition(!a.isEmpty && !b.isEmpty, "Empty range in range set")
_debugPrecondition(
a.upperBound < b.lowerBound,
"Out of order/overlapping ranges in range set")
}
}

/// Creates a new range set from `ranges`, which satisfies the range set
Expand Down
7 changes: 0 additions & 7 deletions stdlib/public/core/UIntBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ extension _UIntBuffer: RangeReplaceableCollection {
@inlinable
@inline(__always)
public mutating func append(_ newElement: Element) {
_debugPrecondition(count + 1 <= capacity)
_storage &= ~(Storage(Element.max) &<< _bitCount)
_storage |= Storage(newElement) &<< _bitCount
_bitCount = _bitCount &+ _elementWidth
Expand All @@ -193,7 +192,6 @@ extension _UIntBuffer: RangeReplaceableCollection {
@inline(__always)
@discardableResult
public mutating func removeFirst() -> Element {
_debugPrecondition(!isEmpty)
let result = Element(truncatingIfNeeded: _storage)
_bitCount = _bitCount &- _elementWidth
_storage = _storage._fullShiftRight(_elementWidth)
Expand All @@ -205,16 +203,11 @@ extension _UIntBuffer: RangeReplaceableCollection {
public mutating func replaceSubrange<C: Collection>(
_ target: Range<Index>, with replacement: C
) where C.Element == Element {
_debugPrecondition(
(0..<_bitCount)._contains_(
target.lowerBound.bitOffset..<target.upperBound.bitOffset))

let replacement1 = _UIntBuffer(replacement)

let targetCount = distance(
from: target.lowerBound, to: target.upperBound)
let growth = replacement1.count &- targetCount
_debugPrecondition(count + growth <= capacity)

let headCount = distance(from: startIndex, to: target.lowerBound)
let tailOffset = distance(from: startIndex, to: target.upperBound)
Expand Down
21 changes: 0 additions & 21 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,11 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
@inlinable // unsafe-performance
public func _failEarlyRangeCheck(_ index: Int, bounds: Range<Int>) {
// NOTE: In release mode, this method is a no-op for performance reasons.
_debugPrecondition(index >= bounds.lowerBound)
_debugPrecondition(index < bounds.upperBound)
}

@inlinable // unsafe-performance
public func _failEarlyRangeCheck(_ range: Range<Int>, bounds: Range<Int>) {
// NOTE: In release mode, this method is a no-op for performance reasons.
_debugPrecondition(range.lowerBound >= bounds.lowerBound)
_debugPrecondition(range.upperBound <= bounds.upperBound)
}

@inlinable // unsafe-performance
Expand Down Expand Up @@ -268,14 +264,10 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
@inlinable // unsafe-performance
public subscript(i: Int) -> Element {
get {
_debugPrecondition(i >= 0)
_debugPrecondition(i < endIndex)
return _position._unsafelyUnwrappedUnchecked[i]
}
%if Mutable:
nonmutating _modify {
_debugPrecondition(i >= 0)
_debugPrecondition(i < endIndex)
yield &_position._unsafelyUnwrappedUnchecked[i]
}
%end
Expand Down Expand Up @@ -341,17 +333,11 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
-> Slice<Unsafe${Mutable}BufferPointer<Element>>
{
get {
_debugPrecondition(bounds.lowerBound >= startIndex)
_debugPrecondition(bounds.upperBound <= endIndex)
return Slice(
base: self, bounds: bounds)
}
% if Mutable:
nonmutating set {
_debugPrecondition(bounds.lowerBound >= startIndex)
_debugPrecondition(bounds.upperBound <= endIndex)
_debugPrecondition(bounds.count == newValue.count)

// FIXME: swift-3-indexing-model: tests.
if !newValue.isEmpty {
(_position! + bounds.lowerBound).assign(
Expand All @@ -375,8 +361,6 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
@inlinable // unsafe-performance
public func swapAt(_ i: Int, _ j: Int) {
guard i != j else { return }
_debugPrecondition(i >= 0 && j >= 0)
_debugPrecondition(i < endIndex && j < endIndex)
let pi = (_position! + i)
let pj = (_position! + j)
let tmp = pi.move()
Expand Down Expand Up @@ -440,10 +424,6 @@ extension Unsafe${Mutable}BufferPointer {
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R? {
let (oldBase, oldCount) = (self.baseAddress, self.count)
defer {
_debugPrecondition((oldBase, oldCount) == (self.baseAddress, self.count),
"UnsafeMutableBufferPointer.withUnsafeMutableBufferPointer: replacing the buffer is not allowed")
}
return try body(&self)
}

Expand Down Expand Up @@ -674,7 +654,6 @@ extension Unsafe${Mutable}BufferPointer {
to type: T.Type, _ body: (${Self}<T>) throws -> Result
) rethrows -> Result {
if let base = _position {
_debugPrecondition(MemoryLayout<Element>.stride == MemoryLayout<T>.stride)
Builtin.bindMemory(base._rawValue, count._builtinWordValue, T.self)
defer {
Builtin.bindMemory(base._rawValue, count._builtinWordValue, Element.self)
Expand Down
19 changes: 0 additions & 19 deletions stdlib/public/core/UnsafePointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
@inlinable
public func initialize(repeating repeatedValue: Pointee, count: Int) {
// FIXME: add tests (since the `count` has been added)
_debugPrecondition(count >= 0,
"UnsafeMutablePointer.initialize(repeating:count:): negative count")
// Must not use `initializeFrom` with a `Collection` as that will introduce
// a cycle.
for offset in 0..<count {
Expand Down Expand Up @@ -719,7 +717,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
/// `count` must not be negative.
@inlinable
public func assign(repeating repeatedValue: Pointee, count: Int) {
_debugPrecondition(count >= 0, "UnsafeMutablePointer.assign(repeating:count:) with negative count")
for i in 0..<count {
self[i] = repeatedValue
}
Expand All @@ -743,8 +740,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
/// `source` to this pointer's memory. `count` must not be negative.
@inlinable
public func assign(from source: UnsafePointer<Pointee>, count: Int) {
_debugPrecondition(
count >= 0, "UnsafeMutablePointer.assign with negative count")
if UnsafePointer(self) < source || UnsafePointer(self) >= source + count {
// assign forward from a disjoint or following overlapping range.
Builtin.assignCopyArrayFrontToBack(
Expand Down Expand Up @@ -787,8 +782,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
public func moveInitialize(
@_nonEphemeral from source: UnsafeMutablePointer, count: Int
) {
_debugPrecondition(
count >= 0, "UnsafeMutablePointer.moveInitialize with negative count")
if self < source || self >= source + count {
// initialize forward from a disjoint or following overlapping range.
Builtin.takeArrayFrontToBack(
Expand Down Expand Up @@ -827,12 +820,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
/// pointer's memory. `count` must not be negative.
@inlinable
public func initialize(from source: UnsafePointer<Pointee>, count: Int) {
_debugPrecondition(
count >= 0, "UnsafeMutablePointer.initialize with negative count")
_debugPrecondition(
UnsafePointer(self) + count <= source ||
source + count <= UnsafePointer(self),
"UnsafeMutablePointer.initialize overlapping range")
Builtin.copyArray(
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
// This builtin is equivalent to:
Expand Down Expand Up @@ -860,11 +847,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
public func moveAssign(
@_nonEphemeral from source: UnsafeMutablePointer, count: Int
) {
_debugPrecondition(
count >= 0, "UnsafeMutablePointer.moveAssign(from:) with negative count")
_debugPrecondition(
self + count <= source || source + count <= self,
"moveAssign overlapping range")
Builtin.assignTakeArray(
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
// These builtins are equivalent to:
Expand All @@ -887,7 +869,6 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
@inlinable
@discardableResult
public func deinitialize(count: Int) -> UnsafeMutableRawPointer {
_debugPrecondition(count >= 0, "UnsafeMutablePointer.deinitialize with negative count")
// FIXME: optimization should be implemented, where if the `count` value
// is 1, the `Builtin.destroy(Pointee.self, _rawValue)` gets called.
Builtin.destroyArray(Pointee.self, _rawValue, count._builtinWordValue)
Expand Down
Loading