Skip to content

[Frontend] Add ExtensibleAttribute to guard use of @extensible at… #81073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/DeclAttr.def
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ SIMPLE_DECL_ATTR(extensible, Extensible,
OnEnum,
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove | ForbiddenInABIAttr,
169)
DECL_ATTR_FEATURE_REQUIREMENT(Extensible, ExtensibleAttribute)

SIMPLE_DECL_ATTR(concurrent, Concurrent,
OnFunc | OnConstructor | OnSubscript | OnVar,
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ EXPERIMENTAL_FEATURE(AllowRuntimeSymbolDeclarations, true)
/// Allow use of `@cdecl`
EXPERIMENTAL_FEATURE(CDecl, false)

/// Allow use of `@extensible` on public enums
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)

#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
Expand Down
8 changes: 7 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
DeclAttrKind::RestatedObjCConformance,
DeclAttrKind::NonSendable,
DeclAttrKind::AllowFeatureSuppression,
DeclAttrKind::Extensible,
};

return result;
Expand Down Expand Up @@ -3302,6 +3301,13 @@ suppressingFeatureAddressableTypes(PrintOptions &options,
action();
}

static void
suppressingFeatureExtensibleAttribute(PrintOptions &options,
llvm::function_ref<void()> action) {
ExcludeAttrRAII scope(options.ExcludeAttrList, DeclAttrKind::Extensible);
action();
}

/// Suppress the printing of a particular feature.
static void suppressingFeature(PrintOptions &options, Feature feature,
llvm::function_ref<void()> action) {
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ static bool usesFeatureAsyncExecutionBehaviorAttributes(Decl *decl) {
return false;
}

static bool usesFeatureExtensibleAttribute(Decl *decl) {
return decl->getAttrs().hasAttribute<ExtensibleAttr>();
}

// ----------------------------------------------------------------------------
// MARK: - FeatureSet
// ----------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions test/IDE/complete_decl_attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ struct _S {
// ON_MEMBER_LAST-DAG: Keyword/None: freestanding[#Declaration Attribute#]; name=freestanding
// ON_MEMBER_LAST-DAG: Keyword/None: storageRestrictions[#Declaration Attribute#]; name=storageRestrictions
// ON_MEMBER_LAST-DAG: Keyword/None: lifetime[#Declaration Attribute#]; name=lifetime
// ON_MEMBER_LAST-DAG: Keyword/None: extensible[#Declaration Attribute#]; name=extensible
// ON_MEMBER_LAST-DAG: Keyword/None: concurrent[#Declaration Attribute#]; name=concurrent
// ON_MEMBER_LAST-NOT: Keyword
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
Expand Down Expand Up @@ -404,7 +403,6 @@ func dummy2() {}
// KEYWORD_LAST-DAG: Keyword/None: attached[#Declaration Attribute#]; name=attached
// KEYWORD_LAST-DAG: Keyword/None: storageRestrictions[#Declaration Attribute#]; name=storageRestrictions
// KEYWORD_LAST-DAG: Keyword/None: lifetime[#Declaration Attribute#]; name=lifetime
// KEYWORD_LAST-DAG: Keyword/None: extensible[#Declaration Attribute#]; name=extensible
// KEYWORD_LAST-DAG: Keyword/None: concurrent[#Declaration Attribute#]; name=concurrent
// KEYWORD_LAST-NOT: Keyword
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

// RUN: %batch-code-completion -filecheck-additional-suffix _DISABLED
// RUN: %batch-code-completion -filecheck-additional-suffix _ENABLED \
// RUN: -enable-experimental-feature ABIAttribute
// RUN: -enable-experimental-feature ABIAttribute \
// RUN: -enable-experimental-feature ExtensibleAttribute

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

@#^KEYWORD5^# struct S{}
Expand Down
8 changes: 0 additions & 8 deletions test/ModuleInterface/attrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ nonisolated(nonsending)
public func testExecutionCaller() async {}
// CHECK: nonisolated(nonsending) public func testExecutionCaller() async

// CHECK-NOT: @extensible
// CHECK: public enum TestExtensible
@extensible
public enum TestExtensible {
case a
case b
}

public struct TestPlacementOfAttrsAndSpecifiers {
// CHECK: public func test1<T>(_: sending @autoclosure () -> T)
public func test1<T>(_: sending @autoclosure () -> T) {}
Expand Down
18 changes: 18 additions & 0 deletions test/ModuleInterface/extensible_attr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -enable-experimental-feature ExtensibleAttribute -module-name Library
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -enable-experimental-feature ExtensibleAttribute -module-name Library
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library
// RUN: %FileCheck %s < %t/Library.swiftinterface

// REQUIRES: swift_feature_ExtensibleAttribute

// CHECK: #if compiler(>=5.3) && $ExtensibleAttribute
// CHECK-NEXT: @extensible public enum E {
// CHECK-NEXT: }
// CHECK-NEXT: #else
// CHECK-NEXT: public enum E {
// CHECK-NEXT: }
// CHECK-NEXT: #endif
@extensible
public enum E {
}
9 changes: 7 additions & 2 deletions test/ModuleInterface/extensible_enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/// Build the library
// RUN: %target-swift-frontend -emit-module %t/src/Lib.swift \
// RUN: -module-name Lib \
// RUN: -emit-module-path %t/Lib.swiftmodule
// RUN: -emit-module-path %t/Lib.swiftmodule \
// RUN: -enable-experimental-feature ExtensibleAttribute

// Check that the errors are produced when using enums from module with `ExtensibleEnums` feature enabled.
// RUN: %target-swift-frontend -typecheck %t/src/TestChecking.swift \
Expand All @@ -18,14 +19,18 @@
// RUN: %target-swift-frontend -emit-module %t/src/Lib.swift \
// RUN: -module-name Lib \
// RUN: -package-name Test \
// RUN: -emit-module-path %t/Lib.swiftmodule
// RUN: -emit-module-path %t/Lib.swiftmodule \
// RUN: -enable-experimental-feature ExtensibleAttribute


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

// REQUIRES: swift_feature_ExtensibleAttribute

//--- Lib.swift

@extensible
Expand Down
4 changes: 3 additions & 1 deletion test/attr/attr_extensible.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ExtensibleAttribute

// REQUIRES: swift_feature_ExtensibleAttribute

@extensible
public enum E1 { // Ok
Expand Down
12 changes: 11 additions & 1 deletion test/attr/feature_requirement.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-typecheck-verify-swift -parse-as-library -disable-experimental-parser-round-trip -verify-additional-prefix disabled-
// RUN: %target-typecheck-verify-swift -parse-as-library -verify-additional-prefix enabled- -enable-experimental-feature ABIAttribute
// RUN: %target-typecheck-verify-swift -parse-as-library -verify-additional-prefix enabled- -enable-experimental-feature ABIAttribute -enable-experimental-feature ExtensibleAttribute

// REQUIRES: asserts

Expand All @@ -14,3 +14,13 @@ func fn() {} // expected-disabled-error@-1 {{'abi' attribute is only valid when
#else
#error("doesn't have @abi") // expected-disabled-error {{doesn't have @abi}}
#endif

@extensible
public enum E {} // expected-disabled-error@-1 {{'extensible' attribute is only valid when experimental feature ExtensibleAttribute is enabled}}

#if hasAttribute(extensible)
#error("does have @extensible") // expected-enabled-error {{does have @extensible}}
#else
#error("doesn't have @extensible") // expected-disabled-error {{doesn't have @extensible}}
#endif