Skip to content

Obj-C class metatypes should never satisfy Obj-C existentials #69435

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

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Oct 26, 2023

Given this

@objc protocol P { func f() }
class C: P { func f() {} }

the casts C.self is any P and C.self as? any P should always fail, because the metatype of C.self does not have an f() implementation.

These casts previously succeeded because the runtime implementation deferred to Obj-C's [o conformsToProtocol:p], and that behaves differently depending on whether o is a class instance or a Class itself.

Since these casts should never succeed, I've just modified the Swift runtime logic to fail whenever the source of the cast is an Obj-C Class and the target is a protocol existential.

Resolves: rdar://106973771

Given this
```
@objc protocol P { func f() }
class C: P { func f() {} }
```
the casts `C.self is any P` and `C.self as? any P` should always fail,
because the metatype of `C.self` does not have an `f()` implementation.

These casts previously succeeded because the runtime implementation
deferred to Obj-C's `[o conformsToProtocol:p]`, and that behaves
differently depending on whether `o` is a class instance or
a Class itself.

Since these casts should never succeed, I've just modified the
Swift runtime logic to fail whenever the source of the cast
is an Obj-C Class and the target is a protocol existential.

Resolves: rdar://106973771
@tbkka tbkka requested a review from a team as a code owner October 26, 2023 19:04
@tbkka tbkka requested a review from mikeash October 26, 2023 19:04
@tbkka
Copy link
Contributor Author

tbkka commented Oct 26, 2023

@swift-ci Please test

@tbkka tbkka merged commit e8761b8 into swiftlang:main Oct 26, 2023
@tbkka tbkka deleted the tbkka-rdar106973771-ObjC-metatype-protocols branch October 26, 2023 23:28
tbkka added a commit that referenced this pull request Jan 23, 2024
Revert "Obj-C class metatypes should never satisfy Obj-C existentials" (#69435)
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.

2 participants