Skip to content

Conversation

@hjyamauchi
Copy link
Contributor

@hjyamauchi hjyamauchi commented Dec 8, 2025

We currently support static linking swiftCxxStdlib only on Windows. When C++ interop is enabled but swiftCxxStdlib isn't actually used or only when its underlying std module is used, we incorrectly emitted the dynamic version of the lib name and caused the link error. This change fixes it by always adding it to the dependency.

Issue #85876

@hjyamauchi
Copy link
Contributor Author

@swift-ci please test

@hjyamauchi hjyamauchi marked this pull request as ready for review December 8, 2025 16:54
@hjyamauchi
Copy link
Contributor Author

@swift-ci please test

@hjyamauchi hjyamauchi requested a review from compnerd December 12, 2025 16:13
[mainModuleName](StringRef Name) {
return mainModuleName == Name;
}))
if (ScanASTContext.LangOpts.Target.getOS() == llvm::Triple::Win32)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this scoped to just windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To match the IRGenModule code where it's scoped to windows

if (Context.LangOpts.Target.getOS() == llvm::Triple::Win32)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the IRGenModule code scoped to just windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think because the library names differ between static and dynamic on Windows and it needs to know whether it's static or dynamic #67776

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the Windows scoping here

@compnerd compnerd requested a review from egorzhdan December 12, 2025 16:28
@compnerd
Copy link
Member

I suspect that @egorzhdan might have some thoughts on this as well. Currently, IRGen will inject the CxxStdlib unconditionally, which this mirrors.

return mainModuleName == Name;
}))
if (ScanASTContext.LangOpts.Target.getOS() == llvm::Triple::Win32)
mainDependencies.addModuleImport("CxxStdlib", /* isExported */ false,
Copy link
Contributor

Choose a reason for hiding this comment

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

The string literal can be replaced with scanASTContext.Id_CxxStdlib.str()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

break;
}

if (ScanASTContext.LangOpts.EnableCXXInterop) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add a comment with a detailed explanation for why Windows behavior differs and why the default flow in

if (lookupCxxStdLibOverlay) {
is insufficient.

Copy link
Contributor Author

@hjyamauchi hjyamauchi Dec 12, 2025

Choose a reason for hiding this comment

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

We (and the IRGenModule code) currently assume that we always link with and add dependency on CxxStdlib if C++ interop is enabled (dynamic or static but it needs to be currently static linking for Windows.) Hence this approach in this PR. But it looks like this lookupCxxStdLibOverlay code doesn't seem to assume that. Do you know why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this code and the lookupCxxStdLibOverlay code are orthogonal. We currently and do always link with CxxStdlib if C++ interop is enabled, dynamic or static. So, we explicitly add CxxStdlib to the (transitive) dependency set by adding a dependency to the main/root module for all platforms but in particular for Windows because it needs to know if it's static or dynamic to determine the correct library name, which this new code does. The lookupCxxStdLibOverlay code differs in that it only adds a CxxStdlib dep if a module depends on the clang std module. We still need the lookupCxxStdLibOverlay code because it affects the direct dependencies of non-root/individual modules (like adding a CxxStdlib dep to what only has a dep on the underlying std module or not adding it to the Darwin module, etc.) as the no-cxx-overlay-for-non-interop-interface.swift test checks.

[mainModuleName](StringRef Name) {
return mainModuleName == Name;
}))
if (ScanASTContext.LangOpts.Target.getOS() == llvm::Triple::Win32)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the IRGenModule code scoped to just windows?

We currently support static linking swiftCxxStdlib only on
Windows. When C++ interop is enabled but swiftCxxStdlib isn't actually
used or only when its underlying std module is used, we incorrectly
emitted the dynamic version of the lib name and caused the link
error. This change fixes it by always adding it to the dependency.

Issue swiftlang#85876
@hjyamauchi
Copy link
Contributor Author

@swift-ci please test

@diggler30100
Copy link

diggler30100 commented Dec 15, 2025 via email

@hjyamauchi hjyamauchi requested a review from artemcm December 15, 2025 19:04
@hjyamauchi hjyamauchi merged commit b1058c8 into swiftlang:main Dec 15, 2025
5 checks passed
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.

4 participants