Skip to content

Commit 7c80976

Browse files
committed
Package name is only printed in private swiftinterface. This causes
ambiguity during lookup when there are multiple public or inlinalbe package decls in public interfaces. This PR adds a package name to public swiftinterface and lets typecheck look up the package name to narrow down the scope of access to package decls from an external module. Resolves rdar://117699160
1 parent 137ba61 commit 7c80976

File tree

8 files changed

+15
-27
lines changed

8 files changed

+15
-27
lines changed

include/swift/AST/Module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class PackageUnit: public DeclContext {
210210
/// Equality check via package name instead of pointer comparison.
211211
/// Returns false if the name is empty.
212212
bool isSamePackageAs(PackageUnit *other) {
213+
if (!other) return false;
213214
return !(getName().empty()) && getName() == other->getName();
214215
}
215216
};

lib/AST/Decl.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4294,13 +4294,8 @@ static bool checkAccessUsingAccessScopes(const DeclContext *useDC,
42944294
VD, access, useDC,
42954295
/*treatUsableFromInlineAsPublic*/ includeInlineable);
42964296
if (accessScope.getDeclContext() == useDC) return true;
4297-
if (!AccessScope(useDC).isChildOf(accessScope)) {
4298-
// Grant access if this VD is an inlinable package decl referenced by
4299-
// another module in an interface file.
4300-
if (VD->skipAccessCheckIfInterface(useDC, access, accessScope))
4301-
return true;
4302-
return false;
4303-
}
4297+
if (!AccessScope(useDC).isChildOf(accessScope)) return false;
4298+
43044299
// useDC is null only when caller wants to skip non-public type checks.
43054300
if (!useDC) return true;
43064301

@@ -4426,14 +4421,6 @@ static bool checkAccess(const DeclContext *useDC, const ValueDecl *VD,
44264421
return useSF && useSF->hasTestableOrPrivateImport(access, sourceModule);
44274422
}
44284423
case AccessLevel::Package: {
4429-
auto srcFile = sourceDC->getParentSourceFile();
4430-
4431-
// srcFile could be null if VD decl is from an imported .swiftmodule
4432-
if (srcFile && srcFile->Kind == SourceFileKind::Interface) {
4433-
// If source file is interface, package decls must be usableFromInline or
4434-
// inlinable, and are accessed only within the defining module so return true
4435-
return true;
4436-
}
44374424
auto srcPkg = sourceDC->getPackageContext(/*lookupIfNotCurrent*/ true);
44384425
auto usePkg = useDC->getPackageContext(/*lookupIfNotCurrent*/ true);
44394426
return srcPkg && usePkg && usePkg->isSamePackageAs(srcPkg);

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
9292
}
9393
}
9494
}
95+
if (!M->getPackageName().empty()) {
96+
StringRef pkgName = M->getPackageName().str();
97+
out << " -package-name " << pkgName;
98+
}
9599
out << "\n";
96100

97101
if (!Opts.IgnorableFlags.empty()) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4435,10 +4435,6 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
44354435
requiredAccessScope.requiredAccessForDiagnostics();
44364436
auto proto = conformance->getProtocol();
44374437
auto protoAccessScope = proto->getFormalAccessScope(DC);
4438-
// Skip diagnostics of a witness of a package protocol that is inlinalbe
4439-
// referenced in an interface file.
4440-
if (proto->skipAccessCheckIfInterface(DC, requiredAccess, protoAccessScope))
4441-
return;
44424438
bool protoForcesAccess =
44434439
requiredAccessScope.hasEqualDeclContextWith(protoAccessScope);
44444440
auto diagKind = protoForcesAccess

test/Sema/accessibility_package_inline_interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// RUN: %target-swift-typecheck-module-from-interface(%t/Utils.swiftinterface) -I%t
1010

1111
// RUN: %FileCheck %s -check-prefix CHECK-UTILS < %t/Utils.swiftinterface
12-
// CHECK-UTILS-NOT: -package-name myLib
1312
// CHECK-UTILS: -module-name Utils
13+
// CHECK-UTILS: -package-name myLib
1414
// CHECK-UTILS: @usableFromInline
1515
// CHECK-UTILS: package class PackageKlassProto {
1616
// CHECK-UTILS: @usableFromInline

test/Sema/accessibility_package_interface.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
// RUN: %target-swift-typecheck-module-from-interface(%t/Utils.swiftinterface) -I %t
1313
// RUN: %FileCheck %s --check-prefix=CHECK-PUBLIC-UTILS < %t/Utils.swiftinterface
1414

