Skip to content

Commit a60b302

Browse files
committed
[Macros] Diagnose invalid attached macros at the attribute location instead
of the declaration.
1 parent 72337ef commit a60b302

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3686,7 +3686,13 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
36863686
// Try resolving an attached macro attribute.
36873687
if (auto *macro = D->getResolvedMacro(attr)) {
36883688
for (auto *roleAttr : macro->getAttrs().getAttributes<MacroRoleAttr>()) {
3689-
diagnoseInvalidAttachedMacro(roleAttr->getMacroRole(), D);
3689+
auto role = roleAttr->getMacroRole();
3690+
if (isInvalidAttachedMacro(role, D)) {
3691+
diagnoseAndRemoveAttr(attr,
3692+
diag::macro_attached_to_invalid_decl,
3693+
getMacroRoleString(role),
3694+
D->getDescriptiveKind());
3695+
}
36903696
}
36913697

36923698
return;

lib/Sema/TypeCheckMacros.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ static Identifier makeIdentifier(ASTContext &ctx, std::nullptr_t) {
543543
return Identifier();
544544
}
545545

546-
bool swift::diagnoseInvalidAttachedMacro(MacroRole role,
547-
Decl *attachedTo) {
546+
bool swift::isInvalidAttachedMacro(MacroRole role,
547+
Decl *attachedTo) {
548548
switch (role) {
549549
case MacroRole::Expression:
550550
case MacroRole::Declaration:
@@ -582,9 +582,6 @@ bool swift::diagnoseInvalidAttachedMacro(MacroRole role,
582582
break;
583583
}
584584

585-
attachedTo->diagnose(diag::macro_attached_to_invalid_decl,
586-
getMacroRoleString(role),
587-
attachedTo->getDescriptiveKind());
588585
return true;
589586
}
590587

lib/Sema/TypeCheckMacros.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ bool accessorMacroOnlyIntroducesObservers(
9090
bool accessorMacroIntroducesInitAccessor(
9191
MacroDecl *macro, const MacroRoleAttr *attr);
9292

93-
/// Diagnose an error if the given macro role does not apply
93+
/// Return true if the given macro role does not apply
9494
/// to the declaration kind of \c attachedTo.
95-
bool diagnoseInvalidAttachedMacro(MacroRole role,
96-
Decl *attachedTo);
95+
bool isInvalidAttachedMacro(MacroRole role,
96+
Decl *attachedTo);
9797

9898
} // end namespace swift
9999

test/Macros/macro_expand_attributes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ expansionOrder.originalMember = 28
144144
#if TEST_DIAGNOSTICS
145145
@wrapAllProperties
146146
typealias A = Int
147-
// expected-error@-1{{'memberAttribute' macro cannot be attached to type alias}}
147+
// expected-error@-2{{'memberAttribute' macro cannot be attached to type alias}}
148148

149149
@wrapAllProperties
150150
func noMembers() {}
151-
// expected-error@-1{{'memberAttribute' macro cannot be attached to global function}}
151+
// expected-error@-2{{'memberAttribute' macro cannot be attached to global function}}
152152
#endif

test/Macros/macro_expand_extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ func testLocal() {
113113

114114
@DelegatedConformance
115115
typealias A = Int
116-
// expected-error@-1 {{'extension' macro cannot be attached to type alias}}
116+
// expected-error@-2 {{'extension' macro cannot be attached to type alias}}
117117

118118
@DelegatedConformance
119119
extension Int {}
120-
// expected-error@-1 {{'extension' macro cannot be attached to extension}}
120+
// expected-error@-2 {{'extension' macro cannot be attached to extension}}
121121

122122
@attached(extension, conformances: P)
123123
macro UndocumentedNamesInExtension() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro")

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro addMembersQuotedInit() = #externalMacro(module: "MacroDefinition", type: "
2222
#if TEST_DIAGNOSTICS
2323
@addMembers
2424
import Swift
25-
// expected-error@-1 {{'member' macro cannot be attached to import}}
25+
// expected-error@-2 {{'member' macro cannot be attached to import}}
2626
#endif
2727

2828
@addMembers

0 commit comments

Comments
 (0)