Skip to content

Commit fef3a37

Browse files
committed
Use access scopes instead of adjustAccessLevelForProtocolExtension
This gets adjustAccessLevelForProtocolExtension, a hack of sorts to begin with, out of ValueDecl's general API, and down to a helper for isAccessibleFrom and isSetterAccessibleFrom. (The only reason these two don't go through access scopes is as an optimization.)
1 parent ca14d6c commit fef3a37

File tree

8 files changed

+45
-26
lines changed

8 files changed

+45
-26
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,18 +2356,6 @@ class ValueDecl : public Decl {
23562356
/// \sa hasOpenAccess
23572357
AccessLevel getFormalAccess() const;
23582358

2359-
/// If this declaration is a member of a protocol extension, return the
2360-
/// minimum of the given access level and the protocol's access level.
2361-
///
2362-
/// Otherwise, return the given access level unmodified.
2363-
///
2364-
/// This is used when checking name lookup visibility. Protocol extension
2365-
/// members can be found when performing name lookup on a concrete type;
2366-
/// if the concrete type is visible from the lookup context but the
2367-
/// protocol is not, we do not want the protocol extension members to be
2368-
/// visible.
2369-
AccessLevel adjustAccessLevelForProtocolExtension(AccessLevel access) const;
2370-
23712359
/// Determine whether this Decl has either Private or FilePrivate access,
23722360
/// and its DeclContext does not.
23732361
bool isOutermostPrivateOrFilePrivateScope() const;

lib/AST/NameLookup.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,9 +1520,9 @@ void ClassDecl::recordObjCMethod(AbstractFunctionDecl *method) {
15201520
vec.push_back(method);
15211521
}
15221522

1523-
AccessLevel
1524-
ValueDecl::adjustAccessLevelForProtocolExtension(AccessLevel access) const {
1525-
if (auto *ext = dyn_cast<ExtensionDecl>(getDeclContext())) {
1523+
static AccessLevel
1524+
adjustAccessLevelForProtocolExtension(const ValueDecl *VD, AccessLevel access) {
1525+
if (auto *ext = dyn_cast<ExtensionDecl>(VD->getDeclContext())) {
15261526
if (auto *protocol = ext->getAsProtocolOrProtocolExtensionContext()) {
15271527
// Note: it gets worse. The standard library has public methods
15281528
// in protocol extensions of a @usableFromInline internal protocol,
@@ -1549,7 +1549,7 @@ static bool checkAccess(const DeclContext *useDC, const ValueDecl *VD,
15491549
auto *sourceDC = VD->getDeclContext();
15501550

15511551
if (!forConformance)
1552-
access = VD->adjustAccessLevelForProtocolExtension(access);
1552+
access = adjustAccessLevelForProtocolExtension(VD, access);
15531553

15541554
switch (access) {
15551555
case AccessLevel::Private:

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,8 +1662,7 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
16621662
// clamp the formal access for diagnostics purposes to the formal access
16631663
// of the protocol itself.
16641664
diagnose(nameLoc, diag::candidate_inaccessible, decl->getBaseName(),
1665-
decl->adjustAccessLevelForProtocolExtension(
1666-
decl->getFormalAccess()));
1665+
decl->getFormalAccessScope().accessLevelForDiagnostics());
16671666
for (auto cand : result.UnviableCandidates)
16681667
diagnose(cand.first.getDecl(), diag::decl_declared_here, memberName);
16691668

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
540540
// FIXME: What if the unviable candidates have different levels of access?
541541
const ValueDecl *first = inaccessibleResults.front().getValueDecl();
542542
diagnose(Loc, diag::candidate_inaccessible, Name,
543-
first->adjustAccessLevelForProtocolExtension(
544-
first->getFormalAccess()));
543+
first->getFormalAccessScope().accessLevelForDiagnostics());
545544

546545
// FIXME: If any of the candidates (usually just one) are in the same
547546
// module we could offer a fix-it.

test/Sema/Inputs/accessibility_multi_other.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct Members {
88
}
99
}
1010

11-
struct PrivateConformance : PrivateProtocol {}
11+
struct PrivateConformance : PrivateProtocol, FilePrivateProtocol {}
1212

1313
private protocol PrivateProtocol {}
1414

@@ -19,3 +19,13 @@ extension PrivateProtocol {
1919
internal func internalExtensionMember() {}
2020
// expected-note@-1 {{'internalExtensionMember' declared here}}
2121
}
22+
23+
fileprivate protocol FilePrivateProtocol {}
24+
25+
extension FilePrivateProtocol {
26+
public func publicFPExtensionMember() {}
27+
// expected-note@-1 {{'publicFPExtensionMember' declared here}}
28+
29+
internal func internalFPExtensionMember() {}
30+
// expected-note@-1 {{'internalFPExtensionMember' declared here}}
31+
}

test/Sema/Inputs/accessibility_multi_other_module.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public struct PrivateConformance : PrivateProtocol {}
1+
public struct PrivateConformance : PrivateProtocol, FilePrivateProtocol {}
22

33
private protocol PrivateProtocol {}
44

@@ -8,6 +8,17 @@ extension PrivateProtocol {
88
internal func internalExtensionMember() {}
99
}
1010

11+
fileprivate protocol FilePrivateProtocol {}
12+
13+
extension FilePrivateProtocol {
14+
public func publicFPExtensionMember() {}
15+
// expected-note@-1 {{'publicFPExtensionMember' declared here}}
16+
17+
internal func internalFPExtensionMember() {}
18+
// expected-note@-1 {{'internalFPExtensionMember' declared here}}
19+
}
20+
21+
1122
public struct InternalConformance : InternalProtocol {}
1223

1324
internal protocol InternalProtocol {}

test/Sema/accessibility_multi.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ func testSubscript(_ instance: Members) {
2424

2525
func testPrivateConformance(_ instance: PrivateConformance) {
2626
instance.publicExtensionMember()
27-
// expected-error@-1 {{'publicExtensionMember' is inaccessible due to 'fileprivate' protection level}}
27+
// expected-error@-1 {{'publicExtensionMember' is inaccessible due to 'private' protection level}}
2828

2929
instance.internalExtensionMember()
30-
// expected-error@-1 {{'internalExtensionMember' is inaccessible due to 'fileprivate' protection level}}
30+
// expected-error@-1 {{'internalExtensionMember' is inaccessible due to 'private' protection level}}
31+
32+
instance.publicFPExtensionMember()
33+
// expected-error@-1 {{'publicFPExtensionMember' is inaccessible due to 'fileprivate' protection level}}
34+
35+
instance.internalFPExtensionMember()
36+
// expected-error@-1 {{'internalFPExtensionMember' is inaccessible due to 'fileprivate' protection level}}
3137
}

test/Sema/accessibility_multi_module.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import accessibility_multi_other_module
66

77
func testPrivateConformance(_ instance: PrivateConformance) {
88
instance.publicExtensionMember()
9-
// expected-error@-1 {{'publicExtensionMember' is inaccessible due to 'fileprivate' protection level}}
9+
// expected-error@-1 {{'publicExtensionMember' is inaccessible due to 'private' protection level}}
1010

1111
instance.internalExtensionMember()
12-
// expected-error@-1 {{'internalExtensionMember' is inaccessible due to 'fileprivate' protection level}}
12+
// expected-error@-1 {{'internalExtensionMember' is inaccessible due to 'private' protection level}}
13+
14+
instance.publicFPExtensionMember()
15+
// expected-error@-1 {{'publicFPExtensionMember' is inaccessible due to 'fileprivate' protection level}}
16+
17+
instance.internalFPExtensionMember()
18+
// expected-error@-1 {{'internalFPExtensionMember' is inaccessible due to 'fileprivate' protection level}}
1319
}
1420

1521
func testInternalConformance(_ instance: InternalConformance) {

0 commit comments

Comments
 (0)