Skip to content

Commit dab6f3d

Browse files
authored
Merge pull request #82807 from xedin/se-0487-implementation-adjustments
[AST/Sema] SE-0487: Adjust implementation based on the LSG feedback
2 parents 1c546e9 + 0444d29 commit dab6f3d

27 files changed

+221
-125
lines changed

include/swift/AST/Attr.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ enum : unsigned {
136136
InheritActorContextModifier::Last_InheritActorContextKind))
137137
};
138138

139+
enum : unsigned {
140+
NumNonexhaustiveModeBits = countBitsUsed(
141+
static_cast<unsigned>(NonexhaustiveMode::Last_NonexhaustiveMode))
142+
};
143+
139144
enum : unsigned { NumDeclAttrKindBits = countBitsUsed(NumDeclAttrKinds - 1) };
140145

141146
enum : unsigned { NumTypeAttrKindBits = countBitsUsed(NumTypeAttrKinds - 1) };
@@ -277,6 +282,10 @@ class DeclAttribute : public AttributeBase {
277282
SWIFT_INLINE_BITFIELD(LifetimeAttr, DeclAttribute, 1,
278283
isUnderscored : 1
279284
);
285+
286+
SWIFT_INLINE_BITFIELD(NonexhaustiveAttr, DeclAttribute, NumNonexhaustiveModeBits,
287+
mode : NumNonexhaustiveModeBits
288+
);
280289
} Bits;
281290
// clang-format on
282291

@@ -3506,6 +3515,36 @@ class ABIAttr : public DeclAttribute {
35063515
}
35073516
};
35083517

3518+
/// Defines a @nonexhaustive attribute.
3519+
class NonexhaustiveAttr : public DeclAttribute {
3520+
public:
3521+
NonexhaustiveAttr(SourceLoc atLoc, SourceRange range, NonexhaustiveMode mode,
3522+
bool implicit = false)
3523+
: DeclAttribute(DeclAttrKind::Nonexhaustive, atLoc, range, implicit) {
3524+
Bits.NonexhaustiveAttr.mode = unsigned(mode);
3525+
}
3526+
3527+
NonexhaustiveAttr(NonexhaustiveMode mode)
3528+
: NonexhaustiveAttr(SourceLoc(), SourceRange(), mode) {}
3529+
3530+
NonexhaustiveMode getMode() const {
3531+
return NonexhaustiveMode(Bits.NonexhaustiveAttr.mode);
3532+
}
3533+
3534+
static bool classof(const DeclAttribute *DA) {
3535+
return DA->getKind() == DeclAttrKind::Nonexhaustive;
3536+
}
3537+
3538+
NonexhaustiveAttr *clone(ASTContext &ctx) const {
3539+
return new (ctx) NonexhaustiveAttr(AtLoc, Range, getMode(), isImplicit());
3540+
}
3541+
3542+
bool isEquivalent(const NonexhaustiveAttr *other, Decl *attachedTo) const {
3543+
return getMode() == other->getMode();
3544+
}
3545+
};
3546+
3547+
35093548
/// The kind of unary operator, if any.
35103549
enum class UnaryOperatorKind : uint8_t { None, Prefix, Postfix };
35113550

include/swift/AST/AttrKind.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ enum class ENUM_EXTENSIBILITY_ATTR(closed)
135135
Last_InheritActorContextKind = Always
136136
};
137137

138+
enum class ENUM_EXTENSIBILITY_ATTR(closed) NonexhaustiveMode : uint8_t {
139+
Error SWIFT_NAME("error") = 0,
140+
Warning SWIFT_NAME("warning") = 1,
141+
Last_NonexhaustiveMode = Warning
142+
};
143+
138144
enum class ENUM_EXTENSIBILITY_ATTR(closed) DeclAttrKind : unsigned {
139145
#define DECL_ATTR(_, CLASS, ...) CLASS,
140146
#define LAST_DECL_ATTR(CLASS) Last_DeclAttr = CLASS,

include/swift/AST/DeclAttr.def

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -877,22 +877,18 @@ SIMPLE_DECL_ATTR(constInitialized, ConstInitialized,
877877
168)
878878
DECL_ATTR_FEATURE_REQUIREMENT(ConstInitialized, CompileTimeValues)
879879

880-
SIMPLE_DECL_ATTR(extensible, Extensible,
880+
DECL_ATTR(nonexhaustive, Nonexhaustive,
881881
OnEnum,
882882
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove | ForbiddenInABIAttr,
883883
169)
884-
DECL_ATTR_FEATURE_REQUIREMENT(Extensible, ExtensibleAttribute)
884+
DECL_ATTR_FEATURE_REQUIREMENT(Nonexhaustive, NonexhaustiveAttribute)
885885

886886
SIMPLE_DECL_ATTR(concurrent, Concurrent,
887887
OnFunc | OnConstructor | OnSubscript | OnVar,
888888
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove | UnconstrainedInABIAttr,
889889
170)
890890

891-
SIMPLE_DECL_ATTR(preEnumExtensibility, PreEnumExtensibility,
892-
OnEnum,
893-
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIBreakingToRemove | UnconstrainedInABIAttr,
894-
171)
895-
DECL_ATTR_FEATURE_REQUIREMENT(PreEnumExtensibility, ExtensibleAttribute)
891+
// Unused '171': Used to be `@preEnumExtensibility`
896892

897893
DECL_ATTR(specialized, Specialized,
898894
OnConstructor | OnFunc | OnAccessor,

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8814,21 +8814,18 @@ GROUPED_WARNING(
88148814
(StringRef))
88158815

88168816
//===----------------------------------------------------------------------===//
8817-
// MARK: @extensible and @preEnumExtensibility Attributes
8817+
// MARK: @nonexhaustive and @preEnumExtensibility Attributes
88188818
//===----------------------------------------------------------------------===//
88198819

8820-
ERROR(extensible_attr_on_frozen_type,none,
8821-
"cannot use '@extensible' together with '@frozen'", ())
8820+
ERROR(nonexhaustive_attr_on_frozen_type,none,
8821+
"cannot use '@nonexhaustive' together with '@frozen'", ())
88228822

8823-
ERROR(extensible_attr_on_internal_type,none,
8824-
"'@extensible' attribute can only be applied to public or package "
8823+
ERROR(nonexhaustive_attr_on_internal_type,none,
8824+
"'@nonexhaustive' attribute can only be applied to public or package "
88258825
"declarations, but %0 is "
88268826
"%select{private|fileprivate|internal|%error|%error|%error}1",
88278827
(DeclName, AccessLevel))
88288828

8829-
ERROR(pre_enum_extensibility_without_extensible,none,
8830-
"%0 can only be used together with '@extensible' attribute", (DeclAttribute))
8831-
88328829
//===----------------------------------------------------------------------===//
88338830
// MARK: `using` declaration
88348831
//===----------------------------------------------------------------------===//

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ IDENTIFIER(value)
169169
IDENTIFIER_WITH_NAME(value_, "_value")
170170
IDENTIFIER(Void)
171171
IDENTIFIER(WinSDK)
172+
IDENTIFIER(warn)
172173
IDENTIFIER(with)
173174
IDENTIFIER(withArguments)
174175
IDENTIFIER(withKeywordArguments)

include/swift/Basic/Features.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ EXPERIMENTAL_FEATURE(AllowRuntimeSymbolDeclarations, true)
519519
/// Allow use of `@cdecl`
520520
EXPERIMENTAL_FEATURE(CDecl, false)
521521

522-
/// Allow use of `@extensible` on public enums
523-
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)
522+
/// Allow use of `@nonexhaustive` on public enums
523+
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(NonexhaustiveAttribute, false)
524524

525525
/// Allow use of `Module::name` syntax
526526
EXPERIMENTAL_FEATURE(ModuleSelector, false)

include/swift/Parse/IDEInspectionCallbacks.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ enum class ParameterizedDeclAttributeKind {
4040
FreestandingMacro,
4141
AttachedMacro,
4242
StorageRestrictions,
43-
InheritActorContext
43+
InheritActorContext,
44+
Nonexhaustive,
4445
};
4546

4647
/// A bit of a hack. When completing inside the '@storageRestrictions'

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5047,9 +5047,8 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
50475047
TRIVIAL_ATTR_PRINTER(Used, used)
50485048
TRIVIAL_ATTR_PRINTER(WarnUnqualifiedAccess, warn_unqualified_access)
50495049
TRIVIAL_ATTR_PRINTER(WeakLinked, weak_linked)
5050-
TRIVIAL_ATTR_PRINTER(Extensible, extensible)
5050+
TRIVIAL_ATTR_PRINTER(Nonexhaustive, nonexhaustive)
50515051
TRIVIAL_ATTR_PRINTER(Concurrent, concurrent)
5052-
TRIVIAL_ATTR_PRINTER(PreEnumExtensibility, preEnumExtensibility)
50535052

50545053
#undef TRIVIAL_ATTR_PRINTER
50555054

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,10 +3325,9 @@ suppressingFeatureAddressableTypes(PrintOptions &options,
33253325
}
33263326

33273327
static void
3328-
suppressingFeatureExtensibleAttribute(PrintOptions &options,
3329-
llvm::function_ref<void()> action) {
3330-
ExcludeAttrRAII scope1(options.ExcludeAttrList, DeclAttrKind::Extensible);
3331-
ExcludeAttrRAII scope2(options.ExcludeAttrList, DeclAttrKind::PreEnumExtensibility);
3328+
suppressingFeatureNonexhaustiveAttribute(PrintOptions &options,
3329+
llvm::function_ref<void()> action) {
3330+
ExcludeAttrRAII scope(options.ExcludeAttrList, DeclAttrKind::Nonexhaustive);
33323331
action();
33333332
}
33343333

lib/AST/Attr.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,19 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
17321732
break;
17331733
}
17341734

1735+
case DeclAttrKind::Nonexhaustive: {
1736+
auto *attr = cast<NonexhaustiveAttr>(this);
1737+
Printer << "@nonexhaustive";
1738+
switch (attr->getMode()) {
1739+
case NonexhaustiveMode::Error:
1740+
break;
1741+
case NonexhaustiveMode::Warning:
1742+
Printer << "(warn)";
1743+
break;
1744+
}
1745+
break;
1746+
}
1747+
17351748
#define SIMPLE_DECL_ATTR(X, CLASS, ...) case DeclAttrKind::CLASS:
17361749
#include "swift/AST/DeclAttr.def"
17371750
llvm_unreachable("handled above");
@@ -1967,6 +1980,8 @@ StringRef DeclAttribute::getAttrName() const {
19671980
}
19681981
case DeclAttrKind::Lifetime:
19691982
return cast<LifetimeAttr>(this)->isUnderscored() ? "_lifetime" : "lifetime";
1983+
case DeclAttrKind::Nonexhaustive:
1984+
return "nonexhaustive";
19701985
}
19711986
llvm_unreachable("bad DeclAttrKind");
19721987
}

0 commit comments

Comments
 (0)