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

[SR-10507] Dynamic cast to existential fails when conformance is added in extension in static library #52907

Closed
ChristopherRogers opened this issue Apr 18, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@ChristopherRogers
Copy link
Contributor

Previous ID SR-10507
Radar rdar://problem/50169873
Original Reporter @ChristopherRogers
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Swift 5.0.1, macOS 10.14.4

Swift Development Snapshot toolchain, 2019-04-11

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 0cd9932cf7032fccdb1ff06896f91884

duplicates:

  • SR-6004 Static libraries don't automatically load extensions

Issue Description:

I've attached sample code & a build script to demonstrate the problem. All of the "My*.swift" files are a part of MyModule, which is built as a static library. main.swift is for an executable that will import MyModule.

MyClass conforms to MyProtocol via an extension, which is present in a separate file. Attempting to dynamically cast MyClass to MyProtocol will either fail or crash at runtime, depending on how the cast is performed.

Three ways to workaround this problem:

  1. Move the protocol conformance to the same file as the class definition

  2. Coerce MyClass to MyProtocol first before attempting to dynamically cast to MyProtocol

  3. Build MyModule as a dylib

@belkadan
Copy link
Contributor

A fourth way that should work: explicitly pass -force_load libMyModule.a to the linker (possibly using -Xlinker if not invoking ld directly).

Thanks for the report. I think we have an old Radar that says this can happen, but it's valuable to know that someone hit it in the wild.

@belkadan
Copy link
Contributor

Bob found it.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants