Skip to content

Swift-to-C++: getTypeMetadata inline helpers that refer to C++ type metadata are annotated as used for debug compilations, causing linker errors #69234

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

Closed
hyp opened this issue Oct 17, 2023 · 0 comments · Fixed by #69519
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself linker error swift to c++ Feature → c++ interop: swift to c++ swift 5.10 swift 6.0

Comments

@hyp
Copy link
Contributor

hyp commented Oct 17, 2023

as reported on https://forums.swift.org/t/undeclared-use-of-identifier-in-generated-module-swift-file/67894.
It shows up as a linker error like this:

ld: Undefined symbols:
  type metadata accessor for __C.HelloCxxWorld, referenced from:
      swift::TypeMetadataTrait<HelloCxxWorld>::getTypeMetadata() in HelloHello.o

This is caused by how the C++ getTypeMetadata inline function is annotated in the generated header. It uses the SWIFT_INLINE_THUNK annotation which in debug mode in Xcode (when DEBUG is defined as a preprocessor macro/Swift build condition) is forced to be emitted into the object file, even when it's not used. This is wrong, as it's not supposed to be linked in forcefully and should only be needed when it's actually used.

Such functions should be annotated with SWIFT_INLINE_PRIVATE_HELPER instead.

@hyp hyp added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ triage needed This issue needs more specific labels labels Oct 17, 2023
@hyp hyp self-assigned this Oct 17, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself swift to c++ Feature → c++ interop: swift to c++ swift 6.0 linker error and removed triage needed This issue needs more specific labels labels Oct 18, 2023
hyp added a commit to hyp/swift that referenced this issue Oct 30, 2023
…tadata helper to avoid emitting a reference to it when 'DEBUG' macro is set

This is needed as the metadata for C++ types might not be actually emitted and thus we can't always reference it

Fixes swiftlang#69234

rdar://117089662
@hyp hyp added the swift 5.10 label Oct 30, 2023
hyp added a commit to hyp/swift that referenced this issue Oct 30, 2023
…tadata helper to avoid emitting a reference to it when 'DEBUG' macro is set

This is needed as the metadata for C++ types might not be actually emitted and thus we can't always reference it

Fixes swiftlang#69234

rdar://117089662
hyp added a commit to hyp/swift that referenced this issue Oct 31, 2023
…tadata helper to avoid emitting a reference to it when 'DEBUG' macro is set

This is needed as the metadata for C++ types might not be actually emitted and thus we can't always reference it

Fixes swiftlang#69234

rdar://117089662
hyp added a commit to hyp/swift that referenced this issue Oct 31, 2023
…tadata helper to avoid emitting a reference to it when 'DEBUG' macro is set

This is needed as the metadata for C++ types might not be actually emitted and thus we can't always reference it

Fixes swiftlang#69234

rdar://117089662
@hyp hyp closed this as completed in #69519 Nov 1, 2023
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. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself linker error swift to c++ Feature → c++ interop: swift to c++ swift 5.10 swift 6.0
Projects
None yet
2 participants