Skip to content

Commit ba8d4d7

Browse files
committed
[cxx-interop] compilations that do not enable C++ interoperability should not be able to import modules that do enable C++ interoperability by default
A supplemental hidden frontend option allows advanced users to opt-out of this requirement. Fixes #65833 Fixes #65832
1 parent 4d1d8a9 commit ba8d4d7

18 files changed

+104
-7
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
659659
HasAnyUnavailableValues : 1
660660
);
661661

662-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
662+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
663663
/// If the module is compiled as static library.
664664
StaticLibrary : 1,
665665

@@ -709,7 +709,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
709709

710710
/// If the map from @objc provided name to top level swift::Decl in this
711711
/// module is populated
712-
ObjCNameLookupCachePopulated : 1
712+
ObjCNameLookupCachePopulated : 1,
713+
714+
/// Whether this module has been built with C++ interoperability enabled.
715+
HasCxxInteroperability : 1
713716
);
714717

715718
SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,12 @@ ERROR(need_hermetic_seal_to_import_module,none,
874874
"module %0 was built with -experimental-hermetic-seal-at-link, but "
875875
"current compilation does not have -experimental-hermetic-seal-at-link",
876876
(Identifier))
877+
ERROR(need_cxx_interop_to_import_module,none,
878+
"module %0 was built with C++ interoperability enabled, but "
879+
"current compilation does not enable C++ interoperability",
880+
(Identifier))
881+
NOTE(enable_cxx_interop_docs,none,
882+
"visit https://www.swift.org/documentation/cxx-interop/project-build-setup to learn how to enable C++ interoperability", ())
877883

878884
ERROR(modularization_issue_decl_moved,Fatal,
879885
"reference to %select{top-level declaration|type}0 %1 broken by a context change; "

include/swift/AST/Module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,14 @@ class ModuleDecl
654654
Bits.ModuleDecl.HasHermeticSealAtLink = enabled;
655655
}
656656

657+
/// Returns true if this module was built with C++ interoperability enabled.
658+
bool hasCxxInteroperability() const {
659+
return Bits.ModuleDecl.HasCxxInteroperability;
660+
}
661+
void setHasCxxInteroperability(bool enabled = true) {
662+
Bits.ModuleDecl.HasCxxInteroperability = enabled;
663+
}
664+
657665
/// \returns true if this module is a system module; note that the StdLib is
658666
/// considered a system module.
659667
bool isSystemModule() const {

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ namespace swift {
313313
/// Imports getters and setters as computed properties.
314314
bool CxxInteropGettersSettersAsProperties = false;
315315

316+
/// Should the compiler require C++ interoperability to be enabled
317+
/// when importing Swift modules that enable C++ interoperability.
318+
bool RequireCxxInteropToImportCxxInteropModule = true;
319+
316320
/// On Darwin platforms, use the pre-stable ABI's mark bit for Swift
317321
/// classes instead of the stable ABI's bit. This is needed when
318322
/// targeting OSes prior to macOS 10.14.4 and iOS 12.2, where

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,11 @@ def cxx_interop_getters_setters_as_properties :
865865
HelpText<"Import getters and setters as computed properties in Swift">,
866866
Flags<[FrontendOption, HelpHidden]>;
867867

868+
def cxx_interop_disable_requirement_at_import :
869+
Flag<["-"], "disable-cxx-interop-requirement-at-import">,
870+
HelpText<"Do not require C++ interoperability to be enabled when importing a Swift module that enables C++ interoperability">,
871+
Flags<[FrontendOption, HelpHidden]>;
872+
868873
def use_malloc : Flag<["-"], "use-malloc">,
869874
HelpText<"Allocate internal data structures using malloc "
870875
"(for memory debugging)">;

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ExtendedValidationInfo {
131131
unsigned IsBuiltFromInterface : 1;
132132
unsigned IsAllowModuleWithCompilerErrorsEnabled : 1;
133133
unsigned IsConcurrencyChecked : 1;
134+
unsigned HasCxxInteroperability : 1;
134135
} Bits;
135136
public:
136137
ExtendedValidationInfo() : Bits() {}
@@ -232,6 +233,10 @@ class ExtendedValidationInfo {
232233
void setIsConcurrencyChecked(bool val = true) {
233234
Bits.IsConcurrencyChecked = val;
234235
}
236+
bool hasCxxInteroperability() const { return Bits.HasCxxInteroperability; }
237+
void setHasCxxInteroperability(bool val) {
238+
Bits.HasCxxInteroperability = val;
239+
}
235240
};
236241

237242
struct SearchPath {

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
646646
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
647647
Bits.ModuleDecl.IsConcurrencyChecked = 0;
648648
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
649+
Bits.ModuleDecl.HasCxxInteroperability = 0;
649650
}
650651

651652
void ModuleDecl::setIsSystemModule(bool flag) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10431043
Args.hasArg(OPT_experimental_c_foreign_reference_types);
10441044

10451045
Opts.CxxInteropGettersSettersAsProperties = Args.hasArg(OPT_cxx_interop_getters_setters_as_properties);
1046+
Opts.RequireCxxInteropToImportCxxInteropModule =
1047+
!Args.hasArg(OPT_cxx_interop_disable_requirement_at_import);
10461048

10471049
Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);
10481050

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,9 @@ ModuleDecl *CompilerInstance::getMainModule() const {
13191319
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
13201320
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))
13211321
MainModule->setIsConcurrencyChecked(true);
1322+
if (Invocation.getLangOptions().EnableCXXInterop &&
1323+
Invocation.getLangOptions().RequireCxxInteropToImportCxxInteropModule)
1324+
MainModule->setHasCxxInteroperability();
13221325

13231326
// Register the main module with the AST context.
13241327
Context->addLoadedModule(MainModule);

lib/Serialization/ModuleFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ class ModuleFile
621621
return Core->Bits.HasHermeticSealAtLink;
622622
}
623623

624+
/// Whether this module was built with C++ interoperability enabled.
625+
bool hasCxxInteroperability() const {
626+
return Core->Bits.HasCxxInteroperability;
627+
}
628+
624629
/// Whether the module is resilient. ('-enable-library-evolution')
625630
ResilienceStrategy getResilienceStrategy() const {
626631
return ResilienceStrategy(Core->Bits.ResilienceStrategy);

0 commit comments

Comments
 (0)