Skip to content

Commit c17acf4

Browse files
authored
Merge pull request #72573 from tshortli/has-retroactive-attribute-6.0
[6.0] AST: `#if hasAttribute(retroactive)` should evaluate true
2 parents fb24ebe + 4f7f3db commit c17acf4

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
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: 33 additions & 1 deletion
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,7 +2957,7 @@ void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
29472957
attr->print(out);
29482958
}
29492959

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

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

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)