Skip to content

Commit 355adfe

Browse files
authored
Merge pull request #72571 from tshortli/has-retroactive-attribute
AST: `#if hasAttribute(retroactive)` should evaluate true
2 parents c0d423a + ed77b8f commit 355adfe

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

include/swift/AST/Attr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,6 +3022,9 @@ class alignas(1 << AttrAlignInBits) TypeAttribute
30223022
/// corresponds to it.
30233023
static std::optional<TypeAttrKind> getAttrKindFromString(StringRef Str);
30243024

3025+
/// Returns true if type attributes of the given kind only appear in SIL.
3026+
static bool isSilOnly(TypeAttrKind TK);
3027+
30253028
/// Return the name (like "autoclosure") for an attribute ID.
30263029
static const char *getAttrName(TypeAttrKind kind);
30273030

lib/AST/Attr.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ TypeAttribute::getAttrKindFromString(StringRef Str) {
124124
.Default(std::nullopt);
125125
}
126126

127+
bool TypeAttribute::isSilOnly(TypeAttrKind TK) {
128+
switch (TK) {
129+
#define SIL_TYPE_ATTR(X, C) case TypeAttrKind::C:
130+
#include "swift/AST/TypeAttr.def"
131+
return true;
132+
default:
133+
return false;
134+
}
135+
}
136+
127137
/// Return the name (like "autoclosure") for an attribute ID.
128138
const char *TypeAttribute::getAttrName(TypeAttrKind kind) {
129139
switch (kind) {
@@ -2947,8 +2957,8 @@ void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
29472957
attr->print(out);
29482958
}
29492959

2950-
bool swift::hasAttribute(
2951-
const LangOptions &langOpts, llvm::StringRef attributeName) {
2960+
static bool hasDeclAttribute(const LangOptions &langOpts,
2961+
llvm::StringRef attributeName) {
29522962
std::optional<DeclAttrKind> kind =
29532963
DeclAttribute::getAttrKindFromString(attributeName);
29542964
if (!kind)
@@ -2967,3 +2977,27 @@ bool swift::hasAttribute(
29672977

29682978
return true;
29692979
}
2980+
2981+
static bool hasTypeAttribute(const LangOptions &langOpts,
2982+
llvm::StringRef attributeName) {
2983+
std::optional<TypeAttrKind> kind =
2984+
TypeAttribute::getAttrKindFromString(attributeName);
2985+
if (!kind)
2986+
return false;
2987+
2988+
if (TypeAttribute::isSilOnly(*kind))
2989+
return false;
2990+
2991+
return true;
2992+
}
2993+
2994+
bool swift::hasAttribute(const LangOptions &langOpts,
2995+
llvm::StringRef attributeName) {
2996+
if (hasDeclAttribute(langOpts, attributeName))
2997+
return true;
2998+
2999+
if (hasTypeAttribute(langOpts, attributeName))
3000+
return true;
3001+
3002+
return false;
3003+
}

test/attr/has_attribute.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,23 @@ UserInaccessibleAreNotAttributes
2323
#if hasAttribute(17)
2424
// expected-error@-1:18 {{unexpected platform condition argument: expected attribute name}}
2525
#endif
26+
27+
#if !hasAttribute(escaping)
28+
#error("type attributes are valid")
29+
#endif
30+
31+
#if !hasAttribute(convention)
32+
#error("type attributes are valid")
33+
#endif
34+
35+
#if !hasAttribute(retroactive)
36+
#error("type attributes are valid")
37+
#endif
38+
39+
#if hasAttribute(in_guaranteed)
40+
#error("SIL type attributes are invalid")
41+
#endif
42+
43+
#if hasAttribute(opened)
44+
#error("SIL type attributes are invalid")
45+
#endif

0 commit comments

Comments
 (0)