Merge pull request #67461 from hborla/invalid-attached-macro-loc

This commit is contained in:
Holly Borla
2023-07-21 21:48:08 -07:00
committed by GitHub
6 changed files with 17 additions and 14 deletions

View File

@@ -3686,7 +3686,13 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
// Try resolving an attached macro attribute. // Try resolving an attached macro attribute.
if (auto *macro = D->getResolvedMacro(attr)) { if (auto *macro = D->getResolvedMacro(attr)) {
for (auto *roleAttr : macro->getAttrs().getAttributes<MacroRoleAttr>()) { for (auto *roleAttr : macro->getAttrs().getAttributes<MacroRoleAttr>()) {
diagnoseInvalidAttachedMacro(roleAttr->getMacroRole(), D); auto role = roleAttr->getMacroRole();
if (isInvalidAttachedMacro(role, D)) {
diagnoseAndRemoveAttr(attr,
diag::macro_attached_to_invalid_decl,
getMacroRoleString(role),
D->getDescriptiveKind());
}
} }
return; return;

View File

@@ -543,8 +543,8 @@ static Identifier makeIdentifier(ASTContext &ctx, std::nullptr_t) {
return Identifier(); return Identifier();
} }
bool swift::diagnoseInvalidAttachedMacro(MacroRole role, bool swift::isInvalidAttachedMacro(MacroRole role,
Decl *attachedTo) { Decl *attachedTo) {
switch (role) { switch (role) {
case MacroRole::Expression: case MacroRole::Expression:
case MacroRole::Declaration: case MacroRole::Declaration:
@@ -582,9 +582,6 @@ bool swift::diagnoseInvalidAttachedMacro(MacroRole role,
break; break;
} }
attachedTo->diagnose(diag::macro_attached_to_invalid_decl,
getMacroRoleString(role),
attachedTo->getDescriptiveKind());
return true; return true;
} }

View File

@@ -90,10 +90,10 @@ bool accessorMacroOnlyIntroducesObservers(
bool accessorMacroIntroducesInitAccessor( bool accessorMacroIntroducesInitAccessor(
MacroDecl *macro, const MacroRoleAttr *attr); MacroDecl *macro, const MacroRoleAttr *attr);
/// Diagnose an error if the given macro role does not apply /// Return true if the given macro role does not apply
/// to the declaration kind of \c attachedTo. /// to the declaration kind of \c attachedTo.
bool diagnoseInvalidAttachedMacro(MacroRole role, bool isInvalidAttachedMacro(MacroRole role,
Decl *attachedTo); Decl *attachedTo);
} // end namespace swift } // end namespace swift

View File

@@ -144,9 +144,9 @@ expansionOrder.originalMember = 28
#if TEST_DIAGNOSTICS #if TEST_DIAGNOSTICS
@wrapAllProperties @wrapAllProperties
typealias A = Int typealias A = Int
// expected-error@-1{{'memberAttribute' macro cannot be attached to type alias}} // expected-error@-2{{'memberAttribute' macro cannot be attached to type alias}}
@wrapAllProperties @wrapAllProperties
func noMembers() {} func noMembers() {}
// expected-error@-1{{'memberAttribute' macro cannot be attached to global function}} // expected-error@-2{{'memberAttribute' macro cannot be attached to global function}}
#endif #endif

View File

@@ -113,11 +113,11 @@ func testLocal() {
@DelegatedConformance @DelegatedConformance
typealias A = Int typealias A = Int
// expected-error@-1 {{'extension' macro cannot be attached to type alias}} // expected-error@-2 {{'extension' macro cannot be attached to type alias}}
@DelegatedConformance @DelegatedConformance
extension Int {} extension Int {}
// expected-error@-1 {{'extension' macro cannot be attached to extension}} // expected-error@-2 {{'extension' macro cannot be attached to extension}}
@attached(extension, conformances: P) @attached(extension, conformances: P)
macro UndocumentedNamesInExtension() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro") macro UndocumentedNamesInExtension() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro")

View File

@@ -22,7 +22,7 @@ macro addMembersQuotedInit() = #externalMacro(module: "MacroDefinition", type: "
#if TEST_DIAGNOSTICS #if TEST_DIAGNOSTICS
@addMembers @addMembers
import Swift import Swift
// expected-error@-1 {{'member' macro cannot be attached to import}} // expected-error@-2 {{'member' macro cannot be attached to import}}
#endif #endif
@addMembers @addMembers