Skip to content

[stdlib] fix getting eager-lazy-bridged Array buffer #78647

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 3 commits into from
Jan 15, 2025
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
5 changes: 2 additions & 3 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,8 @@ extension _ArrayBuffer {
_storage.objCInstance,
_ArrayBuffer.associationKey
) {
let buffer = assocPtr.load(
as: _ContiguousArrayStorage<Element>.self
)
let buffer: _ContiguousArrayStorage<Element>
buffer = Unmanaged.fromOpaque(assocPtr).takeUnretainedValue()
return _ContiguousArrayBuffer(buffer)
}
return nil
Expand Down
42 changes: 42 additions & 0 deletions test/stdlib/BridgedArrayNonContiguous.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===--- BridgedArrayNonContiguous.swift ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// RUN: %target-run-stdlib-swift -enable-experimental-feature LifetimeDependence -enable-experimental-feature Span

// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_Span

import StdlibUnittest

import Foundation

var suite = TestSuite("EagerLazyBridgingTests")
defer { runAllTests() }

var x: NSObject? = nil

suite.test("Bridged NSArray without direct memory sharing") {

var arr = Array(repeating: NSObject(), count: 100) as [AnyObject]
let nsArray:NSArray = NSArray(objects: &arr, count: 100)
for _ in 0 ..< 5 {
let tmp = nsArray as! [NSObject]
x = tmp.withContiguousStorageIfAvailable {
$0[0]
}
expectNotNil(x)
}

x = nil
}