Skip to content

Commit 5426e0d

Browse files
committed
[AST] Decl::is*AccessibleFrom methods should respect access control flag
Otherwise integrated REPL and other tools like LLDB would produce incorrect results or crash. Resolves: rdar://problem/30933988
1 parent 9c04037 commit 5426e0d

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

lib/AST/Decl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,12 @@ ValueDecl::getFormalAccessScope(const DeclContext *useDC,
27502750
treatUsableFromInlineAsPublic);
27512751
}
27522752

2753+
/// Check whether it's important to respect access control restrictions
2754+
/// in current context.
2755+
static bool isAccessControlDisabled(const ASTContext &ctx) {
2756+
return !ctx.LangOpts.EnableAccessControl;
2757+
}
2758+
27532759
/// Checks if \p VD may be used from \p useDC, taking \@testable imports into
27542760
/// account.
27552761
///
@@ -2760,6 +2766,9 @@ ValueDecl::getFormalAccessScope(const DeclContext *useDC,
27602766
static bool checkAccessUsingAccessScopes(const DeclContext *useDC,
27612767
const ValueDecl *VD,
27622768
AccessLevel access) {
2769+
if (isAccessControlDisabled(VD->getASTContext()))
2770+
return true;
2771+
27632772
AccessScope accessScope =
27642773
getAccessScopeForFormalAccess(VD, access, useDC,
27652774
/*treatUsableFromInlineAsPublic*/false);
@@ -2779,6 +2788,9 @@ static bool checkAccessUsingAccessScopes(const DeclContext *useDC,
27792788
/// See ValueDecl::isAccessibleFrom for a description of \p forConformance.
27802789
static bool checkAccess(const DeclContext *useDC, const ValueDecl *VD,
27812790
AccessLevel access, bool forConformance) {
2791+
if (isAccessControlDisabled(VD->getASTContext()))
2792+
return true;
2793+
27822794
auto *sourceDC = VD->getDeclContext();
27832795

27842796
if (!forConformance) {

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6791,9 +6791,8 @@ static Type adjustSelfTypeForMember(Type baseTy, ValueDecl *member,
67916791
// If the base of the access is mutable, then we may be invoking a getter or
67926792
// setter that requires the base to be mutable.
67936793
auto *SD = cast<AbstractStorageDecl>(member);
6794-
bool isSettableFromHere = SD->isSettable(UseDC)
6795-
&& (!UseDC->getASTContext().LangOpts.EnableAccessControl
6796-
|| SD->isSetterAccessibleFrom(UseDC));
6794+
bool isSettableFromHere =
6795+
SD->isSettable(UseDC) && SD->isSetterAccessibleFrom(UseDC);
67976796

67986797
// If neither the property's getter nor its setter are mutating, the base
67996798
// can be an rvalue.

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,7 @@ static bool doesStorageProduceLValue(TypeChecker &TC,
732732
if (!storage->isSettable(useDC, base))
733733
return false;
734734

735-
if (TC.Context.LangOpts.EnableAccessControl &&
736-
!storage->isSetterAccessibleFrom(useDC))
735+
if (!storage->isSetterAccessibleFrom(useDC))
737736
return false;
738737

739738
// If there is no base, or if the base isn't being used, it is settable.

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ static bool isDeclVisibleInLookupMode(ValueDecl *Member, LookupState LS,
129129

130130
// Check access when relevant.
131131
if (!Member->getDeclContext()->isLocalContext() &&
132-
!isa<GenericTypeParamDecl>(Member) && !isa<ParamDecl>(Member) &&
133-
FromContext->getASTContext().LangOpts.EnableAccessControl) {
132+
!isa<GenericTypeParamDecl>(Member) && !isa<ParamDecl>(Member)) {
134133
if (!Member->isAccessibleFrom(FromContext))
135134
return false;
136135
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E<T> {
2+
case foo
3+
case bar(T)
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -o %t/rdar30933988_enum.swiftmodule %S/Inputs/rdar30933988_enum.swift
3+
// RUN: %target-typecheck-verify-swift -I %t -disable-access-control
4+
5+
import rdar30933988_enum
6+
7+
let _: E = .foo
8+
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
9+
let _: E<Int> = .foo // Ok
10+
let _: E = .bar(42) // Ok
11+
let _: E<String> = .bar(42)
12+
// expected-error@-1 {{member 'bar' in 'E<String>' produces result of type 'E<T>', but context expects 'E<String>'}}
13+
let _: E<Int> = .bar(42) // Ok

0 commit comments

Comments
 (0)