Skip to content

Commit 69649a4

Browse files
committed
Generalize CustomAttrNominalRequest's name and signature to account for macros
1 parent 9e76023 commit 69649a4

11 files changed

+62
-31
lines changed

include/swift/AST/Attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ class CustomAttr final : public DeclAttribute {
17121712
}
17131713

17141714
private:
1715-
friend class CustomAttrNominalRequest;
1715+
friend class CustomAttrDeclRequest;
17161716
void resetTypeInformation(TypeExpr *repr);
17171717

17181718
private:

include/swift/AST/NameLookupRequests.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,14 @@ class TypeDeclsFromWhereClauseRequest :
323323
ExtensionDecl *ext) const;
324324
};
325325

326-
/// Request the nominal type declaration to which the given custom attribute
327-
/// refers.
328-
class CustomAttrNominalRequest :
329-
public SimpleRequest<CustomAttrNominalRequest,
330-
NominalTypeDecl *(CustomAttr *, DeclContext *),
326+
using MacroOrNominalTypeDecl =
327+
llvm::PointerUnion<MacroDecl *, NominalTypeDecl *>;
328+
329+
/// Request the macro or nominal type declaration to which the given custom
330+
/// attribute refers.
331+
class CustomAttrDeclRequest :
332+
public SimpleRequest<CustomAttrDeclRequest,
333+
MacroOrNominalTypeDecl(CustomAttr *, DeclContext *),
331334
RequestFlags::Cached> {
332335
public:
333336
using SimpleRequest::SimpleRequest;
@@ -336,7 +339,7 @@ class CustomAttrNominalRequest :
336339
friend SimpleRequest;
337340

338341
// Evaluation.
339-
NominalTypeDecl *
342+
MacroOrNominalTypeDecl
340343
evaluate(Evaluator &evaluator, CustomAttr *attr, DeclContext *dc) const;
341344

342345
public:

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
SWIFT_REQUEST(NameLookup, AnyObjectLookupRequest,
1919
QualifiedLookupResult(const DeclContext *, DeclName, NLOptions),
2020
Uncached, NoLocationInfo)
21-
SWIFT_REQUEST(NameLookup, CustomAttrNominalRequest,
22-
NominalTypeDecl *(CustomAttr *, DeclContext *), Cached,
21+
SWIFT_REQUEST(NameLookup, CustomAttrDeclRequest,
22+
MacroOrNominalTypeDecl(CustomAttr *, DeclContext *), Cached,
2323
NoLocationInfo)
2424
SWIFT_REQUEST(NameLookup, DirectLookupRequest,
2525
TinyPtrVector<ValueDecl *>(DirectLookupDescriptor), Uncached,

lib/AST/Decl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6807,8 +6807,11 @@ VarDecl::getAttachedPropertyWrapperTypeInfo(unsigned i) const {
68076807
auto attr = attrs[i];
68086808
auto dc = getDeclContext();
68096809
ASTContext &ctx = getASTContext();
6810-
nominal = evaluateOrDefault(
6811-
ctx.evaluator, CustomAttrNominalRequest{attr, dc}, nullptr);
6810+
if (auto found = evaluateOrDefault(
6811+
ctx.evaluator, CustomAttrDeclRequest{attr, dc}, nullptr))
6812+
nominal = found.dyn_cast<NominalTypeDecl *>();
6813+
else
6814+
nominal = nullptr;
68126815
}
68136816

68146817
if (!nominal)
@@ -9748,7 +9751,8 @@ NominalTypeDecl *
97489751
ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
97499752
auto &ctx = getASTContext();
97509753
auto *nominal = evaluateOrDefault(
9751-
ctx.evaluator, CustomAttrNominalRequest{attr, getDeclContext()}, nullptr);
9754+
ctx.evaluator, CustomAttrDeclRequest{attr, getDeclContext()}, nullptr)
9755+
.get<NominalTypeDecl *>();
97529756
assert(nominal->getAttrs().hasAttribute<RuntimeMetadataAttr>());
97539757
return nominal;
97549758
}

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,8 +3040,8 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
30403040
parsedGenericParams->getRAngleLoc());
30413041
}
30423042

3043-
NominalTypeDecl *
3044-
CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
3043+
MacroOrNominalTypeDecl
3044+
CustomAttrDeclRequest::evaluate(Evaluator &evaluator,
30453045
CustomAttr *attr, DeclContext *dc) const {
30463046
// Find the types referenced by the custom attribute.
30473047
auto &ctx = dc->getASTContext();

lib/Sema/TypeCheckAttr.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,11 +3553,16 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
35533553
auto dc = D->getDeclContext();
35543554

35553555
// Figure out which nominal declaration this custom attribute refers to.
3556-
auto nominal = evaluateOrDefault(
3557-
Ctx.evaluator, CustomAttrNominalRequest{attr, dc}, nullptr);
3556+
auto found = evaluateOrDefault(
3557+
Ctx.evaluator, CustomAttrDeclRequest{attr, dc}, nullptr);
3558+
3559+
// FIXME: deal with macros.
3560+
NominalTypeDecl *nominal = nullptr;
3561+
if (found)
3562+
nominal = found.dyn_cast<NominalTypeDecl *>();
35583563

35593564
// Diagnose errors.
3560-
if (!nominal) {
3565+
if (!found) {
35613566
auto typeRepr = attr->getTypeRepr();
35623567

35633568
auto type = TypeResolution::forInterface(dc, TypeResolverContext::CustomAttr,
@@ -7377,12 +7382,15 @@ static void forEachCustomAttribute(
73777382
for (auto *attr : decl->getAttrs().getAttributes<CustomAttr>()) {
73787383
auto *mutableAttr = const_cast<CustomAttr *>(attr);
73797384

7380-
auto *nominal = evaluateOrDefault(
7385+
auto found = evaluateOrDefault(
73817386
ctx.evaluator,
7382-
CustomAttrNominalRequest{mutableAttr, decl->getDeclContext()}, nullptr);
7387+
CustomAttrDeclRequest{mutableAttr, decl->getDeclContext()}, nullptr);
7388+
if (!found)
7389+
continue;
73837390

7391+
auto nominal = found.dyn_cast<NominalTypeDecl *>();
73847392
if (!nominal)
7385-
continue;
7393+
continue; // FIXME: add another entry point for macros we've found
73867394

73877395
if (nominal->getAttrs().hasAttribute<ATTR>())
73887396
fn(mutableAttr, nominal);

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,15 @@ swift::checkGlobalActorAttributes(
257257
NominalTypeDecl *globalActorNominal = nullptr;
258258
for (auto attr : attrs) {
259259
// Figure out which nominal declaration this custom attribute refers to.
260-
auto nominal = evaluateOrDefault(ctx.evaluator,
261-
CustomAttrNominalRequest{attr, dc},
262-
nullptr);
260+
auto found = evaluateOrDefault(ctx.evaluator,
261+
CustomAttrDeclRequest{attr, dc},
262+
nullptr);
263263

264264
// Ignore unresolvable custom attributes.
265+
if (!found)
266+
continue;
267+
268+
auto nominal = found.dyn_cast<NominalTypeDecl *>();
265269
if (!nominal)
266270
continue;
267271

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,12 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
439439
for (auto attr : var->getAttrs().getAttributes<CustomAttr>()) {
440440
auto mutableAttr = const_cast<CustomAttr *>(attr);
441441
// Figure out which nominal declaration this custom attribute refers to.
442-
auto nominal = evaluateOrDefault(
443-
ctx.evaluator, CustomAttrNominalRequest{mutableAttr, dc}, nullptr);
442+
auto found = evaluateOrDefault(
443+
ctx.evaluator, CustomAttrDeclRequest{mutableAttr, dc}, nullptr);
444+
445+
NominalTypeDecl *nominal = nullptr;
446+
if (found)
447+
nominal = found.dyn_cast<NominalTypeDecl *>();
444448

445449
// If we didn't find a nominal type with a @propertyWrapper attribute,
446450
// skip this custom attribute.

lib/Sema/TypeCheckRequestFunctions.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,15 @@ AttachedResultBuilderRequest::evaluate(Evaluator &evaluator,
174174
for (auto attr : decl->getAttrs().getAttributes<CustomAttr>()) {
175175
auto mutableAttr = const_cast<CustomAttr *>(attr);
176176
// Figure out which nominal declaration this custom attribute refers to.
177-
auto nominal = evaluateOrDefault(ctx.evaluator,
178-
CustomAttrNominalRequest{mutableAttr, dc},
179-
nullptr);
177+
auto found = evaluateOrDefault(ctx.evaluator,
178+
CustomAttrDeclRequest{mutableAttr, dc},
179+
nullptr);
180180

181181
// Ignore unresolvable custom attributes.
182+
if (!found)
183+
continue;
184+
185+
auto nominal = found.dyn_cast<NominalTypeDecl *>();
182186
if (!nominal)
183187
continue;
184188

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5022,7 +5022,7 @@ Type CustomAttrTypeRequest::evaluate(Evaluator &eval, CustomAttr *attr,
50225022

50235023
// We always require the type to resolve to a nominal type. If the type was
50245024
// not a nominal type, we should have already diagnosed an error via
5025-
// CustomAttrNominalRequest.
5025+
// CustomAttrDeclRequest.
50265026
auto checkType = [](Type type) -> bool {
50275027
while (auto *genericDecl = type->getAnyGeneric()) {
50285028
if (isa<NominalTypeDecl>(genericDecl))

0 commit comments

Comments
 (0)