Skip to content

Commit c3b75ee

Browse files
authored
Merge pull request swiftlang#79580 from xedin/extensible-enums
[Serialization/TypeChecker] Introduce `ExtensibleEnums` feature
2 parents 4c0e141 + b84bf05 commit c3b75ee

File tree

15 files changed

+190
-8
lines changed

15 files changed

+190
-8
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/Basic/Features.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CustomAvailability, true)
450450
/// Be strict about the Sendable conformance of metatypes.
451451
EXPERIMENTAL_FEATURE(StrictSendableMetatypes, true)
452452

453+
/// Allow public enumerations to be extensible by default
454+
/// regardless of whether the module they are declared in
455+
/// is resilient or not.
456+
EXPERIMENTAL_FEATURE(ExtensibleEnums, true)
457+
453458
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
454459
#undef EXPERIMENTAL_FEATURE
455460
#undef UPCOMING_FEATURE

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/AST/Decl.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6696,8 +6696,22 @@ bool EnumDecl::hasOnlyCasesWithoutAssociatedValues() const {
66966696
}
66976697

66986698
bool EnumDecl::treatAsExhaustiveForDiags(const DeclContext *useDC) const {
6699-
return isFormallyExhaustive(useDC) ||
6700-
(useDC && getModuleContext()->inSamePackage(useDC->getParentModule()));
6699+
if (useDC) {
6700+
auto *enumModule = getModuleContext();
6701+
if (enumModule->inSamePackage(useDC->getParentModule()))
6702+
return true;
6703+
6704+
// If the module where enum is declared supports extensible enumerations
6705+
// and this enum is not explicitly marked as "@frozen", cross-module
6706+
// access cannot be exhaustive and requires `@unknown default:`.
6707+
if (enumModule->supportsExtensibleEnums() &&
6708+
!getAttrs().hasAttribute<FrozenAttr>()) {
6709+
if (useDC != enumModule->getDeclContext())
6710+
return false;
6711+
}
6712+
}
6713+
6714+
return isFormallyExhaustive(useDC);
67016715
}
67026716

67036717
bool EnumDecl::isFormallyExhaustive(const DeclContext *useDC) const {

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ UNINTERESTING_FEATURE(SuppressedAssociatedTypes)
122122
UNINTERESTING_FEATURE(StructLetDestructuring)
123123
UNINTERESTING_FEATURE(MacrosOnImports)
124124
UNINTERESTING_FEATURE(AsyncCallerExecution)
125+
UNINTERESTING_FEATURE(ExtensibleEnums)
125126

126127
static bool usesFeatureNonescapableTypes(Decl *decl) {
127128
auto containsNonEscapable =

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/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,13 +1154,19 @@ namespace {
11541154
assert(defaultReason == RequiresDefault::No);
11551155
Type subjectType = Switch->getSubjectExpr()->getType();
11561156
bool shouldIncludeFutureVersionComment = false;
1157+
bool shouldDowngradeToWarning = true;
11571158
if (auto *theEnum = subjectType->getEnumOrBoundGenericEnum()) {
1159+
auto *enumModule = theEnum->getParentModule();
11581160
shouldIncludeFutureVersionComment =
1159-
theEnum->getParentModule()->isSystemModule();
1161+
enumModule->isSystemModule() ||
1162+
enumModule->supportsExtensibleEnums();
1163+
// Since the module enabled `ExtensibleEnums` feature they
1164+
// opted-in all of their clients into exhaustivity errors.
1165+
shouldDowngradeToWarning = !enumModule->supportsExtensibleEnums();
11601166
}
11611167
DE.diagnose(startLoc, diag::non_exhaustive_switch_unknown_only,
11621168
subjectType, shouldIncludeFutureVersionComment)
1163-
.warnUntilSwiftVersion(6);
1169+
.warnUntilSwiftVersionIf(shouldDowngradeToWarning, 6);
11641170
mainDiagType = std::nullopt;
11651171
}
11661172
break;

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();

0 commit comments

Comments
 (0)