Skip to content

Add custom AnyHashable representations to bridged Foundation types #1649

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

Closed

Conversation

lorentey
Copy link
Member

@lorentey lorentey commented Aug 2, 2018

When converted to AnyHashable, both forms of bridged types should hash the same way and compare as identical. To implement this, we need to add _HasCustomAnyHashableRepresentation conformances to all bridgable NSObject subclasses.

This PR brings the set of types with custom AnyHashable representations in swift-corelibs-foundation in sync with the Foundation overlay in apple/swift.

Resolves https://bugs.swift.org/browse/SR-7436

Note that the new tests require the hashing fixes in #1645, #1646, #1647 and #1648.

This ensures bridgable values are interchangeable with their bridged counterparts when converted to AnyHashable, improving the usefulness of AnyHashable and increasing consistency across platforms.
@lorentey lorentey requested review from millenomi and parkera August 2, 2018 14:19
@lorentey lorentey changed the title Add custom AnyHashable representations to Foundation types with bridging Add custom AnyHashable representations to bridged Foundation types Aug 2, 2018
@lorentey
Copy link
Member Author

lorentey commented Aug 2, 2018

Please test with the following PRs:
#1645
#1646
#1647
#1648

@swift-ci please test

@lorentey
Copy link
Member Author

lorentey commented Aug 2, 2018

Please test with the following PRs:
#1645
#1646
#1647
#1648

@swift-ci please test

@parkera parkera requested a review from michael-lehew August 2, 2018 17:48
@lorentey
Copy link
Member Author

lorentey commented Aug 2, 2018

Hm, this looks like a legitimate issue:

01:20:02.190 Test Case 'TestAffineTransform.test_NSCoding' started at 2018-08-02 17:57:37.333
01:20:02.193 Fatal error: Unexpectedly found nil while unwrapping an Optional value: file Foundation/Dictionary.swift, line 92
01:20:02.193 Current stack trace:
01:20:02.194 0    libswiftCore.so                    0x00007ff2a5b5c220 _swift_stdlib_reportFatalErrorInFile + 115
01:20:02.194 1    libswiftCore.so                    0x00007ff2a5a7c794 <unavailable> + 3999636
01:20:02.194 2    libswiftCore.so                    0x00007ff2a5841fc0 <unavailable> + 1662912
01:20:02.194 3    libswiftCore.so                    0x00007ff2a5841c2f <unavailable> + 1661999
01:20:02.195 4    libswiftCore.so                    0x00007ff2a584025f <unavailable> + 1655391
01:20:02.195 5    libswiftCore.so                    0x00007ff2a583fe30 _fatalErrorMessage(_:_:file:line:flags:) + 44
01:20:02.195 6    libFoundation.so                   0x00007ff2a53fbc40 static Dictionary._unconditionallyBridgeFromObjectiveC(_:) + 339
01:20:02.195 7    libFoundation.so                   0x00007ff2a53fbbc0 static Dictionary._forceBridgeFromObjectiveC(_:result:) + 90
01:20:02.196 8    libFoundation.so                   0x00007ff2a53fdc18 <unavailable> + 10177560
01:20:02.196 9    libswiftCore.so                    0x00007ff2a5848240 _forceBridgeFromObjectiveC_bridgeable<A>(_:_:) + 134
01:20:02.196 10   libFoundation.so                   0x00007ff2a4ff1770 NSDictionary._toCustomAnyHashable() + 125
01:20:02.196 11   libFoundation.so                   0x00007ff2a4ff1850 <unavailable> + 5933136
01:20:02.196 12   libswiftCore.so                    0x00007ff2a5900090 AnyHashable.init<A>(_:) + 231
01:20:02.197 13   libswiftCore.so                    0x00007ff2a58332b0 _convertToAnyHashable<A>(_:) + 78
01:20:02.197 14   libFoundation.so                   0x00007ff2a50b1f20 NSKeyedArchiver._haveVisited(_:) + 500
01:20:02.197 15   libFoundation.so                   0x00007ff2a50b2270 NSKeyedArchiver._addObject(_:) + 53
01:20:02.198 16   libFoundation.so                   0x00007ff2a50b4930 NSKeyedArchiver._classReference(_:) + 597
01:20:02.198 17   libFoundation.so                   0x00007ff2a50b5940 NSKeyedArchiver._encodeObject(_:conditional:) + 2099
01:20:02.198 18   libFoundation.so                   0x00007ff2a50b68a0 NSKeyedArchiver._encodeObject(_:forKey:conditional:) + 91
01:20:02.198 19   libFoundation.so                   0x00007ff2a50b6a20 NSKeyedArchiver.encode(_:) + 82
01:20:02.199 20   libFoundation.so                   0x00007ff2a50b83d0 NSKeyedArchiver.encodeValue(ofObjCType:at:) + 1153

@lorentey
Copy link
Member Author

lorentey commented Aug 3, 2018

Ah, it's https://bugs.swift.org/browse/SR-7284 in a new disguise. [Any] is not Hashable, so it's not directly convertible to AnyHashable. However, the conversion succeeds if we first convert it to AnyObject (a.k.a NSArray), which blurs type information. 😶

print(["foo", "bar"] as [Any] as? AnyHashable) // ⇒ nil
print(["foo", "bar"] as [Any] as AnyObject as? AnyHashable) // ⇒ Optional(AnyHashable([AnyHashable("foo"), AnyHashable("bar")]))

@lorentey
Copy link
Member Author

lorentey commented Aug 3, 2018

@swift-ci test

@millenomi
Copy link
Contributor

(While I'm not happy that we have bugs, I'm happy when the bugs are the same across platforms, so that if and when we face them we can fix them on all platforms.)

@lorentey
Copy link
Member Author

lorentey commented Aug 3, 2018

Please test with the following PRs:
#1645
#1646
#1647
#1648

@swift-ci please test

@lorentey
Copy link
Member Author

lorentey commented Aug 6, 2018

Please test with the following PR:
#1656

@swift-ci test

@lorentey lorentey force-pushed the custom-AnyHashable-representations branch from 8057125 to dd824ed Compare August 6, 2018 16:35
@lorentey
Copy link
Member Author

lorentey commented Aug 6, 2018

Please test with the following PR:
#1656

@swift-ci test

@millenomi
Copy link
Contributor

@swift-ci please test

@millenomi
Copy link
Contributor

Oh, this patch has merge conflicts. Can you resolve them?

@lorentey
Copy link
Member Author

Yes, I'll take a look soon!

@parkera
Copy link
Contributor

parkera commented Jul 19, 2024

Closing older PRs after re-core of swift-corelibs-foundation on swift-foundation (#5001).

@parkera parkera closed this Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants