Skip to content

Commit f62c6c0

Browse files
authored
Merge pull request #67698 from DougGregor/macro-reference-availability
[Macros] Emit deprecation warnings for uses of macros.
2 parents c4d6515 + c3af8cb commit f62c6c0

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
528528

529529
DeclVisitor<AccessControlChecker>::visit(D);
530530
checkGlobalActorAccess(D);
531+
checkAttachedMacrosAccess(D);
531532
}
532533

533534
// Force all kinds to be handled at a lower level.
@@ -1260,6 +1261,18 @@ class AccessControlChecker : public AccessControlCheckerBase,
12601261
noteLimitingImport(MD->getASTContext(), minImportLimit, complainRepr);
12611262
}
12621263
}
1264+
1265+
void checkAttachedMacrosAccess(const Decl *D) {
1266+
for (auto customAttrC : D->getSemanticAttrs().getAttributes<CustomAttr>()) {
1267+
auto customAttr = const_cast<CustomAttr *>(customAttrC);
1268+
auto *macroDecl = D->getResolvedMacro(customAttr);
1269+
if (macroDecl) {
1270+
diagnoseDeclAvailability(
1271+
macroDecl, customAttr->getTypeRepr()->getSourceRange(), nullptr,
1272+
ExportContext::forDeclSignature(const_cast<Decl *>(D)), llvm::None);
1273+
}
1274+
}
1275+
}
12631276
};
12641277

12651278
class UsableFromInlineChecker : public AccessControlCheckerBase,

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,11 @@ class ExprAvailabilityWalker : public ASTWalker {
32853285
}
32863286
}
32873287

3288+
if (auto ME = dyn_cast<MacroExpansionExpr>(E)) {
3289+
diagnoseDeclRefAvailability(
3290+
ME->getMacroRef(), ME->getMacroNameLoc().getSourceRange());
3291+
}
3292+
32883293
return Action::Continue(E);
32893294
}
32903295

test/Macros/macro_expand.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,23 @@ func testExpressionAsDeclarationMacro() {
547547
// expected-error@-1{{macro implementation type 'StringifyMacro' doesn't conform to required protocol 'DeclarationMacro' (from macro 'stringifyAsDeclMacro')}}
548548
#endif
549549
}
550+
551+
// Deprecated macro
552+
@available(*, deprecated, message: "This macro is deprecated.")
553+
@freestanding(expression) macro deprecatedStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
554+
555+
@available(*, deprecated, message: "This macro is deprecated.")
556+
@freestanding(declaration) macro deprecatedStringifyAsDeclMacro<T>(_ value: T) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
557+
558+
func testDeprecated() {
559+
// expected-warning@+1{{'deprecatedStringify' is deprecated: This macro is deprecated.}}
560+
_ = #deprecatedStringify(1 + 1)
561+
}
562+
563+
#if TEST_DIAGNOSTICS
564+
struct DeprecatedStructWrapper {
565+
// expected-error@+2{{macro implementation type 'StringifyMacro' doesn't conform to required protocol 'DeclarationMacro' (from macro 'deprecatedStringifyAsDeclMacro')}}
566+
// expected-warning@+1{{'deprecatedStringifyAsDeclMacro' is deprecated: This macro is deprecated.}}
567+
#deprecatedStringifyAsDeclMacro(1 + 1)
568+
}
569+
#endif

test/Macros/macro_expand_peers.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,18 @@ func testStructWithPeers() {
235235
let x = SomeStructWithPeerProperties()
236236
print(x)
237237
}
238+
239+
240+
#if TEST_DIAGNOSTICS
241+
@available(*, deprecated, message: "This macro is deprecated.")
242+
@attached(peer, names: overloaded)
243+
macro deprecatedAddCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
244+
245+
246+
// expected-warning@+1{{'deprecatedAddCompletionHandler()' is deprecated: This macro is deprecated.}}
247+
@deprecatedAddCompletionHandler
248+
func fDeprecated(a: Int, for b: String, _ value: Double) async -> String {
249+
return b
250+
}
251+
252+
#endif

0 commit comments

Comments
 (0)