Skip to content

Commit 3e6fdb8

Browse files
committed
[NameLookup] The custom attribute lookup request only returns nominal types.
Macro attribute lookup is now implemented in ResolveMacroRequest, which happens later because it needs overload resolution to resolve a custom attribute to a macro decl.
1 parent 8d0b187 commit 3e6fdb8

11 files changed

+31
-64
lines changed

include/swift/AST/Attr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ class CustomAttr final : public DeclAttribute {
17161716
}
17171717

17181718
private:
1719-
friend class CustomAttrDeclRequest;
1719+
friend class CustomAttrNominalRequest;
17201720
void resetTypeInformation(TypeExpr *repr);
17211721

17221722
private:

include/swift/AST/NameLookupRequests.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,11 @@ class TypeDeclsFromWhereClauseRequest :
323323
ExtensionDecl *ext) const;
324324
};
325325

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

341338
// Evaluation.
342-
MacroOrNominalTypeDecl
339+
NominalTypeDecl *
343340
evaluate(Evaluator &evaluator, CustomAttr *attr, DeclContext *dc) const;
344341

345342
public:

include/swift/AST/NameLookupTypeIDZone.def

+2-2
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, CustomAttrDeclRequest,
22-
MacroOrNominalTypeDecl(CustomAttr *, DeclContext *), Cached,
21+
SWIFT_REQUEST(NameLookup, CustomAttrNominalRequest,
22+
NominalTypeDecl *(CustomAttr *, DeclContext *), Cached,
2323
NoLocationInfo)
2424
SWIFT_REQUEST(NameLookup, DirectLookupRequest,
2525
TinyPtrVector<ValueDecl *>(DirectLookupDescriptor), Uncached,

lib/AST/Decl.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -6848,11 +6848,8 @@ VarDecl::getAttachedPropertyWrapperTypeInfo(unsigned i) const {
68486848
auto attr = attrs[i];
68496849
auto dc = getDeclContext();
68506850
ASTContext &ctx = getASTContext();
6851-
if (auto found = evaluateOrDefault(
6852-
ctx.evaluator, CustomAttrDeclRequest{attr, dc}, nullptr))
6853-
nominal = found.dyn_cast<NominalTypeDecl *>();
6854-
else
6855-
nominal = nullptr;
6851+
nominal = evaluateOrDefault(
6852+
ctx.evaluator, CustomAttrNominalRequest{attr, dc}, nullptr);
68566853
}
68576854

68586855
if (!nominal)
@@ -9878,8 +9875,7 @@ NominalTypeDecl *
98789875
ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
98799876
auto &ctx = getASTContext();
98809877
auto *nominal = evaluateOrDefault(
9881-
ctx.evaluator, CustomAttrDeclRequest{attr, getDeclContext()}, nullptr)
9882-
.get<NominalTypeDecl *>();
9878+
ctx.evaluator, CustomAttrNominalRequest{attr, getDeclContext()}, nullptr);
98839879
assert(nominal->getAttrs().hasAttribute<RuntimeMetadataAttr>());
98849880
return nominal;
98859881
}

lib/AST/NameLookup.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3065,9 +3065,9 @@ void swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc,
30653065
}
30663066
}
30673067

3068-
MacroOrNominalTypeDecl
3069-
CustomAttrDeclRequest::evaluate(Evaluator &evaluator,
3070-
CustomAttr *attr, DeclContext *dc) const {
3068+
NominalTypeDecl *
3069+
CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
3070+
CustomAttr *attr, DeclContext *dc) const {
30713071
// Look for names at module scope, so we don't trigger name lookup for
30723072
// nested scopes. At this point, we're looking to see whether there are
30733073
// any suitable macros.

lib/Sema/TypeCheckAttr.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -3564,15 +3564,10 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
35643564
auto dc = D->getDeclContext();
35653565

35663566
// Figure out which nominal declaration this custom attribute refers to.
3567-
auto found = evaluateOrDefault(
3568-
Ctx.evaluator, CustomAttrDeclRequest{attr, dc}, nullptr);
3567+
auto *nominal = evaluateOrDefault(
3568+
Ctx.evaluator, CustomAttrNominalRequest{attr, dc}, nullptr);
35693569

3570-
NominalTypeDecl *nominal = nullptr;
3571-
if (found) {
3572-
nominal = found.dyn_cast<NominalTypeDecl *>();
3573-
}
3574-
3575-
if (!found) {
3570+
if (!nominal) {
35763571
// Try resolving an attached macro attribute.
35773572
auto *macro = evaluateOrDefault(
35783573
Ctx.evaluator, ResolveAttachedMacroRequest{attr, dc}, nullptr);
@@ -7426,15 +7421,12 @@ static void forEachCustomAttribute(
74267421
for (auto *attr : decl->getAttrs().getAttributes<CustomAttr>()) {
74277422
auto *mutableAttr = const_cast<CustomAttr *>(attr);
74287423

7429-
auto found = evaluateOrDefault(
7424+
auto *nominal = evaluateOrDefault(
74307425
ctx.evaluator,
7431-
CustomAttrDeclRequest{mutableAttr, decl->getDeclContext()}, nullptr);
7432-
if (!found)
7433-
continue;
7426+
CustomAttrNominalRequest{mutableAttr, decl->getDeclContext()}, nullptr);
74347427

7435-
auto nominal = found.dyn_cast<NominalTypeDecl *>();
74367428
if (!nominal)
7437-
continue; // FIXME: add another entry point for macros we've found
7429+
continue;
74387430

74397431
if (nominal->getAttrs().hasAttribute<ATTR>())
74407432
fn(mutableAttr, nominal);

lib/Sema/TypeCheckConcurrency.cpp

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

264-
// Ignore unresolvable custom attributes.
265-
if (!found)
266-
continue;
267-
268-
auto nominal = found.dyn_cast<NominalTypeDecl *>();
269264
if (!nominal)
270265
continue;
271266

lib/Sema/TypeCheckPropertyWrapper.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,8 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
441441
for (auto attr : attachedAttrs.getAttributes<CustomAttr>()) {
442442
auto mutableAttr = const_cast<CustomAttr *>(attr);
443443
// Figure out which nominal declaration this custom attribute refers to.
444-
auto found = evaluateOrDefault(
445-
ctx.evaluator, CustomAttrDeclRequest{mutableAttr, dc}, nullptr);
446-
447-
NominalTypeDecl *nominal = nullptr;
448-
if (found)
449-
nominal = found.dyn_cast<NominalTypeDecl *>();
444+
auto *nominal = evaluateOrDefault(
445+
ctx.evaluator, CustomAttrNominalRequest{mutableAttr, dc}, nullptr);
450446

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

lib/Sema/TypeCheckRequestFunctions.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,10 @@ 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 found = evaluateOrDefault(ctx.evaluator,
178-
CustomAttrDeclRequest{mutableAttr, dc},
179-
nullptr);
177+
auto *nominal = evaluateOrDefault(ctx.evaluator,
178+
CustomAttrNominalRequest{mutableAttr, dc},
179+
nullptr);
180180

181-
// Ignore unresolvable custom attributes.
182-
if (!found)
183-
continue;
184-
185-
auto nominal = found.dyn_cast<NominalTypeDecl *>();
186181
if (!nominal)
187182
continue;
188183

lib/Sema/TypeCheckType.cpp

+1-1
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-
// CustomAttrDeclRequest.
5025+
// CustomAttrNominalRequest.
50265026
auto checkType = [](Type type) -> bool {
50275027
while (auto *genericDecl = type->getAnyGeneric()) {
50285028
if (isa<NominalTypeDecl>(genericDecl))

lib/Sema/TypeCheckTypeWrapper.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,11 @@ static void getTypeWrappers(NominalTypeDecl *decl,
123123
// Attributes applied directly to the type.
124124
for (auto *attr : decl->getAttrs().getAttributes<CustomAttr>()) {
125125
auto *mutableAttr = const_cast<CustomAttr *>(attr);
126-
auto found = evaluateOrDefault(
126+
auto *nominal = evaluateOrDefault(
127127
ctx.evaluator,
128-
CustomAttrDeclRequest{mutableAttr, decl->getDeclContext()},
128+
CustomAttrNominalRequest{mutableAttr, decl->getDeclContext()},
129129
nullptr);
130130

131-
if (!found)
132-
continue;
133-
134-
auto nominal = found.dyn_cast<NominalTypeDecl *>();
135131
if (!nominal)
136132
continue;
137133

0 commit comments

Comments
 (0)