Skip to content

Commit 482c80e

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 1adcf99 commit 482c80e

18 files changed

+97
-7
lines changed

include/swift/AST/Decl.h

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

635-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
635+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
636636
/// If the module is compiled as static library.
637637
StaticLibrary : 1,
638638

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

683683
/// If the map from @objc provided name to top level swift::Decl in this
684684
/// module is populated
685-
ObjCNameLookupCachePopulated : 1
685+
ObjCNameLookupCachePopulated : 1,
686+
687+
/// Whether this module has been built with C++ interoperability enabled.
688+
HasCxxInteroperability : 1
686689
);
687690

688691
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
@@ -871,6 +871,12 @@ ERROR(need_hermetic_seal_to_import_module,none,
871871
"module %0 was built with -experimental-hermetic-seal-at-link, but "
872872
"current compilation does not have -experimental-hermetic-seal-at-link",
873873
(Identifier))
874+
ERROR(need_cxx_interop_to_import_module,none,
875+
"module %0 was built with C++ interoperability enabled, but "
876+
"current compilation does not enable C++ interoperability",
877+
(Identifier))
878+
NOTE(enable_cxx_interop_docs,none,
879+
"visit https://www.swift.org/documentation/cxx-interop/project-build-setup to learn how to enable C++ interoperability", ())
874880

875881
ERROR(modularization_issue_decl_moved,Fatal,
876882
"reference to %select{top-level|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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,8 @@ 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+
MainModule->setHasCxxInteroperability();
13221324

13231325
// Register the main module with the AST context.
13241326
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)