Skip to content

Commit 093f0a6

Browse files
committed
[Sema] An SPI protocol requirement must have a default implementation
1 parent ae293ab commit 093f0a6

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,10 @@ ERROR(spi_attribute_on_non_public,none,
17051705
"cannot be declared '@_spi' because only public and open "
17061706
"declarations can be '@_spi'",
17071707
(AccessLevel, DescriptiveDeclKind))
1708+
ERROR(spi_attribute_on_protocol_requirement,none,
1709+
"protocol requirement %0 cannot be declared '@_spi' without "
1710+
"a default implementation in a protocol extension",
1711+
(DeclName))
17081712

17091713
// Opaque return types
17101714
ERROR(opaque_type_invalid_constraint,none,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,13 +862,52 @@ void AttributeChecker::visitSetterAccessAttr(
862862

863863
void AttributeChecker::visitSPIAccessControlAttr(SPIAccessControlAttr *attr) {
864864
if (auto VD = dyn_cast<ValueDecl>(D)) {
865+
// VD must be public or open to use an @_spi attribute.
865866
auto declAccess = VD->getFormalAccess();
866867
if (declAccess < AccessLevel::Public) {
867868
diagnoseAndRemoveAttr(attr,
868869
diag::spi_attribute_on_non_public,
869870
declAccess,
870871
D->getDescriptiveKind());
871872
}
873+
874+
// If VD is a public protocol requirement it can be SPI only if there's
875+
// a default implementation.
876+
if (auto protocol = dyn_cast<ProtocolDecl>(D->getDeclContext())) {
877+
auto implementations = TypeChecker::lookupMember(
878+
D->getDeclContext(),
879+
protocol->getDeclaredType(),
880+
VD->createNameRef(),
881+
NameLookupFlags::ProtocolMembers);
882+
bool hasDefaultImplementation = llvm::any_of(implementations,
883+
[&](const LookupResultEntry &entry) {
884+
auto entryDecl = entry.getValueDecl();
885+
auto DC = entryDecl->getDeclContext();
886+
auto extension = dyn_cast<ExtensionDecl>(DC);
887+
888+
// The implementation must be defined in the same module in
889+
// an unconstrained extension.
890+
if (!extension ||
891+
extension->getParentModule() != protocol->getParentModule() ||
892+
extension->isConstrainedExtension())
893+
return false;
894+
895+
// For computed properties and subscripts, check that the default
896+
// implementation defines `set` if the protocol declares it.
897+
if (auto protoStorage = dyn_cast<AbstractStorageDecl>(VD))
898+
if (auto entryStorage = dyn_cast<AbstractStorageDecl>(entryDecl))
899+
if (protoStorage->getAccessor(AccessorKind::Set) &&
900+
!entryStorage->getAccessor(AccessorKind::Set))
901+
return false;
902+
903+
return true;
904+
});
905+
906+
if (!hasDefaultImplementation)
907+
diagnoseAndRemoveAttr(attr,
908+
diag::spi_attribute_on_protocol_requirement,
909+
VD->getName());
910+
}
872911
}
873912
}
874913

test/SPI/protocol_requirement.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Test limitations on SPI protocol requirements.
2+
3+
// RUN: %target-typecheck-verify-swift
4+
5+
// Reject SPI protocol requirements without a default implementation.
6+
public protocol PublicProtoRejected {
7+
@_spi(Private) // expected-error{{protocol requirement 'reqWithoutDefault()' cannot be declared '@_spi' without a default implementation in a protocol extension}}
8+
func reqWithoutDefault()
9+
10+
@_spi(Private) // expected-error{{protocol requirement 'property' cannot be declared '@_spi' without a default implementation in a protocol extension}}
11+
var property: Int { get set }
12+
13+
@_spi(Private) // expected-error{{protocol requirement 'propertyWithoutSetter' cannot be declared '@_spi' without a default implementation in a protocol extension}}
14+
var propertyWithoutSetter: Int { get set }
15+
16+
@_spi(Private) // expected-error{{protocol requirement 'subscript(_:)' cannot be declared '@_spi' without a default implementation in a protocol extension}}
17+
subscript(index: Int) -> Int { get set }
18+
19+
@_spi(Private) // expected-error{{protocol requirement 'init()' cannot be declared '@_spi' without a default implementation in a protocol extension}}
20+
init()
21+
22+
@_spi(Private) // expected-error{{'@_spi' attribute cannot be applied to this declaration}}
23+
associatedtype T
24+
}
25+
26+
extension PublicProtoRejected {
27+
@_spi(Private)
28+
public var propertyWithoutSetter: Int { get { return 42 } }
29+
}
30+
31+
extension PublicProtoRejected where Self : Equatable {
32+
@_spi(Private)
33+
public func reqWithoutDefault() {
34+
// constrainted implementation
35+
}
36+
}
37+
38+
// Accept SPI protocol requirements with an implementation.
39+
public protocol PublicProto {
40+
@_spi(Private)
41+
func reqWithDefaultImplementation()
42+
43+
@_spi(Private)
44+
var property: Int { get set }
45+
46+
@_spi(Private)
47+
subscript(index: Int) -> Int { get set }
48+
49+
@_spi(Private)
50+
init()
51+
}
52+
53+
extension PublicProto {
54+
@_spi(Private)
55+
public func reqWithDefaultImplementation() { }
56+
57+
@_spi(Private)
58+
public var property: Int {
59+
get { return 42 }
60+
set { }
61+
}
62+
63+
@_spi(Private)
64+
public subscript(index: Int) -> Int {
65+
get { return 42 }
66+
set { }
67+
}
68+
69+
@_spi(Private)
70+
public init() { }
71+
}

0 commit comments

Comments
 (0)