Skip to content

[Macros] Emit deprecation warnings for uses of macros. #67698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ class AccessControlChecker : public AccessControlCheckerBase,

DeclVisitor<AccessControlChecker>::visit(D);
checkGlobalActorAccess(D);
checkAttachedMacrosAccess(D);
}

// Force all kinds to be handled at a lower level.
Expand Down Expand Up @@ -1260,6 +1261,18 @@ class AccessControlChecker : public AccessControlCheckerBase,
noteLimitingImport(MD->getASTContext(), minImportLimit, complainRepr);
}
}

void checkAttachedMacrosAccess(const Decl *D) {
for (auto customAttrC : D->getSemanticAttrs().getAttributes<CustomAttr>()) {
auto customAttr = const_cast<CustomAttr *>(customAttrC);
auto *macroDecl = D->getResolvedMacro(customAttr);
if (macroDecl) {
diagnoseDeclAvailability(
macroDecl, customAttr->getTypeRepr()->getSourceRange(), nullptr,
ExportContext::forDeclSignature(const_cast<Decl *>(D)), llvm::None);
}
}
}
};

class UsableFromInlineChecker : public AccessControlCheckerBase,
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3285,6 +3285,11 @@ class ExprAvailabilityWalker : public ASTWalker {
}
}

if (auto ME = dyn_cast<MacroExpansionExpr>(E)) {
diagnoseDeclRefAvailability(
ME->getMacroRef(), ME->getMacroNameLoc().getSourceRange());
}

return Action::Continue(E);
}

Expand Down
20 changes: 20 additions & 0 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")

@available(*, deprecated, message: "This macro is deprecated.")
@freestanding(declaration) macro deprecatedStringifyAsDeclMacro<T>(_ 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
15 changes: 15 additions & 0 deletions test/Macros/macro_expand_peers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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