Skip to content

Commit 584919e

Browse files
authored
Merge pull request #64373 from apple/es-tests
Update package name input and access scope check
2 parents 31389f7 + 0fe0d6d commit 584919e

18 files changed

+655
-68
lines changed

include/swift/AST/AccessScope.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,12 @@ class AccessScope {
107107
/// \see AccessScope::checkAccessUsingAccessScope
108108
/// \see DeclContext::ASTHierarchy
109109
bool isChildOf(AccessScope AS) const {
110-
if (isPackage()) { // This needs to be checked first before isInContext
111-
return AS.isPublic();
112-
} else if (isInContext()) {
110+
if (isInContext()) {
113111
if (AS.isInContext())
114112
return allowsPrivateAccess(getDeclContext(), AS.getDeclContext());
115113
else
116114
return AS.isPublic();
117-
} else { // It's public, so can't be a child of the argument scope
115+
} else { // It's public, so can't be a child of public or less argument scope
118116
return false;
119117
}
120118
}

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ ERROR(error_bad_export_as_name,none,
192192
ERROR(error_bad_package_name,none,
193193
"package name \"%0\" is not a valid identifier",
194194
(StringRef))
195-
ERROR(error_stdlib_package_name,none,
196-
"package name \"%0\" is reserved for the standard library",
197-
(StringRef))
198195
ERROR(error_stdlib_not_found,Fatal,
199196
"unable to load standard library for target '%0'", (StringRef))
200197
ERROR(error_module_alias_invalid_format,none,

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,8 +1681,9 @@ ERROR(access_control_open_bad_decl,none,
16811681
WARNING(access_control_non_objc_open_member,none,
16821682
"non-'@objc' %0 in extensions cannot be overridden; use 'public' instead",
16831683
(DescriptiveDeclKind))
1684-
ERROR(access_control_requires_package_name, none, "decl has a package access level but no -package-name was passed", ())
1685-
1684+
ERROR(access_control_requires_package_name, none,
1685+
"decl has a package access level but no -package-name was passed",
1686+
())
16861687
ERROR(invalid_decl_attribute,none,
16871688
"'%0' attribute cannot be applied to this declaration", (DeclAttribute))
16881689
ERROR(attr_invalid_on_decl_kind,none,

include/swift/AST/Module.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,27 @@ class OverlayFile;
160160
class ModuleSourceFileLocationMap;
161161

162162
/// A unit that allows grouping of modules by a package name.
163-
/// It's set as a property in ModuleDecl, instead of as its parent decl context,
164-
/// since otherwise it will break the existing decl context lookups that assume
165-
/// ModuleDecl as the top level context. See \c ModuleDecl
163+
///
164+
/// PackageUnit is treated as an enclosing scope of ModuleDecl. Unlike other
165+
/// DeclContext subclasses where a parent context is set in ctor, PackageUnit
166+
/// (parent context) is set as a field in ModuleDecl (child context). It also has a
167+
/// pointer back to the ModuleDecl, so that it can be used to return the module
168+
/// in the existing DeclContext lookup functions, which assume ModuleDecl as
169+
/// the top level context. Since both PackageUnit and ModuleDecl are created
170+
/// in the ASTContext memory arena, i.e. they will be destroyed when the
171+
/// ASTContext is destroyed, both pointng to each other is not considered risky.
172+
///
173+
/// See \c ModuleDecl
166174
class PackageUnit: public DeclContext {
167175
/// Identifies this package and used for the equality check
168176
Identifier PackageName;
169-
/// Weakly references ModuleDecl that points to this package.
177+
/// Non-null reference to ModuleDecl that points to this package.
170178
/// Instead of having multiple ModuleDecls pointing to one PackageUnit, we
171-
/// create and set one PackageUnit per ModuleDecl, to make it easier to look
172-
/// up the module pointing to this package; this look up is needed in existing
173-
/// DeclContext functions, e.g. \c DeclContext::getModuleScopeContext,
174-
/// and \c DeclContext::getParentModule
179+
/// create one PackageUnit per ModuleDecl, to make it easier to look up the
180+
/// module pointing to this package context, which is needed in the existing
181+
/// DeclContext look up functions.
182+
/// \see DeclContext::getModuleScopeContext
183+
/// \see DeclContext::getParentModule
175184
ModuleDecl &SourceModule;
176185

177186
PackageUnit(Identifier name, ModuleDecl &src)
@@ -219,7 +228,7 @@ class ModuleDecl
219228

220229
/// A package this module belongs to. It's set as a property instead of a
221230
/// parent decl context; otherwise it will break the existing decl context
222-
/// lookups that assume ModuleDecl as the top level context.
231+
/// lookup functions that assume ModuleDecl as the top level context.
223232
PackageUnit *Package = nullptr;
224233

225234
/// Module name to use when referenced in clients module interfaces.

lib/AST/Decl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3982,12 +3982,11 @@ ValueDecl::getFormalAccessScope(const DeclContext *useDC,
39823982
/// (useDC) and the decl (VD) site, and returns true in this case, since
39833983
/// FileUnit is a child of nullptr based on the DeclContext hierarchy. The
39843984
/// hierarchy is created when subclasses of DeclContext such as FileUnit or
3985-
/// ModuleDecl are constructed. For example, FileUnit ctor takes ModuleDecl as
3986-
/// its parent DeclContext. There's an exception, however; the parent of
3987-
/// ModuleDecl is nullptr, not set to PackageUnit; ModuleDecl has a pointer to
3988-
/// PackageUnit as its field, and it is treated as the enclosing scope of
3989-
/// ModuleDecl in the `isChildOf` call.
3990-
///
3985+
/// ModuleDecl are constructed. For example, a top ClassDecl ctor takes FileUnit
3986+
/// as its parent DeclContext and FileUnit ctor takes ModuleDecl as its parent
3987+
/// DeclContext. There's an exception, however, for the case of PackageUnit.
3988+
/// \see PackageUnit for details on how the hierachy between that and ModuleDecl
3989+
/// is created.
39913990
/// \see DeclContext::ASTHierarchy
39923991
/// \see AccessScope::getAccessScopeForFormalAccess
39933992
/// \see ValueDecl::isAccessibleFrom for a description of \p forConformance.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
762762
auto pkgName = A->getValue();
763763
if (!Lexer::isIdentifier(pkgName))
764764
Diags.diagnose(SourceLoc(), diag::error_bad_package_name, pkgName);
765-
else if (pkgName == STDLIB_NAME)
766-
Diags.diagnose(SourceLoc(), diag::error_stdlib_package_name, pkgName);
767765
else
768766
Opts.PackageName = pkgName;
769767
}

test/Sema/accessibility_compound.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 4
1+
// RUN: %target-typecheck-verify-swift -swift-version 4 -package-name mypkg
22

33
public struct Pair<A, B> {}
44

55
public struct PublicStruct {
66
public struct Inner {}
7-
internal struct Internal {}
7+
package struct PkgInner {}
8+
internal struct Internal {} // expected-note * {{type declared here}}
89
}
910

1011
private typealias PrivateAlias = PublicStruct // expected-note * {{type declared here}}
12+
package typealias PackageAlias = PublicStruct // expected-note * {{type declared here}}
1113

1214
public let a0 = nil as PrivateAlias.Inner? // expected-error {{constant cannot be declared public because its type 'PrivateAlias.Inner?' (aka 'Optional<PublicStruct.Inner>') uses a private type}}
1315
public let a: PrivateAlias.Inner? // expected-error {{constant cannot be declared public because its type uses a private type}}
@@ -16,6 +18,26 @@ public let c: Pair<PrivateAlias.Inner, PublicStruct.Internal>? // expected-error
1618
public let c2: Pair<PublicStruct.Internal, PrivateAlias.Inner>? // expected-error {{constant cannot be declared public because its type uses a private type}}
1719
public let d: PrivateAlias? // expected-error {{constant cannot be declared public because its type uses a private type}}
1820

21+
package let e = nil as PrivateAlias.Inner? // expected-error {{constant cannot be declared package because its type 'PrivateAlias.Inner?' (aka 'Optional<PublicStruct.Inner>') uses a private type}}
22+
package let f: PrivateAlias.Inner? // expected-error {{constant cannot be declared package because its type uses a private type}}
23+
package let g: PrivateAlias.PkgInner? // expected-error {{constant cannot be declared package because its type uses a private type}}
24+
package let h: Pair<PrivateAlias.PkgInner, PublicStruct.Inner>? // expected-error {{constant cannot be declared package because its type uses a private type}}
25+
package let i: Pair<PublicStruct.PkgInner, PrivateAlias.Inner>? // expected-error {{constant cannot be declared package because its type uses a private type}}
26+
package let j: PrivateAlias? // expected-error {{constant cannot be declared package because its type uses a private type}}
27+
28+
public let k = nil as PackageAlias.Inner? // expected-error {{constant cannot be declared public because its type 'PackageAlias.Inner?' (aka 'Optional<PublicStruct.Inner>') uses a package type}}
29+
public let l: PackageAlias.Inner? // expected-error {{constant cannot be declared public because its type uses a package type}}
30+
public let m: PackageAlias.Internal? // expected-error {{constant cannot be declared public because its type uses an internal type}}
31+
public let n: Pair<PackageAlias.Inner, PublicStruct.Internal>? // expected-error {{constant cannot be declared public because its type uses an internal type}}
32+
public let o: Pair<PublicStruct.Internal, PackageAlias.Inner>? // expected-error {{constant cannot be declared public because its type uses an internal type}}
33+
public let p: PackageAlias? // expected-error {{constant cannot be declared public because its type uses a package type}}
34+
35+
package let q = nil as PackageAlias.Inner? // no-error
36+
package let r: PackageAlias.Inner? // no-error
37+
package let s: PackageAlias.Internal? // expected-error {{constant cannot be declared package because its type uses an internal type}}
38+
package let t: Pair<PackageAlias.Inner, PublicStruct.Internal>? // expected-error {{constant cannot be declared package because its type uses an internal type}}
39+
package let u: Pair<PublicStruct.Internal, PackageAlias.Inner>? // expected-error {{constant cannot be declared package because its type uses an internal type}}
40+
package let v: PackageAlias? // no-error
1941

2042
// rdar://problem/21408035
2143
private class PrivateBox<T> { // expected-note 2 {{type declared here}}

test/Sema/accessibility_package.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -module-name Utils %t/Utils.swift -emit-module -emit-module-path %t/Utils.swiftmodule -package-name myLib
5+
// RUN: test -f %t/Utils.swiftmodule
6+
7+
// RUN: %target-swift-frontend -module-name Lib %t/Lib.swift -emit-module -emit-module-path %t/Lib.swiftmodule -package-name myLib -I %t
8+
// RUN: test -f %t/Lib.swiftmodule
9+
10+
// RUN: not %target-swift-frontend -typecheck %t/Lib.swift -package-name "otherLib" -I %t 2>&1 | %FileCheck %s
11+
// CHECK: error: cannot find type 'PackageProto' in scope
12+
// CHECK: error: 'pkgFunc' is inaccessible due to 'package' protection level
13+
// CHECK: error: cannot find 'PackageKlass' in scope
14+
15+
16+
// BEGIN Utils.swift
17+
package protocol PackageProto {
18+
var pkgVar: String { get set }
19+
}
20+
21+
package class PackageKlass {
22+
package init() {}
23+
package func pkgFunc() {}
24+
}
25+
26+
public class PublicKlass {
27+
public init() {}
28+
public func publicFunc() {}
29+
package func pkgFunc() {}
30+
}
31+
32+
33+
// BEGIN Lib.swift
34+
import Utils
35+
36+
public func start() {
37+
let x = PublicKlass()
38+
x.publicFunc()
39+
x.pkgFunc()
40+
41+
let y = PackageKlass()
42+
y.pkgFunc()
43+
}
44+
45+
package struct LibStruct : PackageProto {
46+
package var pkgVar: String = "lib"
47+
}
48+

test/Sema/accessibility_protocol_extension.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 4
1+
// RUN: %target-typecheck-verify-swift -swift-version 4 -package-name myPkg
22

33
fileprivate struct FilePrivateStruct {}
44
// expected-note@-1 4{{type declared here}}
@@ -7,7 +7,10 @@ private struct PrivateStruct {}
77
// expected-note@-1 4{{type declared here}}
88

99
internal struct InternalStruct {}
10-
// expected-note@-1 4{{type declared here}}
10+
// expected-note@-1 7{{type declared here}}
11+
12+
package struct PackageStruct {}
13+
// expected-note@-1 *{{type declared here}}
1114

1215
public protocol P {
1316
typealias TFP = FilePrivateStruct
@@ -18,6 +21,9 @@ public protocol P {
1821

1922
typealias TI = InternalStruct
2023
// expected-error@-1 {{type alias cannot be declared public because its underlying type uses an internal type}}
24+
25+
typealias TPkg = PackageStruct
26+
// expected-error@-1 {{type alias cannot be declared public because its underlying type uses a package type}}
2127
}
2228

2329
extension P {
@@ -49,4 +55,24 @@ extension P {
4955

5056
public var usesInternalStructProp: InternalStruct { get { } set { } }
5157
// expected-error@-1 {{property cannot be declared public because its type uses an internal type}}
58+
59+
60+
package func pkgUsesInternalStruct(_: InternalStruct) {}
61+
// expected-error@-1 {{method cannot be declared package because its parameter uses an internal type}}
62+
63+
package typealias PkgUsesInternalStructAlias = InternalStruct
64+
// expected-error@-1 {{type alias cannot be declared package because its underlying type uses an internal type}}
65+
66+
package var pkgUsesInternalStructProp: InternalStruct { get { } set { } }
67+
// expected-error@-1 {{property cannot be declared package because its type uses an internal type}}
68+
69+
70+
public func usesPackageStruct(_: PackageStruct) {}
71+
// expected-error@-1 {{method cannot be declared public because its parameter uses a package type}}
72+
73+
public typealias UsesPackageStructAlias = PackageStruct
74+
// expected-error@-1 {{type alias cannot be declared public because its underlying type uses a package type}}
75+
76+
public var usesPackageStructProp: PackageStruct { get { } set { } }
77+
// expected-error@-1 {{property cannot be declared public because its type uses a package type}}
5278
}

0 commit comments

Comments
 (0)