Skip to content
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

[SR-7145] Dictionary.Values does not satisfy MutableCollection requirements #49693

Open
lorentey opened this issue Mar 8, 2018 · 3 comments
Open
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@lorentey
Copy link
Member

lorentey commented Mar 8, 2018

Previous ID SR-7145
Radar rdar://problem/38394644
Original Reporter @lorentey
Type Bug
Environment

Swift 4.1
Swift 4.2

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @lorentey
Priority Medium

md5: 4e2e499f88977c2bd588f64fee40342d

Issue Description:

For Dictionaries bridged over from Objective-C, Dictionary.Values does not correctly implement MutableCollection.

MutableCollection has the semantic requirement that the subscript setter must not invalidate indices. However, the Dictionary.Values.subscript setter converts to native storage, which invalidates existing (Cocoa) indices as a side effect:

import Foundation

let d = NSDictionary(dictionary: ["Hello": 1, "World": 2]) as Dictionary
let i = d.index(forKey: "Hello" as NSString)!

var values = d.values
values[i] = 42 as NSNumber
print(values[i]) // Traps, invalid index

@lorentey
Copy link
Member Author

lorentey commented Mar 8, 2018

The least horrible solution seems to be that we keep Cocoa indices valid in the native dictionary.

Cocoa-flavored Dictionary indices happen to contain a reference to their corresponding key, so we can extract that and look it up in the new instance. (We want to migrate to NSEnumerator-based indices soon, but we'll still have to keep the current key in the new index structure.) :-(

@lorentey
Copy link
Member Author

lorentey commented Mar 8, 2018

@swift-ci create

@lorentey
Copy link
Member Author

lorentey commented Oct 4, 2018

#19576 implements support for using indices belonging to bridged dictionaries to subscript native dictionaries that originated from the same NSDictionary instance (amongst other index validation improvements).

#19694 enables native dictionaries to advance bridged indices.

Unfortunately, Dictionary.Values is still not quite a MutableCollection, because there is no way I can see to support comparing bridged and native indices. (They define a completely different ordering.)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant