Skip to content

Commit 8113a1a

Browse files
committed
[Serialization] Serialize a flag that indicates whether ExtensibleEnum feature is supported by a module
When `ExtensibleEnums` flag is set, it's going to be reflected in the module file produced by the compiler to make sure that consumers know that non-`@frozen` enumerations can gain new cases in the future and switching cannot be exhaustive.
1 parent dac2e98 commit 8113a1a

File tree

10 files changed

+50
-4
lines changed

10 files changed

+50
-4
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
736736
HasAnyUnavailableDuringLoweringValues : 1
737737
);
738738

739-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+8,
739+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+8,
740740
/// If the module is compiled as static library.
741741
StaticLibrary : 1,
742742

@@ -805,7 +805,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
805805
SerializePackageEnabled : 1,
806806

807807
/// Whether this module has enabled strict memory safety checking.
808-
StrictMemorySafety : 1
808+
StrictMemorySafety : 1,
809+
810+
/// Whether this module has enabled `ExtensibleEnums` feature.
811+
ExtensibleEnums : 1
809812
);
810813

811814
SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,

include/swift/AST/Module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,14 @@ class ModuleDecl
840840
Bits.ModuleDecl.ObjCNameLookupCachePopulated = value;
841841
}
842842

843+
bool supportsExtensibleEnums() const {
844+
return Bits.ModuleDecl.ExtensibleEnums;
845+
}
846+
847+
void setSupportsExtensibleEnums(bool value = true) {
848+
Bits.ModuleDecl.ExtensibleEnums = value;
849+
}
850+
843851
/// For the main module, retrieves the list of primary source files being
844852
/// compiled, that is, the files we're generating code for.
845853
ArrayRef<SourceFile *> getPrimarySourceFiles() const;

include/swift/Serialization/Validation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ class ExtendedValidationInfo {
150150
unsigned AllowNonResilientAccess: 1;
151151
unsigned SerializePackageEnabled: 1;
152152
unsigned StrictMemorySafety: 1;
153+
unsigned SupportsExtensibleEnums : 1;
153154
} Bits;
155+
154156
public:
155157
ExtendedValidationInfo() : Bits() {}
156158

@@ -270,6 +272,11 @@ class ExtendedValidationInfo {
270272
version, SourceLoc(), /*Diags=*/nullptr))
271273
SwiftInterfaceCompilerVersion = genericVersion.value();
272274
}
275+
276+
bool supportsExtensibleEnums() const { return Bits.SupportsExtensibleEnums; }
277+
void setSupportsExtensibleEnums(bool val) {
278+
Bits.SupportsExtensibleEnums = val;
279+
}
273280
};
274281

275282
struct SearchPath {

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,8 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14571457
MainModule->setSerializePackageEnabled();
14581458
if (Invocation.getLangOptions().hasFeature(Feature::WarnUnsafe))
14591459
MainModule->setStrictMemorySafety(true);
1460+
if (Invocation.getLangOptions().hasFeature(Feature::ExtensibleEnums))
1461+
MainModule->setSupportsExtensibleEnums(true);
14601462

14611463
configureAvailabilityDomains(getASTContext(),
14621464
Invocation.getFrontendOptions(), MainModule);

lib/Serialization/ModuleFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,11 @@ class ModuleFile
707707
/// \c true if this module was built with strict memory safety.
708708
bool strictMemorySafety() const { return Core->strictMemorySafety(); }
709709

710+
/// \c true if this module was built with `ExtensibleEnums` feature enabled.
711+
bool supportsExtensibleEnums() const {
712+
return Core->supportsExtensibleEnums();
713+
}
714+
710715
/// Associates this module file with the AST node representing it.
711716
///
712717
/// Checks that the file is compatible with the AST module it's being loaded

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
224224
case options_block::STRICT_MEMORY_SAFETY:
225225
extendedInfo.setStrictMemorySafety(true);
226226
break;
227+
case options_block::EXTENSIBLE_ENUMS:
228+
extendedInfo.setSupportsExtensibleEnums(true);
229+
break;
227230
default:
228231
// Unknown options record, possibly for use by a future version of the
229232
// module format.
@@ -1501,6 +1504,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
15011504
Bits.AllowNonResilientAccess = extInfo.allowNonResilientAccess();
15021505
Bits.SerializePackageEnabled = extInfo.serializePackageEnabled();
15031506
Bits.StrictMemorySafety = extInfo.strictMemorySafety();
1507+
Bits.SupportsExtensibleEnums = extInfo.supportsExtensibleEnums();
15041508
MiscVersion = info.miscVersion;
15051509
SDKVersion = info.sdkVersion;
15061510
ModuleABIName = extInfo.getModuleABIName();

lib/Serialization/ModuleFileSharedCore.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,11 @@ class ModuleFileSharedCore {
418418
/// Whether this module enabled strict memory safety.
419419
unsigned StrictMemorySafety : 1;
420420

421+
/// Whether this module enabled has `ExtensibleEnums` feature enabled.
422+
unsigned SupportsExtensibleEnums : 1;
423+
421424
// Explicitly pad out to the next word boundary.
422-
unsigned : 2;
425+
unsigned : 1;
423426
} Bits = {};
424427
static_assert(sizeof(ModuleBits) <= 8, "The bit set should be small");
425428

@@ -678,6 +681,8 @@ class ModuleFileSharedCore {
678681

679682
bool strictMemorySafety() const { return Bits.StrictMemorySafety; }
680683

684+
bool supportsExtensibleEnums() const { return Bits.SupportsExtensibleEnums; }
685+
681686
/// How should \p dependency be loaded for a transitive import via \c this?
682687
///
683688
/// If \p importNonPublicDependencies, more transitive dependencies

lib/Serialization/ModuleFormat.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 922; // function type isolation - caller
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 923; // ExtensibleEnums feature
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -974,6 +974,7 @@ namespace options_block {
974974
PUBLIC_MODULE_NAME,
975975
SWIFT_INTERFACE_COMPILER_VERSION,
976976
STRICT_MEMORY_SAFETY,
977+
EXTENSIBLE_ENUMS,
977978
};
978979

979980
using SDKPathLayout = BCRecordLayout<
@@ -1083,6 +1084,10 @@ namespace options_block {
10831084
SWIFT_INTERFACE_COMPILER_VERSION,
10841085
BCBlob // version tuple
10851086
>;
1087+
1088+
using ExtensibleEnumsLayout = BCRecordLayout<
1089+
EXTENSIBLE_ENUMS
1090+
>;
10861091
}
10871092

10881093
/// The record types within the input block.

lib/Serialization/Serialization.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,11 @@ void Serializer::writeHeader() {
11791179
static_cast<uint8_t>(M->getCXXStdlibKind()));
11801180
}
11811181

1182+
if (M->supportsExtensibleEnums()) {
1183+
options_block::ExtensibleEnumsLayout ExtensibleEnums(Out);
1184+
ExtensibleEnums.emit(ScratchRecord);
1185+
}
1186+
11821187
if (Options.SerializeOptionsForDebugging) {
11831188
options_block::SDKPathLayout SDKPath(Out);
11841189
options_block::XCCLayout XCC(Out);

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,8 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
10901090
if (!loadedModuleFile->getModulePackageName().empty()) {
10911091
M.setPackageName(Ctx.getIdentifier(loadedModuleFile->getModulePackageName()));
10921092
}
1093+
if (loadedModuleFile->supportsExtensibleEnums())
1094+
M.setSupportsExtensibleEnums();
10931095
M.setUserModuleVersion(loadedModuleFile->getUserModuleVersion());
10941096
M.setSwiftInterfaceCompilerVersion(
10951097
loadedModuleFile->getSwiftInterfaceCompilerVersion());

0 commit comments

Comments
 (0)