Skip to content

[Runtime] Avoid +class overrides when initializing an ObjC class. #24618

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

Merged
merged 2 commits into from
May 9, 2019

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented May 8, 2019

swift_getInitializedObjCClass called [c class] to trigger class initialization, and returned the value. This wreaked havoc when the class in question overrides +class. Instead, ignore the return value and return c. Switch from +class to +self, which is much less likely to be overridden. Calling an overridden method could have performance downsides or even cause unwanted side effects.

rdar://problem/49853091

swift_getInitializedObjCClass called [c class] to trigger class initialization, and returned the value. This wreaked havoc when the class in question overrides +class. Instead, ignore the return value and return c. Switch from +class to +self, which is much less likely to be overridden. Calling an overridden method could have performance downsides or even cause unwanted side effects.

rdar://problem/49853091
@mikeash mikeash requested a review from jrose-apple May 8, 2019 20:18
@mikeash
Copy link
Contributor Author

mikeash commented May 8, 2019

@swift-ci please test

@jrose-apple
Copy link
Contributor

I think it's possible to write a terrible test case for this that would fail the old way.

@mikeash
Copy link
Contributor Author

mikeash commented May 8, 2019

I think you're right! +class { return nil; } ought to do it, along with something that triggers this path. I'll give that a try.

@mikeash
Copy link
Contributor Author

mikeash commented May 9, 2019

That does indeed trigger a failure with the old code. New code works.

@mikeash
Copy link
Contributor Author

mikeash commented May 9, 2019

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented May 9, 2019

Build failed
Swift Test Linux Platform
Git Sha - 213efbb

@swift-ci
Copy link
Contributor

swift-ci commented May 9, 2019

Build failed
Swift Test OS X Platform
Git Sha - 213efbb

@@ -0,0 +1,25 @@
// RUN: mkdir -p %t
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: %empty-directory(%t) will also clear out any contents left over from the last run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. We have a bunch of tests with each one, and I happened to crib from one with mkdir. Seems better to do it with empty-directory though.

@mikeash mikeash force-pushed the self-awareness-class-struggle branch from 3faade6 to 893e291 Compare May 9, 2019 17:11
@mikeash
Copy link
Contributor Author

mikeash commented May 9, 2019

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented May 9, 2019

Build failed
Swift Test Linux Platform
Git Sha - 3faade61910b2d8a68a02b88527a9ce06f8df029

@swift-ci
Copy link
Contributor

swift-ci commented May 9, 2019

Build failed
Swift Test OS X Platform
Git Sha - 3faade61910b2d8a68a02b88527a9ce06f8df029

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