15-
// CHECK-PUBLIC-UTILS-NOT: -package-name swift-utils.log
1615
// CHECK-PUBLIC-UTILS-NOT: package func packageFunc()
1716
// CHECK-PUBLIC-UTILS-NOT: package protocol PackageProto
1817
// CHECK-PUBLIC-UTILS-NOT: var pkgVar
1918
// CHECK-PUBLIC-UTILS-NOT: package class PackageKlass
2019
// CHECK-PUBLIC-UTILS-NOT: package var pkgVar
2120
// CHECK-PUBLIC-UTILS: -module-name Utils
21+
// CHECK-PUBLIC-UTILS: -package-name swift-utils.log
2222
// CHECK-PUBLIC-UTILS: public func publicFunc()
2323
// CHECK-PUBLIC-UTILS: @usableFromInline
2424
// CHECK-PUBLIC-UTILS: package func ufiPackageFunc()
@@ -39,7 +39,7 @@
3939
// CHECK-PRIVATE-UTILS-NOT: package class PackageKlass
4040
// CHECK-PRIVATE-UTILS-NOT: package var pkgVar
4141
// CHECK-PRIVATE-UTILS: -module-name Utils
42-
// CHECK-PRIVATE-UTILS: swift-module-flags-ignorable-private: -package-name swift-utils.log
42+
// CHECK-PRIVATE-UTILS: -package-name swift-utils.log
4343
// CHECK-PRIVATE-UTILS: public func publicFunc()
4444
// CHECK-PRIVATE-UTILS: @usableFromInline
4545
// CHECK-PRIVATE-UTILS: package func ufiPackageFunc()
@@ -64,7 +64,7 @@
6464

6565
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t -verify
6666
// RUN: %FileCheck %s --check-prefix=CHECK-PUBLIC-CLIENT < %t/Client.swiftinterface
67-
// CHECK-PUBLIC-CLIENT-NOT: -package-name swift-utils.log
67+
// CHECK-PUBLIC-CLIENT: -package-name swift-utils.log
6868
// CHECK-PUBLIC-CLIENT: @inlinable public func clientFunc()
6969
// CHECK-PUBLIC-CLIENT: publicFunc()
7070
// CHECK-PUBLIC-CLIENT: ufiPackageFunc()

test/Serialization/load_package_module.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
// RUN: %target-swift-typecheck-module-from-interface(%t/LibInterface.swiftinterface) -I %t
2020
// RUN: %FileCheck %s --check-prefix=CHECK-PUBLIC < %t/LibInterface.swiftinterface
2121
// CHECK-PUBLIC: -module-name LibInterface
22-
// CHECK-PUBLIC-NOT: -package-name
22+
// CHECK-PUBLIC: -package-name
2323

2424
// RUN: %target-swift-typecheck-module-from-interface(%t/LibInterface.private.swiftinterface) -module-name LibInterface -I %t
2525
// RUN: %FileCheck %s --check-prefix=CHECK-PRIVATE < %t/LibInterface.private.swiftinterface
26-
// CHECK-PRIVATE: swift-module-flags-ignorable-private: -package-name libPkg
26+
// CHECK-PRIVATE: -package-name libPkg
2727

2828
// RUN: not %target-swift-frontend -typecheck %t/ClientLoadInterface.swift -package-name otherPkg -I %t 2> %t/resultX.output
2929
// RUN: %FileCheck %s -check-prefix CHECK-X < %t/resultX.output

test/Serialization/module_package_name.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
// RUN: %target-swift-typecheck-module-from-interface(%t/Logging.swiftinterface) -I %t
1212
// RUN: %FileCheck %s --check-prefix=CHECK-PUBLIC < %t/Logging.swiftinterface
1313
// CHECK-PUBLIC: -module-name Logging
14-
// CHECK-PUBLIC-NOT: -package-name
14+
// CHECK-PUBLIC: -package-name
1515

1616
// RUN: %target-swift-typecheck-module-from-interface(%t/Logging.private.swiftinterface) -module-name Logging -I %t
1717
// RUN: %FileCheck %s --check-prefix=CHECK-PRIVATE < %t/Logging.private.swiftinterface
1818
// CHECK-PRIVATE: -module-name Logging
19-
// CHECK-PRIVATE: swift-module-flags-ignorable-private: -package-name MyLoggingPkg
19+
// CHECK-PRIVATE: -package-name MyLoggingPkg
2020

2121
//--- File.swift
2222
public func log(level: Int) {}

0 commit comments

Comments
 (0)