Skip to content

AST: #if hasAttribute(retroactive) should evaluate true #72571

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
Mar 26, 2024
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
3 changes: 3 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,9 @@ class alignas(1 << AttrAlignInBits) TypeAttribute
/// corresponds to it.
static std::optional<TypeAttrKind> getAttrKindFromString(StringRef Str);

/// Returns true if type attributes of the given kind only appear in SIL.
static bool isSilOnly(TypeAttrKind TK);

/// Return the name (like "autoclosure") for an attribute ID.
static const char *getAttrName(TypeAttrKind kind);

Expand Down
38 changes: 36 additions & 2 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ TypeAttribute::getAttrKindFromString(StringRef Str) {
.Default(std::nullopt);
}

bool TypeAttribute::isSilOnly(TypeAttrKind TK) {
switch (TK) {
#define SIL_TYPE_ATTR(X, C) case TypeAttrKind::C:
#include "swift/AST/TypeAttr.def"
return true;
default:
return false;
}
}

/// Return the name (like "autoclosure") for an attribute ID.
const char *TypeAttribute::getAttrName(TypeAttrKind kind) {
switch (kind) {
Expand Down Expand Up @@ -2947,8 +2957,8 @@ void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
attr->print(out);
}

bool swift::hasAttribute(
const LangOptions &langOpts, llvm::StringRef attributeName) {
static bool hasDeclAttribute(const LangOptions &langOpts,
llvm::StringRef attributeName) {
std::optional<DeclAttrKind> kind =
DeclAttribute::getAttrKindFromString(attributeName);
if (!kind)
Expand All @@ -2967,3 +2977,27 @@ bool swift::hasAttribute(

return true;
}

static bool hasTypeAttribute(const LangOptions &langOpts,
llvm::StringRef attributeName) {
std::optional<TypeAttrKind> kind =
TypeAttribute::getAttrKindFromString(attributeName);
if (!kind)
return false;

if (TypeAttribute::isSilOnly(*kind))
return false;

return true;
}

bool swift::hasAttribute(const LangOptions &langOpts,
llvm::StringRef attributeName) {
if (hasDeclAttribute(langOpts, attributeName))
return true;

if (hasTypeAttribute(langOpts, attributeName))
return true;

return false;
}
20 changes: 20 additions & 0 deletions test/attr/has_attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ UserInaccessibleAreNotAttributes
#if hasAttribute(17)
// expected-error@-1:18 {{unexpected platform condition argument: expected attribute name}}
#endif

#if !hasAttribute(escaping)
#error("type attributes are valid")
#endif

#if !hasAttribute(convention)
#error("type attributes are valid")
#endif

#if !hasAttribute(retroactive)
#error("type attributes are valid")
#endif

#if hasAttribute(in_guaranteed)
#error("SIL type attributes are invalid")
#endif

#if hasAttribute(opened)
#error("SIL type attributes are invalid")
#endif