Skip to content

🍒[cxx-interop] Do not emit C++ interop flag in textual interfaces #77989

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 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,12 @@ def enable_experimental_concise_pound_file : Flag<["-"],

def enable_experimental_cxx_interop :
Flag<["-"], "enable-experimental-cxx-interop">,
Flags<[NoDriverOption, FrontendOption, HelpHidden, ModuleInterfaceOption]>,
Flags<[NoDriverOption, FrontendOption, HelpHidden]>,
HelpText<"Enable experimental C++ interop code generation and config directives">;

def cxx_interoperability_mode :
Joined<["-"], "cxx-interoperability-mode=">,
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption,
Flags<[FrontendOption, SwiftSymbolGraphExtractOption,
SwiftSynthesizeInterfaceOption]>,
HelpText<"Enables C++ interoperability; pass 'default' to enable or 'off' to disable">;

Expand Down
21 changes: 15 additions & 6 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2014,12 +2014,8 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
GenericArgs.push_back(blocklist);
}

// For now, we only inherit the C++ interoperability mode in
// Explicit Module Builds.
if (langOpts.EnableCXXInterop &&
(frontendOpts.DisableImplicitModules ||
LoaderOpts.requestedAction ==
FrontendOptions::ActionType::ScanDependencies)) {
// Inherit the C++ interoperability mode.
if (langOpts.EnableCXXInterop) {
// Modelled after a reverse of validateCxxInteropCompatibilityMode
genericSubInvocation.getLangOptions().EnableCXXInterop = true;
genericSubInvocation.getLangOptions().cxxInteropCompatVersion =
Expand Down Expand Up @@ -2208,6 +2204,19 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
BuildArgs.push_back("-module-name");
BuildArgs.push_back(moduleName);

// FIXME: Hack for Darwin.swiftmodule, which cannot be rebuilt with C++
// interop enabled by the Swift CI because it uses an old host SDK.
if (moduleName == "Darwin") {
subInvocation.getLangOptions().EnableCXXInterop = false;
subInvocation.getLangOptions().cxxInteropCompatVersion = {};
BuildArgs.erase(llvm::remove_if(BuildArgs,
[](StringRef arg) -> bool {
return arg.starts_with(
"-cxx-interoperability-mode=");
}),
BuildArgs.end());
}

// Calculate output path of the module.
llvm::SmallString<256> buffer;
StringRef CacheHash;
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,10 @@ swift::getDisallowedOriginKind(const Decl *decl,
if (SF->getASTContext().LangOpts.EnableCXXInterop && where.getDeclContext() &&
where.getDeclContext()->getAsDecl() &&
where.getDeclContext()->getAsDecl()->getModuleContext()->isResilient() &&
!where.getDeclContext()
->getAsDecl()
->getModuleContext()
->getUnderlyingModuleIfOverlay() &&
decl->hasClangNode() && !decl->getModuleContext()->isSwiftShimsModule() &&
isFragileClangNode(decl->getClangNode()) &&
!SF->getASTContext().LangOpts.hasFeature(
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/Cxx/std/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
-cxx-interoperability-mode=default
-Xfrontend -module-interface-preserve-types-as-written

# This flag is unnecessary when building with newer compilers that allow
# using C++ symbols in resilient overlays (see f4204568).
-enable-experimental-feature AssumeResilientCxxTypes

SWIFT_COMPILE_FLAGS_LINUX
Expand Down
9 changes: 6 additions & 3 deletions test/Interop/Cxx/modules/emit-module-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
// Check if fragile Swift interface with struct
// extensions can be reparsed:
// RUN: %target-swift-frontend -swift-version 5 -typecheck -emit-module-interface-path %t/UsesCxxStruct.swiftinterface %s -I %S/Inputs -swift-version 5 -enable-experimental-cxx-interop %S/Inputs/namespace-extension-lib.swift
// RUN: %target-swift-frontend -swift-version 5 -typecheck-module-from-interface %t/UsesCxxStruct.swiftinterface -I %S/Inputs
// RUN: %target-swift-frontend -swift-version 5 -typecheck-module-from-interface %t/UsesCxxStruct.swiftinterface -I %S/Inputs -enable-experimental-cxx-interop
// RUN: %FileCheck --input-file=%t/UsesCxxStruct.swiftinterface %s
// CHECK: -enable-experimental-cxx-interop

// The textual module interface should not contain the C++ interop flag.
// CHECK-NOT: -enable-experimental-cxx-interop
// CHECK-NOT: -cxx-interoperability-mode


// Check if resilient Swift interface with builtin
// type extensions can be reparsed:
// RUN: %target-swift-emit-module-interface(%t/ResilientStruct.swiftinterface) %s -I %S/Inputs -enable-library-evolution -swift-version 5 -enable-experimental-cxx-interop %S/Inputs/namespace-extension-lib.swift -DRESILIENT
// RUN: %target-swift-typecheck-module-from-interface(%t/ResilientStruct.swiftinterface) -I %S/Inputs -DRESILIENT
// RUN: %target-swift-typecheck-module-from-interface(%t/ResilientStruct.swiftinterface) -I %S/Inputs -DRESILIENT -enable-experimental-cxx-interop
// RUN: %FileCheck --input-file=%t/ResilientStruct.swiftinterface %s

import Namespaces
Expand Down
9 changes: 2 additions & 7 deletions validation-test/ParseableInterface/verify_all_overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@
"DifferentiationUnittest",
"Swift",
"SwiftLang",
"std", # swiftstd uses `-module-interface-preserve-types-as-written`
# swiftCxxStdlib uses `-module-interface-preserve-types-as-written`
"CxxStdlib",
]:
continue

# Cxx and CxxStdlib are built without library evolution and don't have a
# .swiftinterface file
if module_name in ["Cxx", "CxxStdlib"]:
if not os.path.exists(interface_file):
continue

# swift -build-module-from-parseable-interface
output_path = os.path.join(output_dir, module_name + ".swiftmodule")
compiler_args = ["-o", output_path, "-module-name", module_name,
Expand Down