Skip to content

[5.9] [Macros] Use outermost source file for establishing (file)private access scope #68058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4032,7 +4032,7 @@ getAccessScopeForFormalAccess(const ValueDecl *VD,
case AccessLevel::Package: {
auto pkg = resultDC->getPackageContext(/*lookupIfNotCurrent*/ true);
if (!pkg) {
auto srcFile = resultDC->getParentSourceFile();
auto srcFile = resultDC->getOutermostParentSourceFile();
// Check if the file containing package decls is an interface file; if an
// interface file contains package decls, they must be usableFromInline or
// inlinable and are accessed within the defining module, so package-name
Expand Down
12 changes: 6 additions & 6 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ DeclContext *DeclContext::getModuleScopeContext() const {

void DeclContext::getSeparatelyImportedOverlays(
ModuleDecl *declaring, SmallVectorImpl<ModuleDecl *> &overlays) const {
if (auto SF = getParentSourceFile())
if (auto SF = getOutermostParentSourceFile())
SF->getSeparatelyImportedOverlays(declaring, overlays);
}

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

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

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

// If there's no last extension, return the supplied context.
Expand All @@ -1269,7 +1269,7 @@ getPrivateDeclContext(const DeclContext *DC, const SourceFile *useSF) {
AccessScope::AccessScope(const DeclContext *DC, bool isPrivate)
: Value(DC, isPrivate) {
if (isPrivate) {
DC = getPrivateDeclContext(DC, DC->getParentSourceFile());
DC = getPrivateDeclContext(DC, DC->getOutermostParentSourceFile());
Value.setPointer(DC);
}
if (!DC || isa<ModuleDecl>(DC) || isa<PackageUnit>(DC))
Expand Down Expand Up @@ -1319,8 +1319,8 @@ bool AccessScope::allowsPrivateAccess(const DeclContext *useDC, const DeclContex
}
}
// Do not allow access if the sourceDC is in a different file
auto useSF = useDC->getParentSourceFile();
if (useSF != sourceDC->getParentSourceFile())
auto useSF = useDC->getOutermostParentSourceFile();
if (useSF != sourceDC->getOutermostParentSourceFile())
return false;

// Do not allow access if the sourceDC does not represent a type.
Expand Down
9 changes: 9 additions & 0 deletions test/Macros/macro_expand_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// RUN: %empty-directory(%t)
// 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

// Check for errors first
// 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

// 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
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
// 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
Expand Down Expand Up @@ -202,6 +205,12 @@ func testHasPropertyWrappers(hpw: HasPropertyWrappers) {
requiresEquatable(hpw)
}

@Equatable
struct HasPrivateMembers {
@NotEquatable
private var value: Int = 0
}

// Check that conformances implied by a macro-defined conformance are serialized
// without issue.
public protocol ImpliesHashable: Hashable { }
Expand Down