@@ -22,11 +22,32 @@ using namespace swift;
2222using namespace ide ;
2323using TypeRelation = CodeCompletionResultTypeRelation;
2424
25+ // MARK: - Utilities
26+
27+ // / Returns the kind of attributes \c Ty can be used as.
28+ static OptionSet<CustomAttributeKind> getCustomAttributeKinds (Type Ty) {
29+ OptionSet<CustomAttributeKind> Result;
30+ if (auto NominalTy = Ty->getAs <NominalType>()) {
31+ auto NominalDecl = NominalTy->getDecl ();
32+ if (NominalDecl->getAttrs ().hasAttribute <PropertyWrapperAttr>()) {
33+ Result |= CustomAttributeKind::PropertyWrapper;
34+ }
35+ if (NominalDecl->getAttrs ().hasAttribute <ResultBuilderAttr>()) {
36+ Result |= CustomAttributeKind::ResultBuilder;
37+ }
38+ if (NominalDecl->isGlobalActor ()) {
39+ Result |= CustomAttributeKind::GlobalActor;
40+ }
41+ }
42+ return Result;
43+ }
44+
2545// MARK: - USRBasedTypeContext
2646
2747USRBasedTypeContext::USRBasedTypeContext (const ExpectedTypeContext *TypeContext,
2848 USRBasedTypeArena &Arena)
29- : Arena(Arena) {
49+ : Arena(Arena), ExpectedCustomAttributeKinds(
50+ TypeContext->getExpectedCustomAttributeKinds ()) {
3051
3152 for (auto possibleTy : TypeContext->getPossibleTypes ()) {
3253 ContextualTypes.emplace_back (USRBasedType::fromType (possibleTy, Arena),
@@ -65,6 +86,11 @@ USRBasedTypeContext::USRBasedTypeContext(const ExpectedTypeContext *TypeContext,
6586
6687TypeRelation
6788USRBasedTypeContext::typeRelation (const USRBasedType *ResultType) const {
89+ if (ExpectedCustomAttributeKinds) {
90+ return ResultType->getCustomAttributeKinds () & ExpectedCustomAttributeKinds
91+ ? TypeRelation::Convertible
92+ : TypeRelation::Unrelated;
93+ }
6894 const USRBasedType *VoidType = Arena.getVoidType ();
6995 if (ResultType == VoidType) {
7096 // Void is not convertible to anything and we don't report Void <-> Void
@@ -84,7 +110,7 @@ USRBasedTypeContext::typeRelation(const USRBasedType *ResultType) const {
84110
85111USRBasedTypeArena::USRBasedTypeArena () {
86112 // '$sytD' is the USR of the Void type.
87- VoidType = USRBasedType::fromUSR (" $sytD" , {}, *this );
113+ VoidType = USRBasedType::fromUSR (" $sytD" , {}, {}, *this );
88114}
89115
90116const USRBasedType *USRBasedTypeArena::getVoidType () const { return VoidType; }
@@ -124,11 +150,12 @@ TypeRelation USRBasedType::typeRelationImpl(
124150}
125151
126152const USRBasedType *USRBasedType::null (USRBasedTypeArena &Arena) {
127- return USRBasedType::fromUSR (/* USR=*/ " " , /* Supertypes=*/ {}, Arena);
153+ return USRBasedType::fromUSR (/* USR=*/ " " , /* Supertypes=*/ {}, {}, Arena);
128154}
129155
130156const USRBasedType *
131157USRBasedType::fromUSR (StringRef USR, ArrayRef<const USRBasedType *> Supertypes,
158+ OptionSet<CustomAttributeKind> CustomAttributeKinds,
132159 USRBasedTypeArena &Arena) {
133160 auto ExistingTypeIt = Arena.CanonicalTypes .find (USR);
134161 if (ExistingTypeIt != Arena.CanonicalTypes .end ()) {
@@ -141,7 +168,7 @@ USRBasedType::fromUSR(StringRef USR, ArrayRef<const USRBasedType *> Supertypes,
141168 Supertypes = Supertypes.copy (Arena.Allocator );
142169
143170 const USRBasedType *Result =
144- new (Arena.Allocator ) USRBasedType (USR, Supertypes);
171+ new (Arena.Allocator ) USRBasedType (USR, Supertypes, CustomAttributeKinds );
145172 Arena.CanonicalTypes [USR] = Result;
146173 return Result;
147174}
@@ -222,7 +249,8 @@ const USRBasedType *USRBasedType::fromType(Type Ty, USRBasedTypeArena &Arena) {
222249 return ImpliedSupertypes.contains (Ty);
223250 });
224251
225- return USRBasedType::fromUSR (USR, Supertypes, Arena);
252+ return USRBasedType::fromUSR (USR, Supertypes, ::getCustomAttributeKinds (Ty),
253+ Arena);
226254}
227255
228256TypeRelation USRBasedType::typeRelation (const USRBasedType *ResultType,
@@ -284,6 +312,12 @@ calculateMaxTypeRelation(Type Ty, const ExpectedTypeContext &typeContext,
284312 const DeclContext &DC) {
285313 if (Ty->isVoid () && typeContext.requiresNonVoid ())
286314 return TypeRelation::Invalid;
315+ if (typeContext.getExpectedCustomAttributeKinds ()) {
316+ return (getCustomAttributeKinds (Ty) &
317+ typeContext.getExpectedCustomAttributeKinds ())
318+ ? TypeRelation::Convertible
319+ : TypeRelation::Unrelated;
320+ }
287321 if (typeContext.empty ())
288322 return TypeRelation::Unknown;
289323
0 commit comments