Skip to content

sema: diagnose @_spi_available on all platforms #60983

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 1 commit into from
Sep 8, 2022
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/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,10 @@ ERROR(originally_definedin_must_not_before_available_version,none,
"symbols are moved to the current module before they were available in "
"the OSs", ())

WARNING(spi_preferred_over_spi_available,none,
"symbols that are @_spi_available on all platforms should use @_spi "
"instead", ())

// Alignment attribute
ERROR(alignment_not_power_of_two,none,
"alignment value must be a power of two", ())
Expand Down
23 changes: 21 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {

void visitCompilerInitializedAttr(CompilerInitializedAttr *attr);

void checkAvailableAttrs(ArrayRef<AvailableAttr *> Attrs);
void checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs);

void visitKnownToBeLocalAttr(KnownToBeLocalAttr *attr);
Expand Down Expand Up @@ -1421,8 +1422,10 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
TypeChecker::applyAccessNote(VD);

AttributeChecker Checker(D);
// We need to check all OriginallyDefinedInAttr and BackDeployAttr relative
// to each other, so collect them and check in batch later.
// We need to check all availableAttrs, OriginallyDefinedInAttr and
// BackDeployAttr relative to each other, so collect them and check in
// batch later.
llvm::SmallVector<AvailableAttr *, 4> availableAttrs;
llvm::SmallVector<BackDeployAttr *, 4> backDeployAttrs;
llvm::SmallVector<OriginallyDefinedInAttr*, 4> ODIAttrs;
for (auto attr : D->getAttrs()) {
Expand All @@ -1436,6 +1439,10 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
} else if (auto *BD = dyn_cast<BackDeployAttr>(attr)) {
backDeployAttrs.push_back(BD);
} else {
// check @available attribute both collectively and individually.
if (auto *AV = dyn_cast<AvailableAttr>(attr)) {
availableAttrs.push_back(AV);
}
// Otherwise, check it.
Checker.visit(attr);
}
Expand Down Expand Up @@ -1475,6 +1482,7 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
else
Checker.diagnoseAndRemoveAttr(attr, diag::invalid_decl_attribute, attr);
}
Checker.checkAvailableAttrs(availableAttrs);
Checker.checkBackDeployAttrs(backDeployAttrs);
Checker.checkOriginalDefinedInAttrs(ODIAttrs);
}
Expand Down Expand Up @@ -4002,6 +4010,17 @@ void AttributeChecker::checkOriginalDefinedInAttrs(
}
}

void AttributeChecker::checkAvailableAttrs(ArrayRef<AvailableAttr *> Attrs) {
if (Attrs.empty())
return;
// If all available are spi available, we should use @_spi instead.
if (std::all_of(Attrs.begin(), Attrs.end(), [](AvailableAttr *AV) {
return AV->IsSPI;
})) {
diagnose(D->getLoc(), diag::spi_preferred_over_spi_available);
};
}

void AttributeChecker::checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs) {
if (Attrs.empty())
return;
Expand Down
2 changes: 2 additions & 0 deletions test/Sema/spi-available-context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx11.9 -library-level api

@_spi_available(macOS 10.4, *)
@available(iOS 8.0, *)
public protocol Foo { }

@_spi_available(macOS 10.4, *)
@available(iOS 8.0, *)
public class Bar {
public var foo: Foo?
}
2 changes: 2 additions & 0 deletions test/Sema/spi-available-inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx11.9 -library-level api

@_spi_available(macOS 10.4, *)
@available(iOS 8.0, *)
public class MacOSSPIClass { public init() {} }

@_spi_available(iOS 8.0, *)
@available(macOS 10.4, *)
public class iOSSPIClass { public init() {} }

@inlinable public func foo() {
Expand Down
7 changes: 5 additions & 2 deletions test/attr/spi_available.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// RUN: %target-typecheck-verify-swift

@_spi_available(*, deprecated, renamed: "another") // expected-error {{SPI available only supports introducing version on specific platform}}
public class SPIClass1 {}
public class SPIClass1 {} // expected-warning {{symbols that are @_spi_available on all platforms should use @_spi instead}}

@_spi_available(*, unavailable) // expected-error {{SPI available only supports introducing version on specific platform}}
public class SPIClass2 {}
public class SPIClass2 {} // expected-warning {{symbols that are @_spi_available on all platforms should use @_spi instead}}

@_spi_available(AlienPlatform 5.2, *) // expected-warning {{unrecognized platform name 'AlienPlatform'}}
public class SPIClass3 {}

@_spi_available(macOS 10.4, *)
public class SPIClass4 {} // expected-warning {{symbols that are @_spi_available on all platforms should use @_spi instead}}