Skip to content

[5.9] Availability checking for uses of macros #67751

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
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
13 changes: 6 additions & 7 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,7 @@ class TypeRefinementContextBuilder : private ASTWalker {
return Range;
}

// For pattern binding declarations, include the attributes in the source
// range so that we're sure to cover any property wrappers.
if (auto patternBinding = dyn_cast<PatternBindingDecl>(D)) {
return D->getSourceRangeIncludingAttrs();
}

return D->getSourceRange();
return D->getSourceRangeIncludingAttrs();
}

// Creates an implicit decl TRC specifying the deployment
Expand Down Expand Up @@ -3252,6 +3246,11 @@ class ExprAvailabilityWalker : public ASTWalker {
}
}

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

return Action::Continue(E);
}

Expand Down
36 changes: 36 additions & 0 deletions test/Macros/macro_availability_macos.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// REQUIRES: swift_swift_parser, asserts
// REQUIRES: OS=macosx

// RUN: %target-typecheck-verify-swift -swift-version 5 -module-name MacrosTest -target %target-cpu-apple-macos50

@freestanding(expression)
macro overloadedOnAvailability(_: Any) -> Int = #externalMacro(module: "MacroLibrary", type: "MyOldMacro")
//expected-warning@-1{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
// expected-note@-2 2{{'overloadedOnAvailability' declared here}}

@available(macOS 60, *)
@freestanding(expression)
macro overloadedOnAvailability(_: Int) -> Double = #externalMacro(module: "MacroLibrary", type: "MyNewMacro")
//expected-warning@-1{{external macro implementation type 'MacroLibrary.MyNewMacro'}}
// expected-note@-2{{'overloadedOnAvailability' declared here}}


func mutateInt(_: inout Int) { }
func mutateDouble(_: inout Double) { }

func testAvailabilityOld() {
var a = #overloadedOnAvailability(1)
mutateInt(&a)
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
}

@available(macOS 60, *)
func testAvailabilitNew(a: Any) {
var a = #overloadedOnAvailability(1)
mutateDouble(&a)
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyNewMa}}

var b = #overloadedOnAvailability(a)
mutateInt(&b)
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
}
20 changes: 20 additions & 0 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,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 @@ -231,3 +231,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