Skip to content

Sema: Skip members of unavailable extensions for -require-explicit-availability #58426

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
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
7 changes: 5 additions & 2 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3924,11 +3924,14 @@ static bool declNeedsExplicitAvailability(const Decl *decl) {
decl->isImplicit())
return false;

// Skip unavailable decls.
if (AvailableAttr::isUnavailable(decl))
return false;

// Warn on decls without an introduction version.
auto &ctx = decl->getASTContext();
auto safeRangeUnderApprox = AvailabilityInference::availableRange(decl, ctx);
return !safeRangeUnderApprox.getOSVersion().hasLowerEndpoint() &&
!decl->getAttrs().isUnavailable(ctx);
return !safeRangeUnderApprox.getOSVersion().hasLowerEndpoint();
}

void swift::checkExplicitAvailability(Decl *decl) {
Expand Down
36 changes: 36 additions & 0 deletions test/attr/require_explicit_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public struct S { // expected-warning {{public declarations should have an avail
public func method() { }
}

@available(macOS, unavailable)
public struct UnavailableStruct {
public func okMethod() { }
}

public func foo() { bar() } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@usableFromInline
Expand Down Expand Up @@ -41,10 +46,25 @@ public func +(lhs: S, rhs: S) -> S { } // expected-warning {{public declarations

public enum E { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@available(macOS, unavailable)
public enum UnavailableEnum {
case caseOk
}

public class C { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@available(macOS, unavailable)
public class UnavailableClass {
public func okMethod() { }
}

public protocol P { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@available(macOS, unavailable)
public protocol UnavailableProto {
func requirementOk()
}

private protocol PrivateProto { }

extension S { // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
Expand All @@ -56,11 +76,19 @@ extension S {
public func okWhenTheExtensionHasAttribute() { }
}

@available(macOS, unavailable)
extension S {
public func okWhenTheExtensionIsUnavailable() { }
}

extension S {
internal func dontWarnWithoutPublicMembers() { }
private func dontWarnWithoutPublicMembers1() { }
}

// An empty extension should be ok.
extension S { }

extension S : P { // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
}

Expand Down Expand Up @@ -96,6 +124,9 @@ public var publicVar = S() // expected-warning {{public declarations should have
@available(macOS 10.10, *)
public var publicVarOk = S()

@available(macOS, unavailable)
public var unavailablePublicVarOk = S()

public var (a, b) = (S(), S()) // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@available(macOS 10.10, *)
Expand All @@ -112,6 +143,11 @@ public var implicitGetOk: S {
return S()
}

@available(macOS, unavailable)
public var unavailableImplicitGetOk: S {
return S()
}

public var computed: S { // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
get { return S() }
set { }
Expand Down