Skip to content

Commit 7e356f8

Browse files
committed
[Completion] Use TypeAttr.def for simple type attributes
Pick up all non-underscored simple type attributes for code completion, and add support for `@isolated(any)`. rdar://130741006 rdar://130288443
1 parent cc5e158 commit 7e356f8

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

include/swift/AST/Attr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,6 +3055,10 @@ class alignas(1 << AttrAlignInBits) TypeAttribute
30553055
/// Return the name (like "autoclosure") for an attribute ID.
30563056
static const char *getAttrName(TypeAttrKind kind);
30573057

3058+
/// Returns whether the given attribute is considered "user inaccessible",
3059+
/// which affects e.g whether it shows up in code completion.
3060+
static bool isUserInaccessible(TypeAttrKind DK);
3061+
30583062
static TypeAttribute *createSimple(const ASTContext &context,
30593063
TypeAttrKind kind,
30603064
SourceLoc atLoc,

lib/AST/Attr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ const char *TypeAttribute::getAttrName(TypeAttrKind kind) {
145145
llvm_unreachable("unknown type attribute kind");
146146
}
147147

148+
bool TypeAttribute::isUserInaccessible(TypeAttrKind DK) {
149+
// Currently we can base this off whether it is underscored or for SIL.
150+
// TODO: We could introduce a similar options scheme to DECL_ATTR if we ever
151+
// need a user-inaccessible non-underscored attribute.
152+
switch (DK) {
153+
// SIL attributes are always considered user-inaccessible.
154+
#define SIL_TYPE_ATTR(SPELLING, C) \
155+
case TypeAttrKind::C: \
156+
return true;
157+
// For non-SIL attributes, check whether the spelling is underscored.
158+
#define TYPE_ATTR(SPELLING, C) \
159+
case TypeAttrKind::C: \
160+
return StringRef(#SPELLING).starts_with("_");
161+
#include "swift/AST/TypeAttr.def"
162+
}
163+
llvm_unreachable("unhandled case in switch!");
164+
}
165+
148166
TypeAttribute *TypeAttribute::createSimple(const ASTContext &context,
149167
TypeAttrKind kind,
150168
SourceLoc atLoc,

lib/IDE/CompletionLookup.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,12 +3137,21 @@ void CompletionLookup::getTypeAttributeKeywordCompletions() {
31373137
CodeCompletionResultKind::Keyword, SemanticContextKind::None);
31383138
Builder.addAttributeKeyword(Name, "Type Attribute");
31393139
};
3140-
addTypeAttr("autoclosure");
3140+
3141+
// Add simple user-accessible attributes.
3142+
#define SIL_TYPE_ATTR(SPELLING, C)
3143+
#define SIMPLE_SIL_TYPE_ATTR(SPELLING, C)
3144+
#define SIMPLE_TYPE_ATTR(SPELLING, C) \
3145+
if (!TypeAttribute::isUserInaccessible(TypeAttrKind::C)) \
3146+
addTypeAttr(#SPELLING);
3147+
#include "swift/AST/TypeAttr.def"
3148+
3149+
// Add non-simple cases.
31413150
addTypeAttr("convention(swift)");
31423151
addTypeAttr("convention(block)");
31433152
addTypeAttr("convention(c)");
31443153
addTypeAttr("convention(thin)");
3145-
addTypeAttr("escaping");
3154+
addTypeAttr("isolated(any)");
31463155
}
31473156

31483157
void CompletionLookup::collectPrecedenceGroups() {

0 commit comments

Comments
 (0)