Skip to content

Commit 8bf8c24

Browse files
authored
Merge pull request swiftlang#80503 from xedin/extensible-attr
[AST/Sema] Add `@extensible` attribute on `enum` declarations
2 parents 95bd086 + bf19481 commit 8bf8c24

25 files changed

+103
-78
lines changed

include/swift/AST/Decl.h

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

738-
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,
738+
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,
739739
/// If the module is compiled as static library.
740740
StaticLibrary : 1,
741741

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

806806
/// Whether this module has enabled strict memory safety checking.
807-
StrictMemorySafety : 1,
808-
809-
/// Whether this module has enabled `ExtensibleEnums` feature.
810-
ExtensibleEnums : 1
807+
StrictMemorySafety : 1
811808
);
812809

813810
SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,

include/swift/AST/DeclAttr.def

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,12 @@ SIMPLE_DECL_ATTR(constInitialized, ConstInitialized,
879879
168)
880880
DECL_ATTR_FEATURE_REQUIREMENT(ConstInitialized, CompileTimeValues)
881881

882-
LAST_DECL_ATTR(ConstInitialized)
882+
SIMPLE_DECL_ATTR(extensible, Extensible,
883+
OnEnum,
884+
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove | ForbiddenInABIAttr,
885+
169)
886+
887+
LAST_DECL_ATTR(Extensible)
883888

884889
#undef DECL_ATTR_ALIAS
885890
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/AST/DiagnosticsSema.def

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8577,6 +8577,19 @@ GROUPED_WARNING(
85778577
"behavior",
85788578
(StringRef, DeclAttribute))
85798579

8580+
//===----------------------------------------------------------------------===//
8581+
// MARK: @extensible Attribute
8582+
//===----------------------------------------------------------------------===//
8583+
8584+
ERROR(extensible_attr_on_frozen_type,none,
8585+
"cannot use '@extensible' together with '@frozen'", ())
8586+
8587+
ERROR(extensible_attr_on_internal_type,none,
8588+
"'@extensible' attribute can only be applied to public or package "
8589+
"declarations, but %0 is "
8590+
"%select{private|fileprivate|internal|%error|%error|%error}1",
8591+
(DeclName, AccessLevel))
8592+
85808593
//===----------------------------------------------------------------------===//
85818594
// MARK: SwiftSettings
85828595
//===----------------------------------------------------------------------===//

include/swift/AST/Module.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,6 @@ 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-
851843
/// For the main module, retrieves the list of primary source files being
852844
/// compiled, that is, the files we're generating code for.
853845
ArrayRef<SourceFile *> getPrimarySourceFiles() const;

include/swift/Basic/Features.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,6 @@ ADOPTABLE_EXPERIMENTAL_FEATURE(AsyncCallerExecution, false)
495495
/// Allow custom availability domains to be defined and referenced.
496496
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CustomAvailability, true)
497497

498-
/// Allow public enumerations to be extensible by default
499-
/// regardless of whether the module they are declared in
500-
/// is resilient or not.
501-
EXPERIMENTAL_FEATURE(ExtensibleEnums, true)
502-
503498
/// Allow isolated conformances.
504499
EXPERIMENTAL_FEATURE(IsolatedConformances, true)
505500

include/swift/Serialization/Validation.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ class ExtendedValidationInfo {
150150
unsigned AllowNonResilientAccess: 1;
151151
unsigned SerializePackageEnabled: 1;
152152
unsigned StrictMemorySafety: 1;
153-
unsigned SupportsExtensibleEnums : 1;
154153
} Bits;
155154

156155
public:
@@ -272,11 +271,6 @@ class ExtendedValidationInfo {
272271
version, SourceLoc(), /*Diags=*/nullptr))
273272
SwiftInterfaceCompilerVersion = genericVersion.value();
274273
}
275-
276-
bool supportsExtensibleEnums() const { return Bits.SupportsExtensibleEnums; }
277-
void setSupportsExtensibleEnums(bool val) {
278-
Bits.SupportsExtensibleEnums = val;
279-
}
280274
};
281275

282276
struct SearchPath {

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,6 +4925,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
49254925
TRIVIAL_ATTR_PRINTER(Used, used)
49264926
TRIVIAL_ATTR_PRINTER(WarnUnqualifiedAccess, warn_unqualified_access)
49274927
TRIVIAL_ATTR_PRINTER(WeakLinked, weak_linked)
4928+
TRIVIAL_ATTR_PRINTER(Extensible, extensible)
49284929

49294930
#undef TRIVIAL_ATTR_PRINTER
49304931

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
402402
DeclAttrKind::RestatedObjCConformance,
403403
DeclAttrKind::NonSendable,
404404
DeclAttrKind::AllowFeatureSuppression,
405+
DeclAttrKind::Extensible,
405406
};
406407

407408
return result;

lib/AST/Decl.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6854,14 +6854,11 @@ bool EnumDecl::treatAsExhaustiveForDiags(const DeclContext *useDC) const {
68546854
if (enumModule->inSamePackage(useDC->getParentModule()))
68556855
return true;
68566856

6857-
// If the module where enum is declared supports extensible enumerations
6858-
// and this enum is not explicitly marked as "@frozen", cross-module
6859-
// access cannot be exhaustive and requires `@unknown default:`.
6860-
if (enumModule->supportsExtensibleEnums() &&
6861-
!getAttrs().hasAttribute<FrozenAttr>()) {
6862-
if (useDC != enumModule->getDeclContext())
6863-
return false;
6864-
}
6857+
// When the enum is marked as `@extensible` cross-module access
6858+
// cannot be exhaustive and requires `@unknown default:`.
6859+
if (getAttrs().hasAttribute<ExtensibleAttr>() &&
6860+
enumModule != useDC->getParentModule())
6861+
return false;
68656862
}
68666863

68676864
return isFormallyExhaustive(useDC);

lib/AST/FeatureSet.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ UNINTERESTING_FEATURE(SuppressedAssociatedTypes)
124124
UNINTERESTING_FEATURE(StructLetDestructuring)
125125
UNINTERESTING_FEATURE(MacrosOnImports)
126126
UNINTERESTING_FEATURE(AsyncCallerExecution)
127-
UNINTERESTING_FEATURE(ExtensibleEnums)
128127
UNINTERESTING_FEATURE(KeyPathWithMethodMembers)
129128

130129
static bool usesFeatureNonescapableTypes(Decl *decl) {

0 commit comments

Comments
 (0)