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-15257] Unpredictable behaviour of inserting elements in set #3201

Open
swift-ci opened this issue Sep 29, 2021 · 0 comments
Open

[SR-15257] Unpredictable behaviour of inserting elements in set #3201

swift-ci opened this issue Sep 29, 2021 · 0 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-15257
Radar None
Original Reporter wolfandrei (JIRA User)
Type Bug

Attachment: Download

Environment

macOS Big Sur v.11.6
Xcode Version 13.0 (13A233)
xcrun version 60.

swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)
Target: x86_64-apple-macosx11.0

Additional Detail from JIRA
Votes 0
Component/s Foundation, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 3732c4adf17a27bd2984513e86397863

Issue Description:

Unpredictable behaviour in the case of inserting class instance multiple times in set.
When a set was initialised as shown below then we can get 2 situations:

let a = NewClass(x: 1)
var set = Set<NewClass>()
set.insert(a)
a.x = 2
set.insert(a)
print(set.map { $0.x })

1) It is all ok and it prints:

[2]

2) Fatal error

Fatal error: Duplicate elements of type 'NewClass' were found in a Set.
This usually means either that the type violates Hashable's requirements, or
that members of such a set were mutated after insertion.

When a set was initialised from an array as shown below then we can get numerous situations:

let a = NewClass(x: 1)
var array: Array<NewClass> = [a, a]
a.x = 2
var set = Set(array)
a.x = 1
set.insert(a)
a.x = 3
set.insert(a)
print(set.map { $0.x })

It prints either [3] or [3, 3] or even [3, 3, 3].

I also understand that the changing of class's property that is take place in hash formation is unacceptable.

Attach 2 files with source code and compiled files if needed.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant