Skip to content

Commit 30a72ab

Browse files
authored
Merge pull request #81073 from xedin/extensible-enums-flag
[Frontend] Add `ExtensibleAttribute` to guard use of `@extensible` at…
2 parents 63db342 + 0ad7d8b commit 30a72ab

11 files changed

+58
-16
lines changed

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ SIMPLE_DECL_ATTR(extensible, Extensible,
880880
OnEnum,
881881
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove | ForbiddenInABIAttr,
882882
169)
883+
DECL_ATTR_FEATURE_REQUIREMENT(Extensible, ExtensibleAttribute)
883884

884885
SIMPLE_DECL_ATTR(concurrent, Concurrent,
885886
OnFunc | OnConstructor | OnSubscript | OnVar,

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ EXPERIMENTAL_FEATURE(AllowRuntimeSymbolDeclarations, true)
514514
/// Allow use of `@cdecl`
515515
EXPERIMENTAL_FEATURE(CDecl, false)
516516

517+
/// Allow use of `@extensible` on public enums
518+
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)
519+
517520
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
518521
#undef EXPERIMENTAL_FEATURE
519522
#undef UPCOMING_FEATURE

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
402402
DeclAttrKind::RestatedObjCConformance,
403403
DeclAttrKind::NonSendable,
404404
DeclAttrKind::AllowFeatureSuppression,
405-
DeclAttrKind::Extensible,
406405
};
407406

408407
return result;
@@ -3305,6 +3304,13 @@ suppressingFeatureAddressableTypes(PrintOptions &options,
33053304
action();
33063305
}
33073306

3307+
static void
3308+
suppressingFeatureExtensibleAttribute(PrintOptions &options,
3309+
llvm::function_ref<void()> action) {
3310+
ExcludeAttrRAII scope(options.ExcludeAttrList, DeclAttrKind::Extensible);
3311+
action();
3312+
}
3313+
33083314
/// Suppress the printing of a particular feature.
33093315
static void suppressingFeature(PrintOptions &options, Feature feature,
33103316
llvm::function_ref<void()> action) {

lib/AST/FeatureSet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ static bool usesFeatureAsyncExecutionBehaviorAttributes(Decl *decl) {
624624
return false;
625625
}
626626

627+
static bool usesFeatureExtensibleAttribute(Decl *decl) {
628+
return decl->getAttrs().hasAttribute<ExtensibleAttr>();
629+
}
630+
627631
// ----------------------------------------------------------------------------
628632
// MARK: - FeatureSet
629633
// ----------------------------------------------------------------------------

test/IDE/complete_decl_attribute.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ struct _S {
330330
// ON_MEMBER_LAST-DAG: Keyword/None: freestanding[#Declaration Attribute#]; name=freestanding
331331
// ON_MEMBER_LAST-DAG: Keyword/None: storageRestrictions[#Declaration Attribute#]; name=storageRestrictions
332332
// ON_MEMBER_LAST-DAG: Keyword/None: lifetime[#Declaration Attribute#]; name=lifetime
333-
// ON_MEMBER_LAST-DAG: Keyword/None: extensible[#Declaration Attribute#]; name=extensible
334333
// ON_MEMBER_LAST-DAG: Keyword/None: concurrent[#Declaration Attribute#]; name=concurrent
335334
// ON_MEMBER_LAST-NOT: Keyword
336335
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
@@ -404,7 +403,6 @@ func dummy2() {}
404403
// KEYWORD_LAST-DAG: Keyword/None: attached[#Declaration Attribute#]; name=attached
405404
// KEYWORD_LAST-DAG: Keyword/None: storageRestrictions[#Declaration Attribute#]; name=storageRestrictions
406405
// KEYWORD_LAST-DAG: Keyword/None: lifetime[#Declaration Attribute#]; name=lifetime
407-
// KEYWORD_LAST-DAG: Keyword/None: extensible[#Declaration Attribute#]; name=extensible
408406
// KEYWORD_LAST-DAG: Keyword/None: concurrent[#Declaration Attribute#]; name=concurrent
409407
// KEYWORD_LAST-NOT: Keyword
410408
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct

test/IDE/complete_decl_attribute_feature_requirement.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
// RUN: %batch-code-completion -filecheck-additional-suffix _DISABLED
99
// RUN: %batch-code-completion -filecheck-additional-suffix _ENABLED \
10-
// RUN: -enable-experimental-feature ABIAttribute
10+
// RUN: -enable-experimental-feature ABIAttribute \
11+
// RUN: -enable-experimental-feature ExtensibleAttribute
1112

1213
// NOTE: Please do not include the ", N items" after "Begin completions". The
1314
// item count creates needless merge conflicts given that an "End completions"
@@ -34,6 +35,8 @@
3435
// KEYWORD4: Begin completions
3536
// KEYWORD4_ENABLED-NOT: Keyword/None: abi[#{{.*}} Attribute#]; name=abi
3637
// KEYWORD4_DISABLED-NOT: Keyword/None: abi[#{{.*}} Attribute#]; name=abi
38+
// KEYWORD4_ENABLED-DAG: Keyword/None: extensible[#{{.*}} Attribute#]; name=extensible
39+
// KEYWORD4_DISABLED-NOT: Keyword/None: extensible[#{{.*}} Attribute#]; name=extensible
3740
// KEYWORD4: End completions
3841

3942
@#^KEYWORD5^# struct S{}

test/ModuleInterface/attrs.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,6 @@ nonisolated(nonsending)
107107
public func testExecutionCaller() async {}
108108
// CHECK: nonisolated(nonsending) public func testExecutionCaller() async
109109

110-
// CHECK-NOT: @extensible
111-
// CHECK: public enum TestExtensible
112-
@extensible
113-
public enum TestExtensible {
114-
case a
115-
case b
116-
}
117-
118110
public struct TestPlacementOfAttrsAndSpecifiers {
119111
// CHECK: public func test1<T>(_: sending @autoclosure () -> T)
120112
public func test1<T>(_: sending @autoclosure () -> T) {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -enable-experimental-feature ExtensibleAttribute -module-name Library
3+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -enable-experimental-feature ExtensibleAttribute -module-name Library
4+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library
5+
// RUN: %FileCheck %s < %t/Library.swiftinterface
6+
7+
// REQUIRES: swift_feature_ExtensibleAttribute
8+
9+
// CHECK: #if compiler(>=5.3) && $ExtensibleAttribute
10+
// CHECK-NEXT: @extensible public enum E {
11+
// CHECK-NEXT: }
12+
// CHECK-NEXT: #else
13+
// CHECK-NEXT: public enum E {
14+
// CHECK-NEXT: }
15+
// CHECK-NEXT: #endif
16+
@extensible
17+
public enum E {
18+
}

test/ModuleInterface/extensible_enums.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/// Build the library
66
// RUN: %target-swift-frontend -emit-module %t/src/Lib.swift \
77
// RUN: -module-name Lib \
8-
// RUN: -emit-module-path %t/Lib.swiftmodule
8+
// RUN: -emit-module-path %t/Lib.swiftmodule \
9+
// RUN: -enable-experimental-feature ExtensibleAttribute
910

1011
// Check that the errors are produced when using enums from module with `ExtensibleEnums` feature enabled.
1112
// RUN: %target-swift-frontend -typecheck %t/src/TestChecking.swift \
@@ -18,14 +19,18 @@
1819
// RUN: %target-swift-frontend -emit-module %t/src/Lib.swift \
1920
// RUN: -module-name Lib \
2021
// RUN: -package-name Test \
21-
// RUN: -emit-module-path %t/Lib.swiftmodule
22+
// RUN: -emit-module-path %t/Lib.swiftmodule \
23+
// RUN: -enable-experimental-feature ExtensibleAttribute
24+
2225

2326
// Different module but the same package
2427
// RUN: %target-swift-frontend -typecheck %t/src/TestSamePackage.swift \
2528
// RUN: -swift-version 5 -module-name Client -I %t \
2629
// RUN: -package-name Test \
2730
// RUN: -verify
2831

32+
// REQUIRES: swift_feature_ExtensibleAttribute
33+
2934
//--- Lib.swift
3035

3136
@extensible

test/attr/attr_extensible.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ExtensibleAttribute
2+
3+
// REQUIRES: swift_feature_ExtensibleAttribute
24

35
@extensible
46
public enum E1 { // Ok

test/attr/feature_requirement.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %target-typecheck-verify-swift -parse-as-library -disable-experimental-parser-round-trip -verify-additional-prefix disabled-
2-
// RUN: %target-typecheck-verify-swift -parse-as-library -verify-additional-prefix enabled- -enable-experimental-feature ABIAttribute
2+
// RUN: %target-typecheck-verify-swift -parse-as-library -verify-additional-prefix enabled- -enable-experimental-feature ABIAttribute -enable-experimental-feature ExtensibleAttribute
33

44
// REQUIRES: asserts
55

@@ -14,3 +14,13 @@ func fn() {} // expected-disabled-error@-1 {{'abi' attribute is only valid when
1414
#else
1515
#error("doesn't have @abi") // expected-disabled-error {{doesn't have @abi}}
1616
#endif
17+
18+
@extensible
19+
public enum E {} // expected-disabled-error@-1 {{'extensible' attribute is only valid when experimental feature ExtensibleAttribute is enabled}}
20+
21+
#if hasAttribute(extensible)
22+
#error("does have @extensible") // expected-enabled-error {{does have @extensible}}
23+
#else
24+
#error("doesn't have @extensible") // expected-disabled-error {{doesn't have @extensible}}
25+
#endif
26+

0 commit comments

Comments
 (0)