-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Protocol conformance is “stripped” from static library. #62056
Comments
Static linking normally requires something else to refer to a symbol for it to be incorporated into the final product. Nothing refers to protocol conformance descriptors, so they won't be included. Apple's linker provides a Failing that, the linker also has a big hammer in the form of |
@mikeash Thanks for the information. That is very helpful to know, and Is this considered the expected behavior of static libraries? Seems like a very serious gotcha. |
It is expected, and rather unfortunate. The overall problem is fundamental to the model of how static linking works. It would probably make sense to make |
Ok, thanks again for the info! It was very helpful. I'll close this out. |
Glad to help, and sorry for the trouble! |
@mbrandonw Should we keep this open, since the behavior is not desirable, although expected? We definitely want something done about it sooner or later. |
Sure thing, opened! |
It definitely isn't a Swift bug — at most, it's a request for a linker feature (making Static linking (with a library) has always worked by searching the library for symbols that are undefined and including just the object files (within the library) that define those symbols, then repeating until all symbols have been located. The point of doing this is that, for statically linked executables, you don't want to pull in e.g. the entire C library just because they might at some point have called We don't necessarily know at compile time which protocol conformances we're going to need, so we can't emit references to them to cause the static linker to bring in the right object file(s) automatically, hence the need to tell the linker to enable the unusual behaviour that is also needed for ObjC code where it will include object files simply because they have ObjC or Swift metadata in them, even if there are no references to that metadata from outside. I'm going to close it again here, but as I say, please do file a feedback request for the linker folks. |
Describe the bug
It is possible to have a protocol conformance in a static library that is not recognizable from another library. If you do a dynamic type check via
is any Q
it will fail even though the value definitely conforms toQ
.Suppose you have the following protocol hierarchy in a static library:
And you create a conformance to
P
andQ
in separate files:Then, in a separate library, checking for a dynamic conformance fails, but a direct check succeeds:
I have a project prepared to demonstrate the problems. Open the StaticStrippingApp.swift file for instructions on how to reproduce.
So far I have found 3 ways to prevent Swift from stripping the
Q
conformance:StaticLibrary
toDynamicLibrary
you will see that the problem is fixed.Expected behavior
I would expect that dynamically checking a conformance on
Q
to return true instead of false.Environment (please fill out the following information)
The text was updated successfully, but these errors were encountered: