Skip to content

Commit 8221409

Browse files
committed
[Macros] Distinguish between attributes that do not reference macros and
invalid macro attributes in `findMacroForCustomAttr`.
1 parent 292e3a0 commit 8221409

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

include/swift/AST/MacroDeclaration.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ class CustomAttr;
7171

7272
/// Perform lookup to determine whether the given custom attribute refers to
7373
/// a macro declaration, and return that macro declaration.
74-
MacroDecl *findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc);
74+
///
75+
/// \Returns \c None if the custom attribute name does not match any macro
76+
/// declarations, \c nullptr if the macro reference has errors in the argument
77+
/// list, or a resolved macro declaration for a valid macro attribute.
78+
Optional<MacroDecl *>
79+
findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc);
7580

7681
class MacroIntroducedDeclName {
7782
public:

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3048,7 +3048,7 @@ CustomAttrDeclRequest::evaluate(Evaluator &evaluator,
30483048
// nested scopes. At this point, we're looking to see whether there are
30493049
// any suitable macros.
30503050
if (auto macro = findMacroForCustomAttr(attr, dc))
3051-
return macro;
3051+
return macro.getValue();
30523052

30533053
// Find the types referenced by the custom attribute.
30543054
auto &ctx = dc->getASTContext();

lib/Sema/TypeCheckMacros.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,11 +1083,11 @@ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
10831083
return addedAttributes;
10841084
}
10851085

1086-
MacroDecl *
1086+
Optional<MacroDecl *>
10871087
swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
10881088
auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr());
10891089
if (!identTypeRepr)
1090-
return nullptr;
1090+
return None;
10911091

10921092
// Look for macros at module scope. They can only occur at module scope, and
10931093
// we need to be sure not to trigger name lookup into type contexts along
@@ -1108,7 +1108,7 @@ swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
11081108
}
11091109

11101110
if (macros.empty())
1111-
return nullptr;
1111+
return None;
11121112

11131113
// Extract macro arguments from the attribute, or create an empty list.
11141114
ArgumentList *attrArgs;
@@ -1133,5 +1133,7 @@ swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
11331133
if (auto *macro = dyn_cast<MacroDecl>(fn->getDecl()))
11341134
return macro;
11351135

1136-
return dyn_cast<MacroDecl>(macros.front());
1136+
// If we couldn't resolve a macro decl, the attribute is invalid.
1137+
attr->setInvalid();
1138+
return nullptr;
11371139
}

0 commit comments

Comments
 (0)