Skip to content

IRGen: Make type(of:) behavior consistent in ObjC bridged contexts. #15798

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 1 commit into from
Apr 9, 2018

Conversation

jckarter
Copy link
Contributor

@jckarter jckarter commented Apr 6, 2018

When we use type(of: x) on a class in an ObjC bridged context, the optimizer turns this into a SIL value_metatype @objc operation, which is supposed to get the dynamic type of the object as an ObjC class. This was previously lowered by IRGen into a object_getClass call, which extracts the isa pointer from the object, but is inconsistent with the -class method in ObjC or with the Swift-native behavior, which both look through artificial subclasses, proxies, and so on. This inconsistency led to observably different behavior between debug and release builds and between ObjC-bridged and native entry points, so provide an alternative runtime entry point that replicates the behavior of getting a native Swift class. Fixes SR-7258 | rdar://problem/38797313.

@jckarter
Copy link
Contributor Author

jckarter commented Apr 6, 2018

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2018

Build failed
Swift Test OS X Platform
Git Sha - 9c62fadfbc3d940dd9987552e66651123661ac19

When we use type(of: x) on a class in an ObjC bridged context, the optimizer turns this into a SIL `value_metatype @objc` operation, which is supposed to get the dynamic type of the object as an ObjC class. This was previously lowered by IRGen into a `object_getClass` call, which extracts the isa pointer from the object, but is inconsistent with the `-class` method in ObjC or with the Swift-native behavior, which both look through artificial subclasses, proxies, and so on. This inconsistency led to observably different behavior between debug and release builds and between ObjC-bridged and native entry points, so provide an alternative runtime entry point that replicates the behavior of getting a native Swift class. Fixes SR-7258.
@jckarter jckarter force-pushed the objc-value-metatype branch from 9c62fad to a4aa054 Compare April 6, 2018 22:17
@jckarter
Copy link
Contributor Author

jckarter commented Apr 6, 2018

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2018

Build failed
Swift Test OS X Platform
Git Sha - 9c62fadfbc3d940dd9987552e66651123661ac19

@swift-ci
Copy link
Contributor

swift-ci commented Apr 6, 2018

Build failed
Swift Test Linux Platform
Git Sha - 9c62fadfbc3d940dd9987552e66651123661ac19

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

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

LGTM.

I am assuming that the lookup in swift_getObjCClassFromObject of objc class objects has to be done this way based on Greg's description here: http://sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

Should there be an executable test that would have failed before this commit based on sr7258?

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