Skip to content

[AST/Sema] Add @preEnumExtensibility attribute to downgrade warning… #81176

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 1 commit into from
May 3, 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
8 changes: 7 additions & 1 deletion include/swift/AST/DeclAttr.def
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,13 @@ SIMPLE_DECL_ATTR(concurrent, Concurrent,
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove | UnconstrainedInABIAttr,
170)

LAST_DECL_ATTR(Concurrent)
SIMPLE_DECL_ATTR(preEnumExtensibility, PreEnumExtensibility,
OnEnum,
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIBreakingToRemove | UnconstrainedInABIAttr,
171)
DECL_ATTR_FEATURE_REQUIREMENT(PreEnumExtensibility, ExtensibleAttribute)

LAST_DECL_ATTR(PreEnumExtensibility)

#undef DECL_ATTR_ALIAS
#undef CONTEXTUAL_DECL_ATTR_ALIAS
Expand Down
5 changes: 4 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -8698,7 +8698,7 @@ GROUPED_WARNING(
(StringRef, DeclAttribute))

//===----------------------------------------------------------------------===//
// MARK: @extensible Attribute
// MARK: @extensible and @preEnumExtensibility Attributes
//===----------------------------------------------------------------------===//

ERROR(extensible_attr_on_frozen_type,none,
Expand All @@ -8710,6 +8710,9 @@ ERROR(extensible_attr_on_internal_type,none,
"%select{private|fileprivate|internal|%error|%error|%error}1",
(DeclName, AccessLevel))

ERROR(pre_enum_extensibility_without_extensible,none,
"%0 can only be used together with '@extensible' attribute", (DeclAttribute))

//===----------------------------------------------------------------------===//
// MARK: SwiftSettings
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5062,6 +5062,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
TRIVIAL_ATTR_PRINTER(WeakLinked, weak_linked)
TRIVIAL_ATTR_PRINTER(Extensible, extensible)
TRIVIAL_ATTR_PRINTER(Concurrent, concurrent)
TRIVIAL_ATTR_PRINTER(PreEnumExtensibility, preEnumExtensibility)

#undef TRIVIAL_ATTR_PRINTER

Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3301,7 +3301,8 @@ suppressingFeatureAddressableTypes(PrintOptions &options,
static void
suppressingFeatureExtensibleAttribute(PrintOptions &options,
llvm::function_ref<void()> action) {
ExcludeAttrRAII scope(options.ExcludeAttrList, DeclAttrKind::Extensible);
ExcludeAttrRAII scope1(options.ExcludeAttrList, DeclAttrKind::Extensible);
ExcludeAttrRAII scope2(options.ExcludeAttrList, DeclAttrKind::PreEnumExtensibility);
action();
}

Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/Sources/ASTGen/DeclAttrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ extension ASTGenVisitor {
.eagerMove,
.exported,
.extensible,
.preEnumExtensibility,
.discardableResult,
.disfavoredOverload,
.dynamicMemberLookup,
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
}
}

void visitPreEnumExtensibilityAttr(PreEnumExtensibilityAttr *attr) {
if (!D->getAttrs().hasAttribute<ExtensibleAttr>()) {
diagnoseAndRemoveAttr(
attr, diag::pre_enum_extensibility_without_extensible, attr);
return;
}
}

void visitConcurrentAttr(ConcurrentAttr *attr) {
checkExecutionBehaviorAttribute(attr);

Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,7 @@ namespace {
UNINTERESTING_ATTR(Optimize)
UNINTERESTING_ATTR(Exclusivity)
UNINTERESTING_ATTR(Extensible)
UNINTERESTING_ATTR(PreEnumExtensibility)
UNINTERESTING_ATTR(NoLocks)
UNINTERESTING_ATTR(NoAllocation)
UNINTERESTING_ATTR(NoRuntime)
Expand Down
22 changes: 17 additions & 5 deletions lib/Sema/TypeCheckSwitchStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,16 +1154,28 @@ namespace {
assert(defaultReason == RequiresDefault::No);
Type subjectType = Switch->getSubjectExpr()->getType();
bool shouldIncludeFutureVersionComment = false;
bool shouldDowngradeToWarning = true;
if (auto *theEnum = subjectType->getEnumOrBoundGenericEnum()) {
auto *theEnum = subjectType->getEnumOrBoundGenericEnum();

if (theEnum) {
auto *enumModule = theEnum->getParentModule();
shouldIncludeFutureVersionComment =
enumModule->isSystemModule() ||
theEnum->getAttrs().hasAttribute<ExtensibleAttr>();
}
DE.diagnose(startLoc, diag::non_exhaustive_switch_unknown_only,
subjectType, shouldIncludeFutureVersionComment)
.warnUntilSwiftVersionIf(shouldDowngradeToWarning, 6);

auto diag =
DE.diagnose(startLoc, diag::non_exhaustive_switch_unknown_only,
subjectType, shouldIncludeFutureVersionComment);

// Presence of `@preEnumExtensibility` pushed the warning farther
// into the future.
if (theEnum &&
theEnum->getAttrs().hasAttribute<PreEnumExtensibilityAttr>()) {
diag.warnUntilFutureSwiftVersion();
} else {
diag.warnUntilSwiftVersion(6);
}

mainDiagType = std::nullopt;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 946; // end_cow_mutation_addr
const uint16_t SWIFTMODULE_VERSION_MINOR = 947; // @preEnumExtensibility attribute

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down
11 changes: 11 additions & 0 deletions test/ModuleInterface/extensible_attr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@
@extensible
public enum E {
}

// CHECK: #if compiler(>=5.3) && $ExtensibleAttribute
// CHECK-NEXT: @preEnumExtensibility @extensible public enum F {
// CHECK: #else
// CHECK-NEXT: public enum F {
// CHECK: #endif
@preEnumExtensibility
@extensible
public enum F {
case a
}
41 changes: 40 additions & 1 deletion test/ModuleInterface/extensible_enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
// RUN: -package-name Test \
// RUN: -verify

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

// REQUIRES: swift_feature_ExtensibleAttribute

//--- Lib.swift
Expand All @@ -38,6 +43,12 @@ public enum E {
case a
}

@preEnumExtensibility
@extensible
public enum PE {
case a
}

@frozen
public enum F {
case a
Expand All @@ -58,7 +69,7 @@ func test_same_module(e: E, f: F) {
//--- TestChecking.swift
import Lib

func test(e: E, f: F) {
func test(e: E, pe: PE, f: F) {
// `E` is marked as `@extensible` which means it gets new semantics

switch e {
Expand All @@ -72,6 +83,12 @@ func test(e: E, f: F) {
@unknown default: break
}

switch pe {
// expected-warning@-1 {{switch covers known cases, but 'PE' may have additional unknown values, possibly added in future versions; this will be an error in a future Swift language mode}}
// expected-note@-2 {{handle unknown values using "@unknown default"}}
case .a: break
}

// `F` is marked as `@frozen` which means regular rules apply.

switch f { // Ok (no errors because `F` is `@frozen`)
Expand Down Expand Up @@ -110,3 +127,25 @@ func test_no_default(e: E, f: F) {
@unknown default: break
}
}

//--- TestSwift6.swift
import Lib

func test(e: E, pe: PE, f: F) {
switch e {
// expected-error@-1 {{switch covers known cases, but 'E' may have additional unknown values, possibly added in future versions}}
// expected-note@-2 {{handle unknown values using "@unknown default"}}
case .a: break
}

switch e { // Ok (no warnings)
case .a: break
@unknown default: break
}

switch pe {
// expected-warning@-1 {{switch covers known cases, but 'PE' may have additional unknown values, possibly added in future versions; this will be an error in a future Swift language mode}}
// expected-note@-2 {{handle unknown values using "@unknown default"}}
case .a: break
}
}
9 changes: 9 additions & 0 deletions test/attr/attr_extensible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ struct Test {
set { }
}
}

@preEnumExtensibility
@extensible
public enum PE {
}

@preEnumExtensibility // expected-error {{@preEnumExtensibility can only be used together with '@extensible' attribute}}
public enum WrongPreE {
}