Skip to content

Commit 5889c59

Browse files
authored
Merge pull request #67751 from DougGregor/macro-reference-availability-5.9
[5.9] Availability checking for uses of macros
2 parents 2211b2c + 75078e3 commit 5889c59

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,7 @@ class TypeRefinementContextBuilder : private ASTWalker {
634634
return Range;
635635
}
636636

637-
// For pattern binding declarations, include the attributes in the source
638-
// range so that we're sure to cover any property wrappers.
639-
if (auto patternBinding = dyn_cast<PatternBindingDecl>(D)) {
640-
return D->getSourceRangeIncludingAttrs();
641-
}
642-
643-
return D->getSourceRange();
637+
return D->getSourceRangeIncludingAttrs();
644638
}
645639

646640
// Creates an implicit decl TRC specifying the deployment
@@ -3252,6 +3246,11 @@ class ExprAvailabilityWalker : public ASTWalker {
32523246
}
32533247
}
32543248

3249+
if (auto ME = dyn_cast<MacroExpansionExpr>(E)) {
3250+
diagnoseDeclRefAvailability(
3251+
ME->getMacroRef(), ME->getMacroNameLoc().getSourceRange());
3252+
}
3253+
32553254
return Action::Continue(E);
32563255
}
32573256

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// REQUIRES: swift_swift_parser, asserts
2+
// REQUIRES: OS=macosx
3+
4+
// RUN: %target-typecheck-verify-swift -swift-version 5 -module-name MacrosTest -target %target-cpu-apple-macos50
5+
6+
@freestanding(expression)
7+
macro overloadedOnAvailability(_: Any) -> Int = #externalMacro(module: "MacroLibrary", type: "MyOldMacro")
8+
//expected-warning@-1{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
9+
// expected-note@-2 2{{'overloadedOnAvailability' declared here}}
10+
11+
@available(macOS 60, *)
12+
@freestanding(expression)
13+
macro overloadedOnAvailability(_: Int) -> Double = #externalMacro(module: "MacroLibrary", type: "MyNewMacro")
14+
//expected-warning@-1{{external macro implementation type 'MacroLibrary.MyNewMacro'}}
15+
// expected-note@-2{{'overloadedOnAvailability' declared here}}
16+
17+
18+
func mutateInt(_: inout Int) { }
19+
func mutateDouble(_: inout Double) { }
20+
21+
func testAvailabilityOld() {
22+
var a = #overloadedOnAvailability(1)
23+
mutateInt(&a)
24+
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
25+
}
26+
27+
@available(macOS 60, *)
28+
func testAvailabilitNew(a: Any) {
29+
var a = #overloadedOnAvailability(1)
30+
mutateDouble(&a)
31+
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyNewMa}}
32+
33+
var b = #overloadedOnAvailability(a)
34+
mutateInt(&b)
35+
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
36+
}

test/Macros/macro_expand.swift

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

test/Macros/macro_expand_peers.swift

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

0 commit comments

Comments
 (0)