Skip to content

CastOptimizer: don't assume dynamic casts from ObjectiveC classes to unrelated classes will fail #81203

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
May 2, 2025

Conversation

eeckstein
Copy link
Contributor

In case of ObjectiveC classes, the runtime type can differ from its declared type. Therefore a cast between (compile-time) unrelated classes may succeed at runtime.

rdar://149810124

@eeckstein eeckstein requested a review from jckarter as a code owner April 30, 2025 14:41
…unrelated classes will fail

In case of ObjectiveC classes, the runtime type can differ from its declared type.
Therefore a cast between (compile-time) unrelated classes may succeed at runtime.

rdar://149810124
@eeckstein eeckstein force-pushed the fix-objc-class-casting branch from bc279ec to 7a5dbdc Compare April 30, 2025 14:43
Copy link
Contributor

@mikeash mikeash left a comment

Choose a reason for hiding this comment

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

Approving with sadness.

@eeckstein
Copy link
Contributor Author

@swift-ci apple silicon benchmark

@tbkka
Copy link
Contributor

tbkka commented Apr 30, 2025

For anyone curious about this change, consider this code:

class Foo {}
class Bar {}

let foo: Foo = ...

if foo is Foo { print("Foo") }
else if foo is Bar { print("Bar") }

"Obviously", the casts here can be optimized away and we're left with just print("Foo"). But in fact, if Foo and Bar are Obj-C class types, then we can't optimize this, because foo might really be a Bar -- Objective-C lets programmers get away with things like that. We're still going to optimize this for Swift types, though, since this situation occurs surprisingly often in inlined code.

@tbkka
Copy link
Contributor

tbkka commented May 1, 2025

@swift-ci Please test

@eeckstein eeckstein merged commit cc11a9b into swiftlang:main May 2, 2025
6 checks passed
@eeckstein eeckstein deleted the fix-objc-class-casting branch May 2, 2025 05:08
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