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

Foundation: more idiomatic Swift in NSKeyedArchiver #3160

Merged
merged 5 commits into from Dec 12, 2022

Conversation

lhoward
Copy link
Contributor

@lhoward lhoward commented Mar 28, 2022

Originally when I implemented NSValue, I intended for it to be a class cluster with the concrete classes being NSSpecialValue or NSConcreteValue. At the time, Swift didn't support this pattern. Now it does support reassigning self (also see 00737ae), it makes sense to revisit this.

Also, make some other related code paths more idiomatically Swift, by eliminating force-unwrap and force-cast; at the time, I was less familiar with Swift.

Unfortunately since I cannot build the TestFoundation target on macOS, it's not possible for me to run the tests (I don't have a Linux VM handy right now).

@lhoward lhoward changed the title Foundation: use class cluster in NSValue implementation Foundation: more idiomatic Swift in NSKeyedArchiver Mar 28, 2022
@millenomi
Copy link
Contributor

I am interested in code that aligns behavior, but I'm less interested in code that modifies e.g. object identity to look like Darwin's unless that is something that a user could detect or that changes the semantics of their code. Is it?

@millenomi
Copy link
Contributor

In general, we've spent so much work moving away from the factory self pattern I'm not super interested in having people spend effort to reverse that.

@lhoward
Copy link
Contributor Author

lhoward commented Mar 30, 2022

Fair enough. Note, though, that the _Factory extension was added by another party; this patch simply takes advantage of it. Also, having a side table is pretty inelegant and adds synchronisation overhead.

@millenomi
Copy link
Contributor

Those are arguments I can buy on deviating from these principles. Let me look.

@millenomi
Copy link
Contributor

@swift-ci please test

@lhoward
Copy link
Contributor Author

lhoward commented Apr 5, 2022

Four tests failing here:

Test Case 'TestNSKeyedArchiver.test_archive_concrete_value' started at 2022-04-05 22:28:38.896
Foundation/NSKeyedUnarchiver.swift:286: Fatal error: Value was of unexpected class NSValue
Current stack trace:
0    libswiftCore.so                    0x00007f313dca1b00 _swift_stdlib_reportFatalErrorInFile + 112
1    libswiftCore.so                    0x00007f313d98c5dc <unavailable> + 1439196
2    libswiftCore.so                    0x00007f313d98c403 <unavailable> + 1438723
3    libswiftCore.so                    0x00007f313d98af10 _assertionFailure(_:_:file:line:flags:) + 411
4    libFoundation.so                   0x00007f313e5575a0 NSKeyedUnarchiver._isClassAllowed(_:allowedClasses:) + 959
5    libFoundation.so                   0x00007f313e557a50 NSKeyedUnarchiver._validateAndMapClassDictionary(_:allowedClasses:classToConstruct:) + 1331
6    libFoundation.so                   0x00007f313e5586a0 NSKeyedUnarchiver._validateAndMapClassReference(_:allowedClasses:) + 663
7    libFoundation.so                   0x00007f313e559ac0 NSKeyedUnarchiver._decodeObject(_:) + 2182
8    libFoundation.so                   0x00007f313e55bc50 NSKeyedUnarchiver._decodeObject(forKey:) + 360
9    libFoundation.so                   0x00007f313e55d0a0 NSKeyedUnarchiver._decodeObject(of:forKey:) + 324
10   libFoundation.so                   0x00007f313e55d4b0 NSKeyedUnarchiver.decodeObject(of:forKey:) + 103

@lhoward
Copy link
Contributor Author

lhoward commented Apr 6, 2022

There was a bug in the test when constructing the list of expected classes, it was expecting the concrete type (reported by type(of:)) rather than the cluster type (which could be obtained by NSObject.classForCoder). Fixed now, although again because I cannot build or run the tests on macOS, it may require further fixes.

@lhoward
Copy link
Contributor Author

lhoward commented Apr 6, 2022

@swift-ci please test

@AtariDreams
Copy link
Contributor

Any updates on this @parkera

@lhoward
Copy link
Contributor Author

lhoward commented Oct 3, 2022

Are you able to run an automated test again? Don't think I can do it

@parkera
Copy link
Member

parkera commented Oct 5, 2022

@swift-ci test

@lhoward
Copy link
Contributor Author

lhoward commented Oct 6, 2022

OK, still breaking on test, investigating.

Use first(where:) in NSSpecialValue instead of iterating loop, as it is more
idiomatic.
Eliminate some force-unwrap/force-casts in NSKeyedArchiver and
NSKeyedUnarchiver. Some notes about whether unarchiver delegate is called when
decoding nil objects.
Unwrapped variable can have same name as optional.
@lhoward
Copy link
Contributor Author

lhoward commented Oct 6, 2022

OK, perhaps this will fix it...

@parkera
Copy link
Member

parkera commented Oct 6, 2022

@swift-ci test

@lhoward
Copy link
Contributor Author

lhoward commented Oct 7, 2022

Seems to pass tests, I also rebased against main when fixing, ready for review I guess?

@lhoward
Copy link
Contributor Author

lhoward commented Dec 9, 2022

Just following up on this one.

@parkera parkera merged commit 7f08595 into apple:main Dec 12, 2022
@parkera
Copy link
Member

parkera commented Dec 12, 2022

Thanks!

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.

None yet

4 participants