Skip to content

Commit 1fa0120

Browse files
committed
stdlib: remove the inout-violation check from Array bounds checking
As we have exclusivity checking since a long time, this "explicit" check for inout violations is not needed anymore. The `_checkInoutAndNativeTypeCheckedBounds` function must remain in the library for backward compatibility. The remaining relevant subscript index checking from that function is now simply inlined in into the caller.
1 parent 77757c4 commit 1fa0120

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

stdlib/public/core/Array.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,13 @@ extension Array {
386386
_ index: Int, wasNativeTypeChecked: Bool
387387
) -> _DependenceToken {
388388
#if _runtime(_ObjC)
389-
_buffer._checkInoutAndNativeTypeCheckedBounds(
390-
index, wasNativeTypeChecked: wasNativeTypeChecked)
389+
// There is no need to do bounds checking for the non-native case because
390+
// ObjectiveC arrays do bounds checking by their own.
391+
// And in the native-non-type-checked case, it's also not needed to do bounds
392+
// checking here, because it's done in ArrayBuffer._getElementSlowPath.
393+
if _fastPath(wasNativeTypeChecked) {
394+
_buffer._native._checkValidSubscript(index)
395+
}
391396
#else
392397
_buffer._checkValidSubscript(index)
393398
#endif

stdlib/public/core/ArrayBuffer.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ extension _ArrayBuffer {
422422
/// wasNative == _isNative in the absence of inout violations.
423423
/// Because the optimizer can hoist the original check it might have
424424
/// been invalidated by illegal user code.
425+
///
426+
/// This function is obsolete but must stay in the library for backward
427+
/// compatibility.
425428
@inlinable
426429
internal func _checkInoutAndNativeBounds(_ index: Int, wasNative: Bool) {
427430
_precondition(
@@ -439,6 +442,9 @@ extension _ArrayBuffer {
439442
/// wasNativeTypeChecked == _isNativeTypeChecked in the absence of
440443
/// inout violations. Because the optimizer can hoist the original
441444
/// check it might have been invalidated by illegal user code.
445+
///
446+
/// This function is obsolete but must stay in the library for backward
447+
/// compatibility.
442448
@inlinable
443449
internal func _checkInoutAndNativeTypeCheckedBounds(
444450
_ index: Int, wasNativeTypeChecked: Bool

0 commit comments

Comments
 (0)