Skip to content

Commit e1d13b8

Browse files
committed
[Sema] Don't warn for availability of extensions with no public members
rdar://problem/61879659
1 parent b27f3e0 commit e1d13b8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,6 +2862,27 @@ void swift::checkExplicitAvailability(Decl *decl) {
28622862
valueDecl = extension->getExtendedNominal();
28632863
if (!valueDecl)
28642864
return;
2865+
2866+
// Skip extensions without public members or conformances.
2867+
auto members = extension->getMembers();
2868+
auto hasMembers = std::any_of(members.begin(), members.end(),
2869+
[](const Decl *D) -> bool {
2870+
if (auto VD = dyn_cast<ValueDecl>(D))
2871+
if (declNeedsExplicitAvailability(VD))
2872+
return true;
2873+
return false;
2874+
});
2875+
2876+
auto protocols = extension->getLocalProtocols(ConformanceLookupKind::OnlyExplicit);
2877+
auto hasProtocols = std::any_of(protocols.begin(), protocols.end(),
2878+
[](const ProtocolDecl *PD) -> bool {
2879+
AccessScope scope =
2880+
PD->getFormalAccessScope(/*useDC*/nullptr,
2881+
/*treatUsableFromInlineAsPublic*/true);
2882+
return scope.isPublic();
2883+
});
2884+
2885+
if (!hasMembers && !hasProtocols) return;
28652886
}
28662887

28672888
if (declNeedsExplicitAvailability(valueDecl)) {

test/attr/require_explicit_availability.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Test the -require-explicit-availability flag
2+
13
// RUN: %swiftc_driver -typecheck -parse-stdlib -target x86_64-apple-macosx10.10 -Xfrontend -verify -require-explicit-availability -require-explicit-availability-target "macOS 10.10" %s
24
// RUN: %swiftc_driver -typecheck -parse-stdlib -target x86_64-apple-macosx10.10 -warnings-as-errors %s
35

@@ -43,8 +45,21 @@ public class C { } // expected-warning {{public declarations should have an avai
4345

4446
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}}
4547

48+
private protocol PrivateProto { }
49+
4650
extension S { // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
47-
func ok() { }
51+
public func warnForPublicMembers() { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{3-3=@available(macOS 10.10, *)\n }}
52+
}
53+
54+
extension S {
55+
internal func dontWarnWithoutPublicMembers() { }
56+
private func dontWarnWithoutPublicMembers1() { }
57+
}
58+
59+
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}}
60+
}
61+
62+
extension S : PrivateProto {
4863
}
4964

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

0 commit comments

Comments
 (0)