From 0a1fbe7f3060a6412b1897279d9dffeab510117c Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Wed, 2 Aug 2023 22:58:11 -0700 Subject: [PATCH 1/2] [Macros] Emit deprecation warnings for uses of freestanding macros. Fixes rdar://113138432. --- lib/Sema/TypeCheckAvailability.cpp | 5 +++++ test/Macros/macro_expand.swift | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/Sema/TypeCheckAvailability.cpp b/lib/Sema/TypeCheckAvailability.cpp index 5c35966d5caa2..07a9579331577 100644 --- a/lib/Sema/TypeCheckAvailability.cpp +++ b/lib/Sema/TypeCheckAvailability.cpp @@ -3285,6 +3285,11 @@ class ExprAvailabilityWalker : public ASTWalker { } } + if (auto ME = dyn_cast(E)) { + diagnoseDeclRefAvailability( + ME->getMacroRef(), ME->getMacroNameLoc().getSourceRange()); + } + return Action::Continue(E); } diff --git a/test/Macros/macro_expand.swift b/test/Macros/macro_expand.swift index cd74d5ebd4304..7141e8a5bb81a 100644 --- a/test/Macros/macro_expand.swift +++ b/test/Macros/macro_expand.swift @@ -547,3 +547,23 @@ func testExpressionAsDeclarationMacro() { // expected-error@-1{{macro implementation type 'StringifyMacro' doesn't conform to required protocol 'DeclarationMacro' (from macro 'stringifyAsDeclMacro')}} #endif } + +// Deprecated macro +@available(*, deprecated, message: "This macro is deprecated.") +@freestanding(expression) macro deprecatedStringify(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro") + +@available(*, deprecated, message: "This macro is deprecated.") +@freestanding(declaration) macro deprecatedStringifyAsDeclMacro(_ value: T) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro") + +func testDeprecated() { + // expected-warning@+1{{'deprecatedStringify' is deprecated: This macro is deprecated.}} + _ = #deprecatedStringify(1 + 1) +} + +#if TEST_DIAGNOSTICS +struct DeprecatedStructWrapper { + // expected-error@+2{{macro implementation type 'StringifyMacro' doesn't conform to required protocol 'DeclarationMacro' (from macro 'deprecatedStringifyAsDeclMacro')}} + // expected-warning@+1{{'deprecatedStringifyAsDeclMacro' is deprecated: This macro is deprecated.}} + #deprecatedStringifyAsDeclMacro(1 + 1) +} +#endif From c3af8cbe42d98678006ae70d13f9ad94519aaf6a Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Wed, 2 Aug 2023 23:23:38 -0700 Subject: [PATCH 2/2] [Macros] Diagnose availability of attached macros --- lib/Sema/TypeCheckAccess.cpp | 13 +++++++++++++ test/Macros/macro_expand_peers.swift | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/Sema/TypeCheckAccess.cpp b/lib/Sema/TypeCheckAccess.cpp index 3996bf09beaf5..5d90d6ff95989 100644 --- a/lib/Sema/TypeCheckAccess.cpp +++ b/lib/Sema/TypeCheckAccess.cpp @@ -528,6 +528,7 @@ class AccessControlChecker : public AccessControlCheckerBase, DeclVisitor::visit(D); checkGlobalActorAccess(D); + checkAttachedMacrosAccess(D); } // Force all kinds to be handled at a lower level. @@ -1260,6 +1261,18 @@ class AccessControlChecker : public AccessControlCheckerBase, noteLimitingImport(MD->getASTContext(), minImportLimit, complainRepr); } } + + void checkAttachedMacrosAccess(const Decl *D) { + for (auto customAttrC : D->getSemanticAttrs().getAttributes()) { + auto customAttr = const_cast(customAttrC); + auto *macroDecl = D->getResolvedMacro(customAttr); + if (macroDecl) { + diagnoseDeclAvailability( + macroDecl, customAttr->getTypeRepr()->getSourceRange(), nullptr, + ExportContext::forDeclSignature(const_cast(D)), llvm::None); + } + } + } }; class UsableFromInlineChecker : public AccessControlCheckerBase, diff --git a/test/Macros/macro_expand_peers.swift b/test/Macros/macro_expand_peers.swift index 83e3474859e33..b7eca3b3325fb 100644 --- a/test/Macros/macro_expand_peers.swift +++ b/test/Macros/macro_expand_peers.swift @@ -235,3 +235,18 @@ func testStructWithPeers() { let x = SomeStructWithPeerProperties() print(x) } + + +#if TEST_DIAGNOSTICS +@available(*, deprecated, message: "This macro is deprecated.") +@attached(peer, names: overloaded) +macro deprecatedAddCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler") + + +// expected-warning@+1{{'deprecatedAddCompletionHandler()' is deprecated: This macro is deprecated.}} +@deprecatedAddCompletionHandler +func fDeprecated(a: Int, for b: String, _ value: Double) async -> String { + return b +} + +#endif