Skip to content

[5.1] Support unavailable and obsoleted attributes on extensions #26009

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 3 commits into from
Jul 17, 2019
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
9 changes: 8 additions & 1 deletion lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,14 @@ AvailableVersionComparison AvailableAttr::getVersionAvailability(

const AvailableAttr *AvailableAttr::isUnavailable(const Decl *D) {
ASTContext &ctx = D->getASTContext();
return D->getAttrs().getUnavailable(ctx);
if (auto attr = D->getAttrs().getUnavailable(ctx))
return attr;

// If D is an extension member, check if the extension is unavailable.
if (auto ext = dyn_cast<ExtensionDecl>(D->getDeclContext()))
return AvailableAttr::isUnavailable(ext);

return nullptr;
}

SpecializeAttr::SpecializeAttr(SourceLoc atLoc, SourceRange range,
Expand Down
12 changes: 8 additions & 4 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,13 +1502,17 @@ static bool isInsideUnavailableDeclaration(SourceRange ReferenceRange,
/// Returns true if the reference or any of its parents is an
/// unconditional unavailable declaration for the same platform.
static bool isInsideCompatibleUnavailableDeclaration(
SourceRange ReferenceRange, const DeclContext *ReferenceDC,
const AvailableAttr *attr) {
const ValueDecl *referencedD, SourceRange ReferenceRange,
const DeclContext *ReferenceDC, const AvailableAttr *attr) {
if (!attr->isUnconditionallyUnavailable()) {
return false;
}

// Refuse calling unavailable functions from unavailable code,
// but allow the use of types.
PlatformKind platform = attr->Platform;
if (platform == PlatformKind::none) {
if (platform == PlatformKind::none &&
!isa<TypeDecl>(referencedD)) {
return false;
}

Expand Down Expand Up @@ -2163,7 +2167,7 @@ bool swift::diagnoseExplicitUnavailability(
// unavailability is OK -- the eventual caller can't call the
// enclosing code in the same situations it wouldn't be able to
// call this code.
if (isInsideCompatibleUnavailableDeclaration(R, DC, Attr)) {
if (isInsideCompatibleUnavailableDeclaration(D, R, DC, Attr)) {
return false;
}

Expand Down
13 changes: 13 additions & 0 deletions test/attr/attr_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1087,3 +1087,16 @@ func rdar46348825_deprecated() {}
@available(swift, obsoleted: 4.0, obsoleted: 4.0)
// expected-warning@-1 {{'obsoleted' argument has already been specified}}
func rdar46348825_obsoleted() {}

// Referencing unavailable types in signatures of unavailable functions should be accepted
@available(*, unavailable)
protocol UnavailableProto {
}

@available(*, unavailable)
func unavailableFunc(_ arg: UnavailableProto) -> UnavailableProto {}

@available(*, unavailable)
struct S {
var a: UnavailableProto
}
57 changes: 57 additions & 0 deletions test/attr/attr_availability_osx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,60 @@ func testAccessors() {
t.unavailableSetter = .init() // expected-error {{setter for 'unavailableSetter' is unavailable in macOS: bad setter}}
t.unavailableSetter.mutate() // expected-error {{setter for 'unavailableSetter' is unavailable in macOS: bad setter}}
}

// Check available on extensions

@available(macOS, unavailable)
extension TestStruct {
func unavailInExtension() {} // expected-note 2 {{'unavailInExtension()' has been explicitly marked unavailable here}}
}

@available(macOS, obsoleted: 10.0)
extension TestStruct {
func obsoletedInExtension() {} // expected-note 2 {{'obsoletedInExtension()' was obsoleted in macOS 10.0}}
}

@available(macOS, deprecated: 10.0)
extension TestStruct {
func deprecatedInExtension() {}
}

@available(swift, introduced: 50.0)
extension TestStruct {
func introducedInExtensionSwift() {} // expected-note 2 {{'introducedInExtensionSwift()' was introduced in Swift 50.0}}
}

@available(macOS, introduced: 10.50)
extension TestStruct {
func introducedInExtensionMacOS() {}
}

TestStruct().unavailInExtension() // expected-error {{'unavailInExtension()' is unavailable in macOS}}
TestStruct().obsoletedInExtension() // expected-error {{'obsoletedInExtension()' is unavailable}}
TestStruct().deprecatedInExtension() // expected-warning {{'deprecatedInExtension()' was deprecated in macOS 10.0}}
TestStruct().introducedInExtensionSwift() // expected-error {{'introducedInExtensionSwift()' is unavailable}}
TestStruct().introducedInExtensionMacOS() // expected-error {{'introducedInExtensionMacOS()' is only available in macOS 10.50 or newer}}
// expected-note@-1{{add 'if #available' version check}}

extension TestStruct {
func availableFunc() {
unavailInExtension() // expected-error {{'unavailInExtension()' is unavailable in macOS}}
obsoletedInExtension() // expected-error {{'obsoletedInExtension()' is unavailable}}
deprecatedInExtension() // expected-warning {{'deprecatedInExtension()' was deprecated in macOS 10.0}}
introducedInExtensionSwift() // expected-error {{'introducedInExtensionSwift()' is unavailable}}
}
}

extension TestStruct { // expected-note{{add @available attribute to enclosing extension}}
func availableFuncMacOS() { // expected-note{{add @available attribute to enclosing instance method}}
introducedInExtensionMacOS() // expected-error {{'introducedInExtensionMacOS()' is only available in macOS 10.50 or newer}}
// expected-note@-1{{add 'if #available' version check}}
}
}

@available(macOS, introduced: 10.50)
extension TestStruct {
func futureFuncMacOS() {
introducedInExtensionMacOS()
}
}
6 changes: 3 additions & 3 deletions test/attr/attr_availability_swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ extension TestStruct {
func doTheThing() {} // expected-note {{'doTheThing()' was introduced in Swift 400}}
}

@available(swift 400) // FIXME: This has no effect and should be complained about.
@available(swift 400)
extension TestStruct {
func doAnotherThing() {}
func doAnotherThing() {} // expected-note {{'doAnotherThing()' was introduced in Swift 400}}
}

@available(macOS 10.11, *)
func testMemberAvailability() {
TestStruct().doTheThing() // expected-error {{'doTheThing()' is unavailable}}
TestStruct().doAnotherThing() // okay (for now)
TestStruct().doAnotherThing() // expected-error {{'doAnotherThing()' is unavailable}}
}

@available(swift 400) // FIXME: This has no effect and should be complained about.
Expand Down