Skip to content

[5.3][stdlib] Fix Array.append(contentsOf:) for arguments of type NSArray #34445

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

Conversation

lorentey
Copy link
Member

Cherry-picked from #34426, reviewed by @kylemacomber.

Due to a couple of unfortunate circumstances, appending an NSArray instance to an Array instance does not actually append any elements. The cause is #29220, which accidentally optimized away the actual loop that appends the elements in this particular case. (And only this particular case, which is why this wasn’t detected by the test suite.)

When the argument to Array.append(contentsOf:) is of type NSArray, the newElements is [Element] expression is compiled into a runtime check that returns true, eliminating the subsequent loop over the remaining items of the iterator. Sadly, NSArray.underestimatedCount currently returns 0, so the earlier _copyContents call is a noop, so no elements get added to self at all.

Turning the is test into a direct equality check between the metatype instances resolves the issue.

Resolves rdar://70448247.

Due to a couple of unfortunate circumstances, appending an NSArray instance to an Array instance does not actually append any elements.

The cause is swiftlang#29220, which accidentally optimized away the actual loop that appends the elements in this particular case. (And only this particular case, which is why this wasn’t detected by the test suite.)

When the argument to `Array.append(contentsOf:)` is of type NSArray, the `newElements is [Element]` expression is compiled into a runtime check that returns true, eliminating the subsequent loop over the remaining items of the iterator. Sadly, NSArray.underestimatedCount` currently returns 0, so the earlier _copyContents call is a noop, so no elements get added to `self` at all.

Turning the `is` test into a direct equality check between the metatype instances resolves the issue.

(cherry picked from commit 184367c)
@lorentey lorentey requested a review from a team as a code owner October 26, 2020 21:23
@lorentey
Copy link
Member Author

@swift-ci test

@lorentey
Copy link
Member Author

Windows failure looks unrelated:

ci@SWIFTCI-WINDOWS S:\jenkins\workspace\swift-PR-windows>EME"
'EME"' is not recognized as an internal or external command,
operable program or batch file.

@lorentey
Copy link
Member Author

@swift-ci test windows platform

1 similar comment
@lorentey
Copy link
Member Author

@swift-ci test windows platform

@lorentey lorentey merged commit 3250715 into swiftlang:release/5.3-20201012 Oct 27, 2020
@lorentey lorentey deleted the array-cannot-append-5.3 branch October 27, 2020 06:17
@AnthonyLatsis AnthonyLatsis added the 🍒 release cherry pick Flag: Release branch cherry picks label May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants