Skip to content

Commit 89813e4

Browse files
committed
[Macros] Use outermost source file for establishing (file)private access scope
Fixes rdar://114048069.
1 parent ca1b731 commit 89813e4

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4032,7 +4032,7 @@ getAccessScopeForFormalAccess(const ValueDecl *VD,
40324032
case AccessLevel::Package: {
40334033
auto pkg = resultDC->getPackageContext(/*lookupIfNotCurrent*/ true);
40344034
if (!pkg) {
4035-
auto srcFile = resultDC->getParentSourceFile();
4035+
auto srcFile = resultDC->getOutermostParentSourceFile();
40364036
// Check if the file containing package decls is an interface file; if an
40374037
// interface file contains package decls, they must be usableFromInline or
40384038
// inlinable and are accessed within the defining module, so package-name

lib/AST/DeclContext.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ DeclContext *DeclContext::getModuleScopeContext() const {
407407

408408
void DeclContext::getSeparatelyImportedOverlays(
409409
ModuleDecl *declaring, SmallVectorImpl<ModuleDecl *> &overlays) const {
410-
if (auto SF = getParentSourceFile())
410+
if (auto SF = getOutermostParentSourceFile())
411411
SF->getSeparatelyImportedOverlays(declaring, overlays);
412412
}
413413

@@ -1253,13 +1253,13 @@ getPrivateDeclContext(const DeclContext *DC, const SourceFile *useSF) {
12531253

12541254
// use the type declaration as the private scope if it is in the same
12551255
// file as useSF. This occurs for both extensions and declarations.
1256-
if (NTD->getParentSourceFile() == useSF)
1256+
if (NTD->getOutermostParentSourceFile() == useSF)
12571257
return NTD;
12581258

12591259
// Otherwise use the last extension declaration in the same file.
12601260
const DeclContext *lastExtension = nullptr;
12611261
for (ExtensionDecl *ED : NTD->getExtensions())
1262-
if (ED->getParentSourceFile() == useSF)
1262+
if (ED->getOutermostParentSourceFile() == useSF)
12631263
lastExtension = ED;
12641264

12651265
// If there's no last extension, return the supplied context.
@@ -1269,7 +1269,7 @@ getPrivateDeclContext(const DeclContext *DC, const SourceFile *useSF) {
12691269
AccessScope::AccessScope(const DeclContext *DC, bool isPrivate)
12701270
: Value(DC, isPrivate) {
12711271
if (isPrivate) {
1272-
DC = getPrivateDeclContext(DC, DC->getParentSourceFile());
1272+
DC = getPrivateDeclContext(DC, DC->getOutermostParentSourceFile());
12731273
Value.setPointer(DC);
12741274
}
12751275
if (!DC || isa<ModuleDecl>(DC) || isa<PackageUnit>(DC))
@@ -1319,8 +1319,8 @@ bool AccessScope::allowsPrivateAccess(const DeclContext *useDC, const DeclContex
13191319
}
13201320
}
13211321
// Do not allow access if the sourceDC is in a different file
1322-
auto useSF = useDC->getParentSourceFile();
1323-
if (useSF != sourceDC->getParentSourceFile())
1322+
auto useSF = useDC->getOutermostParentSourceFile();
1323+
if (useSF != sourceDC->getOutermostParentSourceFile())
13241324
return false;
13251325

13261326
// Do not allow access if the sourceDC does not represent a type.

test/Macros/macro_expand_extensions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: %empty-directory(%t)
44
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
55

6+
// Check for errors first
7+
// RUN: %target-swift-frontend -enable-experimental-feature ExtensionMacros -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) %s -I %t -disable-availability-checking
8+
69
// RUN: %target-swift-frontend -enable-experimental-feature ExtensionMacros -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) %s -I %t -disable-availability-checking -dump-macro-expansions > %t/expansions-dump.txt 2>&1
710
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
811
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ExtensionMacros -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 -I %t
@@ -202,6 +205,12 @@ func testHasPropertyWrappers(hpw: HasPropertyWrappers) {
202205
requiresEquatable(hpw)
203206
}
204207

208+
@Equatable
209+
struct HasPrivateMembers {
210+
@NotEquatable
211+
private var value: Int = 0
212+
}
213+
205214
// Check that conformances implied by a macro-defined conformance are serialized
206215
// without issue.
207216
public protocol ImpliesHashable: Hashable { }

0 commit comments

Comments
 (0)