Skip to content

Sema: Fix a regression in -require-explicit-availability diagnostics #80481

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
4 changes: 4 additions & 0 deletions include/swift/AST/DeclExportabilityVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ bool isExported(const ValueDecl *VD);
/// A specialization of `isExported` for `ExtensionDecl`.
bool isExported(const ExtensionDecl *ED);

/// Returns true if the extension declares any protocol conformances that
/// require the extension to be exported.
bool hasConformancesToPublicProtocols(const ExtensionDecl *ED);

} // end namespace swift

#endif
2 changes: 1 addition & 1 deletion lib/AST/Availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ bool swift::isExported(const ValueDecl *VD) {
return false;
}

static bool hasConformancesToPublicProtocols(const ExtensionDecl *ED) {
bool swift::hasConformancesToPublicProtocols(const ExtensionDecl *ED) {
auto nominal = ED->getExtendedNominal();
if (!nominal)
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3521,7 +3521,7 @@ void swift::checkExplicitAvailability(Decl *decl) {
return false;
});

if (!hasMembers && !isExported(extension))
if (!hasMembers && !hasConformancesToPublicProtocols(extension))
return;
} else if (auto pbd = dyn_cast<PatternBindingDecl>(decl)) {
// Check the first var instead.
Expand Down
38 changes: 26 additions & 12 deletions test/attr/require_explicit_availability_macos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
// RUN: %empty-directory(%t)

/// Using the flag directly raises warnings and fixits.
// RUN: %swiftc_driver -typecheck -parse-as-library -Xfrontend -verify %s \
// RUN: -target %target-cpu-apple-macosx10.10 -require-explicit-availability \
// RUN: -require-explicit-availability-target "macOS 10.10"
// RUN: %swiftc_driver -typecheck -parse-as-library -Xfrontend -verify %s \
// RUN: -target %target-cpu-apple-macosx10.10 -require-explicit-availability=warn \
// RUN: -require-explicit-availability-target "macOS 10.10"
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s \
// RUN: -target %target-cpu-apple-macosx10.10 -package-name Foo \
// RUN: -require-explicit-availability -require-explicit-availability-target "macOS 10.10"
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s \
// RUN: -target %target-cpu-apple-macosx10.10 -package-name Foo \
// RUN: -require-explicit-availability=warn -require-explicit-availability-target "macOS 10.10"

/// Using -library-level api defaults to enabling warnings, without fixits.
// RUN: sed -e "s/}} {{.*/}}/" < %s > %t/NoFixits.swift
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %t/NoFixits.swift \
// RUN: -target %target-cpu-apple-macosx10.10 -library-level api
// RUN: -target %target-cpu-apple-macosx10.10 -package-name Foo -library-level api

/// Explicitly disable the diagnostic.
// RUN: sed -e 's/xpected-warning/not-something-expected/' < %s > %t/None.swift
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %t/None.swift \
// RUN: -target %target-cpu-apple-macosx10.10 -require-explicit-availability=ignore \
// RUN: -require-explicit-availability-target "macOS 10.10" -library-level api
// RUN: -target %target-cpu-apple-macosx10.10 -package-name Foo -library-level api \
// RUN: -require-explicit-availability=ignore -require-explicit-availability-target "macOS 10.10"

/// Upgrade the diagnostic to an error.
// RUN: sed -e "s/xpected-warning/xpected-error/" < %s > %t/Errors.swift
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %t/Errors.swift \
// RUN: -target %target-cpu-apple-macosx10.10 -require-explicit-availability=error \
// RUN: -require-explicit-availability-target "macOS 10.10"
// RUN: -target %target-cpu-apple-macosx10.10 -package-name Foo \
// RUN: -require-explicit-availability=error -require-explicit-availability-target "macOS 10.10"

/// Error on an invalid argument.
// RUN: not %target-swift-frontend -typecheck %s -require-explicit-availability=NotIt 2>&1 \
Expand Down Expand Up @@ -58,7 +58,9 @@ public func missingIntro() { } // expected-warning {{public declarations should
@available(iOS 9.0, *)
public func missingTargetPlatform() { } // expected-warning {{public declarations should have an availability attribute with an introduction version}} {{-1:1-1=@available(macOS 10.10, *)\n}}

func privateFunc() { }
private func privateFunc() { }
internal func internalFunc() { }
package func packageFunc() { }

@_alwaysEmitIntoClient
public func alwaysEmitted() { }
Expand Down Expand Up @@ -113,6 +115,18 @@ extension S {
extension S {
internal func dontWarnWithoutPublicMembers() { }
private func dontWarnWithoutPublicMembers1() { }
package func dontWarnWithoutPublicMembers2() { }
}

extension S {
@_spi(SPIsAreOK)
public func dontWarnWithSPIMembers() {}

@available(macOS, unavailable)
public func dontWarnWithUnavailableMembers() { }

@_alwaysEmitIntoClient
public func dontWarnWithAEICMembers() { }
}

// An empty extension should be ok.
Expand Down