Skip to content

Commit 0dce9b7

Browse files
author
Brian King
committed
Store the private scope context in the AccessScope constructor to fix equality support
1 parent 0a1d52e commit 0dce9b7

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

lib/AST/DeclContext.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -947,29 +947,6 @@ IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
947947
llvm_unreachable("Unhandled DeclKind in switch.");
948948
}
949949

950-
AccessScope::AccessScope(const DeclContext *DC, bool isPrivate)
951-
: Value(DC, isPrivate) {
952-
if (!DC || isa<ModuleDecl>(DC))
953-
assert(!isPrivate && "public or internal scope can't be private");
954-
}
955-
956-
bool AccessScope::isFileScope() const {
957-
auto DC = getDeclContext();
958-
return DC && isa<FileUnit>(DC);
959-
}
960-
961-
Accessibility AccessScope::accessibilityForDiagnostics() const {
962-
if (isPublic())
963-
return Accessibility::Public;
964-
if (isa<ModuleDecl>(getDeclContext()))
965-
return Accessibility::Internal;
966-
if (getDeclContext()->isModuleScopeContext()) {
967-
return isPrivate() ? Accessibility::Private : Accessibility::FilePrivate;
968-
}
969-
970-
return Accessibility::Private;
971-
}
972-
973950
/// Return the DeclContext to compare when checking private access in
974951
/// Swift 4 mode. The context returned is the type declaration if the context
975952
/// and the type declaration are in the same file, otherwise it is the types
@@ -996,6 +973,33 @@ getPrivateDeclContext(const DeclContext *DC, const SourceFile *useSF) {
996973
return lastExtension ? lastExtension : DC;
997974
}
998975

976+
AccessScope::AccessScope(const DeclContext *DC, bool isPrivate)
977+
: Value(DC, isPrivate) {
978+
if (isPrivate) {
979+
DC = getPrivateDeclContext(DC, DC->getParentSourceFile());
980+
Value.setPointer(DC);
981+
}
982+
if (!DC || isa<ModuleDecl>(DC))
983+
assert(!isPrivate && "public or internal scope can't be private");
984+
}
985+
986+
bool AccessScope::isFileScope() const {
987+
auto DC = getDeclContext();
988+
return DC && isa<FileUnit>(DC);
989+
}
990+
991+
Accessibility AccessScope::accessibilityForDiagnostics() const {
992+
if (isPublic())
993+
return Accessibility::Public;
994+
if (isa<ModuleDecl>(getDeclContext()))
995+
return Accessibility::Internal;
996+
if (getDeclContext()->isModuleScopeContext()) {
997+
return isPrivate() ? Accessibility::Private : Accessibility::FilePrivate;
998+
}
999+
1000+
return Accessibility::Private;
1001+
}
1002+
9991003
bool AccessScope::allowsPrivateAccess(const DeclContext *useDC, const DeclContext *sourceDC) {
10001004
// Check the lexical scope.
10011005
if (useDC->isChildContextOf(sourceDC))
@@ -1005,11 +1009,14 @@ bool AccessScope::allowsPrivateAccess(const DeclContext *useDC, const DeclContex
10051009
if (useDC->getASTContext().isSwiftVersion3())
10061010
return false;
10071011

1008-
// Do not allow access if the sourceDC is in a different file, or if the
1009-
// sourceDC does not represent a type.
1010-
auto sourceNTD = sourceDC->getAsNominalTypeOrNominalTypeExtensionContext();
1012+
// Do not allow access if the sourceDC is in a different file
10111013
auto useSF = useDC->getParentSourceFile();
1012-
if (useSF != sourceDC->getParentSourceFile() || !sourceNTD)
1014+
if (useSF != sourceDC->getParentSourceFile())
1015+
return false;
1016+
1017+
// Do not allow access if the sourceDC does not represent a type.
1018+
auto sourceNTD = sourceDC->getAsNominalTypeOrNominalTypeExtensionContext();
1019+
if (!sourceNTD)
10131020
return false;
10141021

10151022
// Compare the private scopes and iterate over the parent types.

0 commit comments

Comments
 (0)