Skip to content

stdlib: remove the inout-violation check from Array bounds checking #59947

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
Jul 9, 2022
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
9 changes: 7 additions & 2 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,13 @@ extension Array {
_ index: Int, wasNativeTypeChecked: Bool
) -> _DependenceToken {
#if _runtime(_ObjC)
_buffer._checkInoutAndNativeTypeCheckedBounds(
index, wasNativeTypeChecked: wasNativeTypeChecked)
// There is no need to do bounds checking for the non-native case because
// ObjectiveC arrays do bounds checking by their own.
// And in the native-non-type-checked case, it's also not needed to do bounds
// checking here, because it's done in ArrayBuffer._getElementSlowPath.
if _fastPath(wasNativeTypeChecked) {
_buffer._native._checkValidSubscript(index)
}
#else
_buffer._checkValidSubscript(index)
#endif
Expand Down
6 changes: 6 additions & 0 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ extension _ArrayBuffer {
/// wasNative == _isNative in the absence of inout violations.
/// Because the optimizer can hoist the original check it might have
/// been invalidated by illegal user code.
///
/// This function is obsolete but must stay in the library for backward
/// compatibility.
@inlinable
internal func _checkInoutAndNativeBounds(_ index: Int, wasNative: Bool) {
_precondition(
Expand All @@ -439,6 +442,9 @@ extension _ArrayBuffer {
/// wasNativeTypeChecked == _isNativeTypeChecked in the absence of
/// inout violations. Because the optimizer can hoist the original
/// check it might have been invalidated by illegal user code.
///
/// This function is obsolete but must stay in the library for backward
/// compatibility.
@inlinable
internal func _checkInoutAndNativeTypeCheckedBounds(
_ index: Int, wasNativeTypeChecked: Bool
Expand Down