Skip to content

Commit b4e476e

Browse files
committed
Check access control for the generic requirements of subscripts
Oops.
1 parent 3a4dbd6 commit b4e476e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,16 @@ void AccessControlCheckerBase::checkGenericParamAccess(
365365
if (minAccessScope.isPublic())
366366
return;
367367

368+
// FIXME: Promote this to an error in the next -swift-version break.
369+
if (isa<SubscriptDecl>(owner))
370+
downgradeToWarning = DowngradeToWarning::Yes;
371+
368372
if (checkUsableFromInline) {
369-
auto diagID = diag::generic_param_usable_from_inline;
370373
if (!TC.Context.isSwiftVersionAtLeast(5))
374+
downgradeToWarning = DowngradeToWarning::Yes;
375+
376+
auto diagID = diag::generic_param_usable_from_inline;
377+
if (downgradeToWarning == DowngradeToWarning::Yes)
371378
diagID = diag::generic_param_usable_from_inline_warn;
372379
auto diag = TC.diagnose(owner,
373380
diagID,
@@ -802,6 +809,8 @@ class AccessControlChecker : public AccessControlCheckerBase,
802809
}
803810

804811
void visitSubscriptDecl(SubscriptDecl *SD) {
812+
checkGenericParamAccess(SD->getGenericParams(), SD);
813+
805814
auto minAccessScope = AccessScope::getPublic();
806815
const TypeRepr *complainRepr = nullptr;
807816
auto downgradeToWarning = DowngradeToWarning::No;
@@ -1294,6 +1303,8 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
12941303
}
12951304

12961305
void visitSubscriptDecl(SubscriptDecl *SD) {
1306+
checkGenericParamAccess(SD->getGenericParams(), SD);
1307+
12971308
for (auto &P : *SD->getIndices()) {
12981309
checkTypeAccess(P->getTypeLoc(), SD, /*mayBeInferred*/false,
12991310
[&](AccessScope typeAccessScope,

test/Sema/accessibility.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,3 +869,10 @@ public extension ObjCSub {
869869
set {}
870870
}
871871
}
872+
873+
public struct TestGenericSubscripts {
874+
// We'd like these to be errors in a future version of Swift, but they weren't
875+
// in Swift 5.0.
876+
public subscript<T: PrivateProto>(_: T) -> Int { return 0 } // expected-warning {{subscript should not be declared public because its generic parameter uses a private type}} {{none}}
877+
public subscript<T>(where _: T) -> Int where T: PrivateProto { return 0 } // expected-warning {{subscript should not be declared public because its generic requirement uses a private type}} {{none}}
878+
}

test/attr/attr_usableFromInline.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal struct InternalStruct {}
8787
// expected-error@-1 {{type referenced from the underlying type of a '@usableFromInline' type alias must be '@usableFromInline' or public}}
8888

8989
protocol InternalProtocol {
90-
// expected-note@-1 4{{type declared here}}
90+
// expected-note@-1 * {{type declared here}}
9191
associatedtype T
9292
}
9393

@@ -147,3 +147,8 @@ enum BadEnum {
147147
@usableFromInline
148148
class BadClass : InternalClass {}
149149
// expected-error@-1 {{type referenced from the superclass of a '@usableFromInline' class must be '@usableFromInline' or public}}
150+
151+
public struct TestGenericSubscripts {
152+
@usableFromInline subscript<T: InternalProtocol>(_: T) -> Int { return 0 } // expected-warning {{type referenced from a generic parameter of a '@usableFromInline' subscript should be '@usableFromInline' or public}}
153+
@usableFromInline subscript<T>(where _: T) -> Int where T: InternalProtocol { return 0 } // expected-warning {{type referenced from a generic requirement of a '@usableFromInline' subscript should be '@usableFromInline' or public}}
154+
}

0 commit comments

Comments
 (0)