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

Fix Codable vtable layout #16057

Merged
merged 2 commits into from
Apr 20, 2018

Conversation

slavapestov
Copy link
Contributor

Fixes rdar://problem/35647420 and https://bugs.swift.org/browse/SR-6468.

@slavapestov
Copy link
Contributor Author

@lorentey This introduces a mechanism for derived conformances to define class methods with deterministic vtable order. Just remember to update TypeChecker::finalizeType() to force the conformance, and remember to call setSynthesized() on every method you add to the class.

@slavapestov slavapestov force-pushed the codable-vtable-layout branch 2 times, most recently from e19a237 to fa11300 Compare April 20, 2018 15:28
@slavapestov
Copy link
Contributor Author

@swift-ci Please test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - da47f19f8ae45d53ef166a2920f40e9e012af3f1

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - da47f19f8ae45d53ef166a2920f40e9e012af3f1

return elts.begin();
}

decltype(elts)::const_iterator end() const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if some of the less-capable C++ compilers we build with will handle this decltype idiom. I guess we'll find out


synthesizedMembers.sort();

for (auto pair : synthesizedMembers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const auto &pair to avoid the extraneous copies, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

TC.requestSuperclassLayout(CD);

auto useConformance = [&](ProtocolDecl *protocol) {
(void)TC.conformsToProtocol(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to check the conformance even if it's written on an extension, which will perform some unnecessary work. I guess that's going to be fairly rare for a class, but if you're concerned about it, you could drop the Used flag here and only force the completion of the conformance when it's DeclContext is the nominal type itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, fixed

auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
if (!evaluateTargetConformanceTo(decodableProto))
(void)evaluateTargetConformanceTo(encodableProto);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want to check for the name CodingKeys at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's checked with an early return at the top of the method

@lorentey
Copy link
Member

@slavapestov Thank you, this looks great! 🎉

@itaiferber
Copy link
Contributor

This looks good too! A lot of the changes here were also made in #16054 too; should this subsume that PR?

(Also, both PRs appear to have the same test failures in init checking from ObjC; possibly related to the changes in the DeclChecker?)

This ensures that the vtable of a class conforming to Encodable or
Decodable always contains encode(to:) and init(from:), respectively.

They might still get added in arbitrary order, however. This will
be addressed in a subsequent patch.

Mostly fixes <rdar://problem/35647420> and
<https://bugs.swift.org/browse/SR-6468>.
Completes the fix for <rdar://problem/35647420> and
<https://bugs.swift.org/browse/SR-6468>.
@slavapestov slavapestov force-pushed the codable-vtable-layout branch from fa11300 to 1602580 Compare April 20, 2018 19:36
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

1 similar comment
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

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.

5 participants