Skip to content

Commit e12e16d

Browse files
authored
Merge pull request #65772 from tshortli/suppress-explicit-availability-diags-non-darwin
Sema: Suppress explicit availability diagnostics on non-Darwin targets
2 parents d006afb + 93a96e8 commit e12e16d

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,9 @@ class ASTContext final {
970970
/// Swift compiler for future versions of the target platform.
971971
AvailabilityContext getSwiftFutureAvailability();
972972

973+
/// Returns `true` if versioned availability annotations are supported for the
974+
/// target triple.
975+
bool supportsVersionedAvailability() const;
973976

974977
//===--------------------------------------------------------------------===//
975978
// Diagnostics Helper functions

lib/AST/Availability.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/TypeCheckRequests.h"
2323
#include "swift/AST/TypeWalker.h"
2424
#include "swift/AST/Types.h"
25+
#include "swift/Basic/Platform.h"
2526
#include <map>
2627

2728
using namespace swift;
@@ -706,3 +707,7 @@ ASTContext::getSwift5PlusAvailability(llvm::VersionTuple swiftVersion) {
706707
Twine("Missing call to getSwiftXYAvailability for Swift ") +
707708
swiftVersion.getAsString());
708709
}
710+
711+
bool ASTContext::supportsVersionedAvailability() const {
712+
return minimumAvailableOSVersionForTriple(LangOpts.Target).has_value();
713+
}

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4261,6 +4261,13 @@ swift::diagnoseSubstitutionMapAvailability(SourceLoc loc,
42614261
/// Should we warn that \p decl needs an explicit availability annotation
42624262
/// in -require-explicit-availability mode?
42634263
static bool declNeedsExplicitAvailability(const Decl *decl) {
4264+
auto &ctx = decl->getASTContext();
4265+
4266+
// Don't require an introduced version on platforms that don't support
4267+
// versioned availability.
4268+
if (!ctx.supportsVersionedAvailability())
4269+
return false;
4270+
42644271
// Skip non-public decls.
42654272
if (auto valueDecl = dyn_cast<const ValueDecl>(decl)) {
42664273
AccessScope scope =
@@ -4281,17 +4288,16 @@ static bool declNeedsExplicitAvailability(const Decl *decl) {
42814288
return false;
42824289

42834290
// Warn on decls without an introduction version.
4284-
auto &ctx = decl->getASTContext();
42854291
auto safeRangeUnderApprox = AvailabilityInference::availableRange(decl, ctx);
42864292
return !safeRangeUnderApprox.getOSVersion().hasLowerEndpoint();
42874293
}
42884294

42894295
void swift::checkExplicitAvailability(Decl *decl) {
42904296
// Skip if the command line option was not set and
42914297
// accessors as we check the pattern binding decl instead.
4292-
auto DiagLevel = decl->getASTContext().LangOpts.RequireExplicitAvailability;
4293-
if (!DiagLevel ||
4294-
isa<AccessorDecl>(decl))
4298+
auto &ctx = decl->getASTContext();
4299+
auto DiagLevel = ctx.LangOpts.RequireExplicitAvailability;
4300+
if (!DiagLevel || isa<AccessorDecl>(decl))
42954301
return;
42964302

42974303
// Only look at decls at module level or in extensions.
@@ -4336,8 +4342,7 @@ void swift::checkExplicitAvailability(Decl *decl) {
43364342
auto diag = decl->diagnose(diag::public_decl_needs_availability);
43374343
diag.limitBehavior(*DiagLevel);
43384344

4339-
auto suggestPlatform =
4340-
decl->getASTContext().LangOpts.RequireExplicitAvailabilityTarget;
4345+
auto suggestPlatform = ctx.LangOpts.RequireExplicitAvailabilityTarget;
43414346
if (!suggestPlatform.empty()) {
43424347
auto InsertLoc = decl->getAttrs().getStartLoc(/*forModifiers=*/false);
43434348
if (InsertLoc.isInvalid())
@@ -4350,7 +4355,6 @@ void swift::checkExplicitAvailability(Decl *decl) {
43504355
{
43514356
llvm::raw_string_ostream Out(AttrText);
43524357

4353-
auto &ctx = decl->getASTContext();
43544358
StringRef OriginalIndent = Lexer::getIndentationForLine(
43554359
ctx.SourceMgr, InsertLoc);
43564360
Out << "@available(" << suggestPlatform << ", *)\n"

test/SPI/spi-only-and-library-level.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
// RUN: %target-swift-frontend -emit-module %t/APILib.swift -I %t \
88
// RUN: -swift-version 5 -verify \
99
// RUN: -experimental-spi-only-imports \
10-
// RUN: -library-level api
10+
// RUN: -library-level api \
11+
// RUN: -require-explicit-availability=ignore
1112
// RUN: %target-swift-frontend -emit-module %t/SPILib.swift -I %t \
1213
// RUN: -swift-version 5 -verify \
1314
// RUN: -experimental-spi-only-imports \
@@ -25,7 +26,6 @@ public struct LibStruct {}
2526
@_spiOnly import Lib
2627

2728
public func publicClient() -> LibStruct { fatalError() } // expected-error {{cannot use struct 'LibStruct' here; 'Lib' was imported for SPI only}}
28-
// expected-warning @-1 {{public declarations should have an availability attribute with an introduction version}}
2929
@_spi(X) public func spiClient() -> LibStruct { fatalError() }
3030

3131
//--- SPILib.swift
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test that the -require-explicit-availability flag does not cause diagnostics
2+
// to be emitted on platforms where versioned availability annotations are not
3+
// meaningful.
4+
5+
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s -require-explicit-availability=error
6+
7+
// Currently versioned availability should only be required on Apple platforms.
8+
// UNSUPPORTED: VENDOR=apple
9+
10+
public struct NoAvailabilityStruct {
11+
public func method() { }
12+
}
13+
14+
@available(*, unavailable)
15+
public struct UnavailableStruct {
16+
public func okMethod() { }
17+
}
18+
19+
public func noAvailabilityFunc() { }
20+
21+
@available(SwiftStdlib 5.9, *)
22+
public func stdlib5_9AvailabilityFunc() { }

0 commit comments

Comments
 (0)