Allow Swift as a package name

Update AccessScope::isChildOf
Add more tests for package access level
Resolves rdar://106728606
This commit is contained in:
Ellie Shin
2023-03-13 15:18:06 -07:00
parent c71cdccd54
commit 0fe0d6d221
18 changed files with 655 additions and 68 deletions

View File

@@ -107,14 +107,12 @@ public:
/// \see AccessScope::checkAccessUsingAccessScope
/// \see DeclContext::ASTHierarchy
bool isChildOf(AccessScope AS) const {
if (isPackage()) { // This needs to be checked first before isInContext
return AS.isPublic();
} else if (isInContext()) {
if (isInContext()) {
if (AS.isInContext())
return allowsPrivateAccess(getDeclContext(), AS.getDeclContext());
else
return AS.isPublic();
} else { // It's public, so can't be a child of the argument scope
} else { // It's public, so can't be a child of public or less argument scope
return false;
}
}

View File

@@ -192,9 +192,6 @@ ERROR(error_bad_export_as_name,none,
ERROR(error_bad_package_name,none,
"package name \"%0\" is not a valid identifier",
(StringRef))
ERROR(error_stdlib_package_name,none,
"package name \"%0\" is reserved for the standard library",
(StringRef))
ERROR(error_stdlib_not_found,Fatal,
"unable to load standard library for target '%0'", (StringRef))
ERROR(error_module_alias_invalid_format,none,

View File

@@ -1681,8 +1681,9 @@ ERROR(access_control_open_bad_decl,none,
WARNING(access_control_non_objc_open_member,none,
"non-'@objc' %0 in extensions cannot be overridden; use 'public' instead",
(DescriptiveDeclKind))
ERROR(access_control_requires_package_name, none, "decl has a package access level but no -package-name was passed", ())
ERROR(access_control_requires_package_name, none,
"decl has a package access level but no -package-name was passed",
())
ERROR(invalid_decl_attribute,none,
"'%0' attribute cannot be applied to this declaration", (DeclAttribute))
ERROR(attr_invalid_on_decl_kind,none,

View File

@@ -160,18 +160,27 @@ class OverlayFile;
class ModuleSourceFileLocationMap;
/// A unit that allows grouping of modules by a package name.
/// It's set as a property in ModuleDecl, instead of as its parent decl context,
/// since otherwise it will break the existing decl context lookups that assume
/// ModuleDecl as the top level context. See \c ModuleDecl
///
/// PackageUnit is treated as an enclosing scope of ModuleDecl. Unlike other
/// DeclContext subclasses where a parent context is set in ctor, PackageUnit
/// (parent context) is set as a field in ModuleDecl (child context). It also has a
/// pointer back to the ModuleDecl, so that it can be used to return the module
/// in the existing DeclContext lookup functions, which assume ModuleDecl as
/// the top level context. Since both PackageUnit and ModuleDecl are created
/// in the ASTContext memory arena, i.e. they will be destroyed when the
/// ASTContext is destroyed, both pointng to each other is not considered risky.
///
/// See \c ModuleDecl
class PackageUnit: public DeclContext {
/// Identifies this package and used for the equality check
Identifier PackageName;
/// Weakly references ModuleDecl that points to this package.
/// Non-null reference to ModuleDecl that points to this package.
/// Instead of having multiple ModuleDecls pointing to one PackageUnit, we
/// create and set one PackageUnit per ModuleDecl, to make it easier to look
/// up the module pointing to this package; this look up is needed in existing
/// DeclContext functions, e.g. \c DeclContext::getModuleScopeContext,
/// and \c DeclContext::getParentModule
/// create one PackageUnit per ModuleDecl, to make it easier to look up the
/// module pointing to this package context, which is needed in the existing
/// DeclContext look up functions.
/// \see DeclContext::getModuleScopeContext
/// \see DeclContext::getParentModule
ModuleDecl &SourceModule;
PackageUnit(Identifier name, ModuleDecl &src)
@@ -219,7 +228,7 @@ class ModuleDecl
/// A package this module belongs to. It's set as a property instead of a
/// parent decl context; otherwise it will break the existing decl context
/// lookups that assume ModuleDecl as the top level context.
/// lookup functions that assume ModuleDecl as the top level context.
PackageUnit *Package = nullptr;
/// Module name to use when referenced in clients module interfaces.

View File

@@ -3974,12 +3974,11 @@ ValueDecl::getFormalAccessScope(const DeclContext *useDC,
/// (useDC) and the decl (VD) site, and returns true in this case, since
/// FileUnit is a child of nullptr based on the DeclContext hierarchy. The
/// hierarchy is created when subclasses of DeclContext such as FileUnit or
/// ModuleDecl are constructed. For example, FileUnit ctor takes ModuleDecl as
/// its parent DeclContext. There's an exception, however; the parent of
/// ModuleDecl is nullptr, not set to PackageUnit; ModuleDecl has a pointer to
/// PackageUnit as its field, and it is treated as the enclosing scope of
/// ModuleDecl in the `isChildOf` call.
///
/// ModuleDecl are constructed. For example, a top ClassDecl ctor takes FileUnit
/// as its parent DeclContext and FileUnit ctor takes ModuleDecl as its parent
/// DeclContext. There's an exception, however, for the case of PackageUnit.
/// \see PackageUnit for details on how the hierachy between that and ModuleDecl
/// is created.
/// \see DeclContext::ASTHierarchy
/// \see AccessScope::getAccessScopeForFormalAccess
/// \see ValueDecl::isAccessibleFrom for a description of \p forConformance.

View File

@@ -762,8 +762,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
auto pkgName = A->getValue();
if (!Lexer::isIdentifier(pkgName))
Diags.diagnose(SourceLoc(), diag::error_bad_package_name, pkgName);
else if (pkgName == STDLIB_NAME)
Diags.diagnose(SourceLoc(), diag::error_stdlib_package_name, pkgName);
else
Opts.PackageName = pkgName;
}

View File

@@ -1,13 +1,15 @@
// RUN: %target-typecheck-verify-swift -swift-version 4
// RUN: %target-typecheck-verify-swift -swift-version 4 -package-name mypkg
public struct Pair<A, B> {}
public struct PublicStruct {
public struct Inner {}
internal struct Internal {}
package struct PkgInner {}
internal struct Internal {} // expected-note * {{type declared here}}
}
private typealias PrivateAlias = PublicStruct // expected-note * {{type declared here}}
package typealias PackageAlias = PublicStruct // expected-note * {{type declared here}}
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}}
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
public let c2: Pair<PublicStruct.Internal, PrivateAlias.Inner>? // expected-error {{constant cannot be declared public because its type uses a private type}}
public let d: PrivateAlias? // expected-error {{constant cannot be declared public because its type uses a private type}}
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}}
package let f: PrivateAlias.Inner? // expected-error {{constant cannot be declared package because its type uses a private type}}
package let g: PrivateAlias.PkgInner? // expected-error {{constant cannot be declared package because its type uses a private type}}
package let h: Pair<PrivateAlias.PkgInner, PublicStruct.Inner>? // expected-error {{constant cannot be declared package because its type uses a private type}}
package let i: Pair<PublicStruct.PkgInner, PrivateAlias.Inner>? // expected-error {{constant cannot be declared package because its type uses a private type}}
package let j: PrivateAlias? // expected-error {{constant cannot be declared package because its type uses a private type}}
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}}
public let l: PackageAlias.Inner? // expected-error {{constant cannot be declared public because its type uses a package type}}
public let m: PackageAlias.Internal? // expected-error {{constant cannot be declared public because its type uses an internal type}}
public let n: Pair<PackageAlias.Inner, PublicStruct.Internal>? // expected-error {{constant cannot be declared public because its type uses an internal type}}
public let o: Pair<PublicStruct.Internal, PackageAlias.Inner>? // expected-error {{constant cannot be declared public because its type uses an internal type}}
public let p: PackageAlias? // expected-error {{constant cannot be declared public because its type uses a package type}}
package let q = nil as PackageAlias.Inner? // no-error
package let r: PackageAlias.Inner? // no-error
package let s: PackageAlias.Internal? // expected-error {{constant cannot be declared package because its type uses an internal type}}
package let t: Pair<PackageAlias.Inner, PublicStruct.Internal>? // expected-error {{constant cannot be declared package because its type uses an internal type}}
package let u: Pair<PublicStruct.Internal, PackageAlias.Inner>? // expected-error {{constant cannot be declared package because its type uses an internal type}}
package let v: PackageAlias? // no-error
// rdar://problem/21408035
private class PrivateBox<T> { // expected-note 2 {{type declared here}}

View File

@@ -0,0 +1,48 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s
// RUN: %target-swift-frontend -module-name Utils %t/Utils.swift -emit-module -emit-module-path %t/Utils.swiftmodule -package-name myLib
// RUN: test -f %t/Utils.swiftmodule
// RUN: %target-swift-frontend -module-name Lib %t/Lib.swift -emit-module -emit-module-path %t/Lib.swiftmodule -package-name myLib -I %t
// RUN: test -f %t/Lib.swiftmodule
// RUN: not %target-swift-frontend -typecheck %t/Lib.swift -package-name "otherLib" -I %t 2>&1 | %FileCheck %s
// CHECK: error: cannot find type 'PackageProto' in scope
// CHECK: error: 'pkgFunc' is inaccessible due to 'package' protection level
// CHECK: error: cannot find 'PackageKlass' in scope
// BEGIN Utils.swift
package protocol PackageProto {
var pkgVar: String { get set }
}
package class PackageKlass {
package init() {}
package func pkgFunc() {}
}
public class PublicKlass {
public init() {}
public func publicFunc() {}
package func pkgFunc() {}
}
// BEGIN Lib.swift
import Utils
public func start() {
let x = PublicKlass()
x.publicFunc()
x.pkgFunc()
let y = PackageKlass()
y.pkgFunc()
}
package struct LibStruct : PackageProto {
package var pkgVar: String = "lib"
}

View File

@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -swift-version 4
// RUN: %target-typecheck-verify-swift -swift-version 4 -package-name myPkg
fileprivate struct FilePrivateStruct {}
// expected-note@-1 4{{type declared here}}
@@ -7,7 +7,10 @@ private struct PrivateStruct {}
// expected-note@-1 4{{type declared here}}
internal struct InternalStruct {}
// expected-note@-1 4{{type declared here}}
// expected-note@-1 7{{type declared here}}
package struct PackageStruct {}
// expected-note@-1 *{{type declared here}}
public protocol P {
typealias TFP = FilePrivateStruct
@@ -18,6 +21,9 @@ public protocol P {
typealias TI = InternalStruct
// expected-error@-1 {{type alias cannot be declared public because its underlying type uses an internal type}}
typealias TPkg = PackageStruct
// expected-error@-1 {{type alias cannot be declared public because its underlying type uses a package type}}
}
extension P {
@@ -49,4 +55,24 @@ extension P {
public var usesInternalStructProp: InternalStruct { get { } set { } }
// expected-error@-1 {{property cannot be declared public because its type uses an internal type}}
package func pkgUsesInternalStruct(_: InternalStruct) {}
// expected-error@-1 {{method cannot be declared package because its parameter uses an internal type}}
package typealias PkgUsesInternalStructAlias = InternalStruct
// expected-error@-1 {{type alias cannot be declared package because its underlying type uses an internal type}}
package var pkgUsesInternalStructProp: InternalStruct { get { } set { } }
// expected-error@-1 {{property cannot be declared package because its type uses an internal type}}
public func usesPackageStruct(_: PackageStruct) {}
// expected-error@-1 {{method cannot be declared public because its parameter uses a package type}}
public typealias UsesPackageStructAlias = PackageStruct
// expected-error@-1 {{type alias cannot be declared public because its underlying type uses a package type}}
public var usesPackageStructProp: PackageStruct { get { } set { } }
// expected-error@-1 {{property cannot be declared public because its type uses a package type}}
}

View File

@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -swift-version 4 -module-name main
// RUN: %target-typecheck-verify-swift -swift-version 4 -module-name main -package-name myPkg
public protocol P {
associatedtype Element
@@ -10,17 +10,27 @@ struct S<T> : P {
func f() -> T { while true {} }
}
package struct PkgStruct<T> : P {
package func f() -> T { while true {} }
}
public struct G<T> {
typealias A = S<T> // expected-note {{type declared here}}
package typealias B = PkgStruct<T> // expected-note {{type declared here}}
public func foo<U : P>(u: U) where U.Element == A.Element {}
public func foo<U : P>(arg: U) where U.Element == A.Element {}
// expected-error@-1 {{instance method cannot be declared public because its generic requirement uses an internal type}}
public func bar<U : P>(arg: U) where U.Element == B.Element {}
// expected-error@-1 {{instance method cannot be declared public because its generic requirement uses a package type}}
}
public final class ReplayableGenerator<S: Sequence> : IteratorProtocol {
typealias Sequence = S // expected-note {{type declared here}}
public typealias Element = Sequence.Iterator.Element // expected-error {{type alias cannot be declared public because its underlying type uses an internal type}}
package typealias PkgSequence = S // expected-note {{type declared here}}
public typealias ElementPkg = PkgSequence.Iterator.Element // expected-error {{type alias cannot be declared public because its underlying type uses a package type}}
public func next() -> Element? {
return nil
}
@@ -30,6 +40,10 @@ struct Generic<T> {
fileprivate typealias Dependent = T // expected-note 2{{type declared here}}
}
package struct PkgGeneric<T> { // expected-note *{{type declared here}}
package typealias Dependent = T // expected-note *{{type declared here}}
}
var x: Generic<Int>.Dependent = 3 // expected-error {{variable must be declared private or fileprivate because its type uses a fileprivate type}}
func internalFuncWithFileprivateAlias() -> Generic<Int>.Dependent { // expected-error {{function must be declared private or fileprivate because its result uses a fileprivate type}}
@@ -42,54 +56,161 @@ private func privateFuncWithFileprivateAlias() -> Generic<Int>.Dependent {
var y = privateFuncWithFileprivateAlias() // expected-error{{variable must be declared private or fileprivate because its type 'Generic<Int>.Dependent' (aka 'Int') uses a fileprivate type}}
public var z: PkgGeneric<Int>.Dependent = 3 // expected-error {{variable cannot be declared public because its type uses a package type}}
public func funcWithPackageAlias() -> PkgGeneric<Int>.Dependent { // expected-error {{function cannot be declared public because its result uses a package type}}
return 3
}
package func pkgFuncWithPackageAlias() -> PkgGeneric<Int>.Dependent {
return 3
}
public var zz = pkgFuncWithPackageAlias() // expected-error{{variable cannot be declared public because its type 'PkgGeneric<Int>.Dependent' (aka 'Int') uses a package type}}
private typealias FnType = (_ x: Int) -> Void // expected-note * {{type declared here}}
package typealias PkgFnType = (_ x: Int) -> Void // expected-note * {{type declared here}}
public var fn1: (FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn2: (_ x: FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn3: (main.FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn4: (_ x: main.FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn1p: (PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn2p: (_ x: PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn3p: (main.PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var fn4p: (_ x: main.PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
package var pfn1: (FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfn2: (_ x: FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfn3: (main.FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfn4: (_ x: main.FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
public var nested1: (_ x: (FnType) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var nested2: (_ x: (main.FnType) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var nested1p: (_ x: (PkgFnType) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var nested2p: (_ x: (main.PkgFnType) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
package var pnested1: (_ x: (FnType) -> Void) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pnested2: (_ x: (main.FnType) -> Void) -> Void = { _ in } // expected-error {{cannot be declared package}}
public func test1(x: FnType) {} // expected-error {{cannot be declared public}}
public func test2(x: main.FnType) {} // expected-error {{cannot be declared public}}
public func test1p(x: PkgFnType) {} // expected-error {{cannot be declared public}}
public func test2p(x: main.PkgFnType) {} // expected-error {{cannot be declared public}}
package func ptest1(x: FnType) {} // expected-error {{cannot be declared package}}
package func ptest2(x: main.FnType) {} // expected-error {{cannot be declared package}}
public func reject1(x: FnType?) {} // expected-error {{cannot be declared public}}
public func reject2(x: main.FnType?) {} // expected-error {{cannot be declared public}}
public func reject3() -> FnType { fatalError() } // expected-error {{cannot be declared public}}
public func reject4() -> main.FnType { fatalError() } // expected-error {{cannot be declared public}}
public func reject1p(x: PkgFnType?) {} // expected-error {{cannot be declared public}}
public func reject2p(x: main.PkgFnType?) {} // expected-error {{cannot be declared public}}
public func reject3p() -> PkgFnType { fatalError() } // expected-error {{cannot be declared public}}
public func reject4p() -> main.PkgFnType { fatalError() } // expected-error {{cannot be declared public}}
package func preject1(x: FnType?) {} // expected-error {{cannot be declared package}}
package func preject2(x: main.FnType?) {} // expected-error {{cannot be declared package}}
package func preject3() -> FnType { fatalError() } // expected-error {{cannot be declared package}}
package func preject4() -> main.FnType { fatalError() } // expected-error {{cannot be declared package}}
public var rejectVar1: FnType = {_ in } // expected-error {{cannot be declared public}}
public var rejectVar2: main.FnType = {_ in } // expected-error {{cannot be declared public}}
public var rejectVar3: FnType? // expected-error {{cannot be declared public}}
public var rejectVar4: main.FnType? // expected-error {{cannot be declared public}}
public var rejectVar1p: PkgFnType = {_ in } // expected-error {{cannot be declared public}}
public var rejectVar2p: main.PkgFnType = {_ in } // expected-error {{cannot be declared public}}
public var rejectVar3p: PkgFnType? // expected-error {{cannot be declared public}}
public var rejectVar4p: main.PkgFnType? // expected-error {{cannot be declared public}}
package var prejectVar1: FnType = {_ in } // expected-error {{cannot be declared package}}
package var prejectVar2: main.FnType = {_ in } // expected-error {{cannot be declared package}}
package var prejectVar3: FnType? // expected-error {{cannot be declared package}}
package var prejectVar4: main.FnType? // expected-error {{cannot be declared package}}
public var escaping1: (@escaping FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping2: (_ x: @escaping FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping3: (@escaping main.FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping4: (_ x: @escaping main.FnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping1p: (@escaping PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping2p: (_ x: @escaping PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping3p: (@escaping main.PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var escaping4p: (_ x: @escaping main.PkgFnType) -> Void = { _ in } // expected-error {{cannot be declared public}}
package var pescaping1: (@escaping FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pescaping2: (_ x: @escaping FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pescaping3: (@escaping main.FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pescaping4: (_ x: @escaping main.FnType) -> Void = { _ in } // expected-error {{cannot be declared package}}
public struct SubscriptTest {
public subscript(test1 x: FnType) -> () { return } // expected-error {{cannot be declared public}}
public subscript(test2 x: main.FnType) -> () { return } // expected-error {{cannot be declared public}}
public subscript(test1p x: PkgFnType) -> () { return } // expected-error {{cannot be declared public}}
public subscript(test2p x: main.PkgFnType) -> () { return } // expected-error {{cannot be declared public}}
package subscript(ptest1 x: FnType) -> () { return } // expected-error {{cannot be declared package}}
package subscript(ptest2 x: main.FnType) -> () { return } // expected-error {{cannot be declared package}}
public subscript(reject1 x: FnType?) -> () { return } // expected-error {{cannot be declared public}}
public subscript(reject2 x: main.FnType?) -> () { return } // expected-error {{cannot be declared public}}
public subscript(reject3 x: Int) -> FnType { fatalError() } // expected-error {{cannot be declared public}}
public subscript(reject4 x: Int) -> main.FnType { fatalError() } // expected-error {{cannot be declared public}}
public subscript(reject1p x: PkgFnType?) -> () { return } // expected-error {{cannot be declared public}}
public subscript(reject2p x: main.PkgFnType?) -> () { return } // expected-error {{cannot be declared public}}
public subscript(reject3p x: Int) -> PkgFnType { fatalError() } // expected-error {{cannot be declared public}}
public subscript(reject4p x: Int) -> main.PkgFnType { fatalError() } // expected-error {{cannot be declared public}}
package subscript(preject1 x: FnType?) -> () { return } // expected-error {{cannot be declared package}}
package subscript(preject2 x: main.FnType?) -> () { return } // expected-error {{cannot be declared package}}
package subscript(preject3 x: Int) -> FnType { fatalError() } // expected-error {{cannot be declared package}}
package subscript(preject4 x: Int) -> main.FnType { fatalError() } // expected-error {{cannot be declared package}}
}
private struct ActuallyPrivate {} // expected-note * {{declared here}}
private typealias ActuallyPrivateAlias = ActuallyPrivate
package struct ActuallyPackage {} // expected-note * {{declared here}}
private typealias ActuallyPackageAlias = ActuallyPackage
public var failFn: (ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFn2: (_ x: ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFn3: (main.ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFn4: (_ x: main.ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFnp: (ActuallyPackage) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFn2p: (_ x: ActuallyPackage) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFn3p: (main.ActuallyPackage) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failFn4p: (_ x: main.ActuallyPackage) -> Void = { _ in } // expected-error {{cannot be declared public}}
package var pfailFn: (ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfailFn2: (_ x: ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfailFn3: (main.ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfailFn4: (_ x: main.ActuallyPrivate) -> Void = { _ in } // expected-error {{cannot be declared package}}
public var failNested1: (_ x: (ActuallyPrivate) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failNested2: (_ x: (main.ActuallyPrivate) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public func failTest(x: ActuallyPrivate) {} // expected-error {{cannot be declared public}}
public func failTest2(x: main.ActuallyPrivate) {} // expected-error {{cannot be declared public}}
public var failNested1p: (_ x: (ActuallyPackage) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public var failNested2p: (_ x: (main.ActuallyPackage) -> Void) -> Void = { _ in } // expected-error {{cannot be declared public}}
public func failTestp(x: ActuallyPackage) {} // expected-error {{cannot be declared public}}
public func failTest2p(x: main.ActuallyPackage) {} // expected-error {{cannot be declared public}}
package var pfailNested1: (_ x: (ActuallyPrivate) -> Void) -> Void = { _ in } // expected-error {{cannot be declared package}}
package var pfailNested2: (_ x: (main.ActuallyPrivate) -> Void) -> Void = { _ in } // expected-error {{cannot be declared package}}
package func pfailTest(x: ActuallyPrivate) {} // expected-error {{cannot be declared package}}
package func pfailTest2(x: main.ActuallyPrivate) {} // expected-error {{cannot be declared package}}
// Property has an inferred type, public alias with
// private generic parameter bound.
public struct PublicGeneric<T> {}
@@ -97,6 +218,13 @@ public struct PublicGeneric<T> {}
public typealias GenericAlias<T> = PublicGeneric<T>
fileprivate func makeAValue() -> GenericAlias<ActuallyPrivate> { }
package func makeAValuePkg() -> GenericAlias<ActuallyPackage> { }
public var cannotBePublic = makeAValue()
// expected-error@-1 {{variable cannot be declared public because its type 'GenericAlias<ActuallyPrivate>' (aka 'PublicGeneric<ActuallyPrivate>') uses a private type}}
package var cannotBePackage = makeAValue()
// expected-error@-1 {{variable cannot be declared package because its type 'GenericAlias<ActuallyPrivate>' (aka 'PublicGeneric<ActuallyPrivate>') uses a private type}}
public var cannotBePublicPkg = makeAValuePkg()
// expected-error@-1 {{variable cannot be declared public because its type 'GenericAlias<ActuallyPackage>' (aka 'PublicGeneric<ActuallyPackage>') uses a package type}}

View File

@@ -1,7 +1,8 @@
// RUN: %target-typecheck-verify-swift -swift-version 5
// RUN: %target-typecheck-verify-swift -swift-version 5 -package-name myPkg
private struct PrivateStruct {} // expected-note 8{{type declared here}}
internal struct InternalStruct {} // expected-note 2{{type declared here}}
private struct PrivateStruct {} // expected-note 10{{type declared here}}
internal struct InternalStruct {} // expected-note 4{{type declared here}}
package struct PackageStruct {} // expected-note *{{type declared here}}
public struct PublicStruct {}
private class PrivateClass {} // expected-note 4{{type declared here}}
@@ -26,7 +27,35 @@ public protocol PublicProtocol2 : BaseProtocol where T == InternalStruct {
// expected-error@-1 {{associated type in a public protocol uses an internal type in its requirement}}
}
public protocol PublicProtocol3 : BaseProtocol where T == PublicStruct {
public protocol PublicProtocol3 : BaseProtocol where T == PackageStruct {
// expected-error@-1 {{public protocol's 'where' clause cannot use a package struct}}
associatedtype X : BaseProtocol where X.T == PackageStruct
// expected-error@-1 {{associated type in a public protocol uses a package type in its requirement}}
}
public protocol PublicProtocol4 : BaseProtocol where T == PublicStruct {
associatedtype X : BaseProtocol where X.T == PublicStruct
}
package protocol PackageProtocol1 : BaseProtocol where T == PrivateStruct {
// expected-error@-1 {{package protocol's 'where' clause cannot use a private struct}}
associatedtype X : BaseProtocol where X.T == PrivateStruct
// expected-error@-1 {{associated type in a package protocol uses a private type in its requirement}}
}
package protocol PackageProtocol2 : BaseProtocol where T == InternalStruct {
// expected-error@-1 {{package protocol's 'where' clause cannot use an internal struct}}
associatedtype X : BaseProtocol where X.T == InternalStruct
// expected-error@-1 {{associated type in a package protocol uses an internal type in its requirement}}
}
package protocol PackageProtocol3 : BaseProtocol where T == PackageStruct {
associatedtype X : BaseProtocol where X.T == PackageStruct
}
package protocol PackageProtocol4 : BaseProtocol where T == PublicStruct {
associatedtype X : BaseProtocol where X.T == PublicStruct
}
@@ -41,7 +70,11 @@ internal protocol InternalProtocol2 : BaseProtocol where T == InternalStruct {
associatedtype X : BaseProtocol where X.T == InternalStruct
}
internal protocol InternalProtocol3 : BaseProtocol where T == PublicStruct {
internal protocol InternalProtocol3 : BaseProtocol where T == PackageStruct {
associatedtype X : BaseProtocol where X.T == PackageStruct
}
internal protocol InternalProtocol4 : BaseProtocol where T == PublicStruct {
associatedtype X : BaseProtocol where X.T == PublicStruct
}
@@ -88,7 +121,7 @@ protocol Protocol7 : BaseProtocol where T == (PrivateStruct) -> Void {
// expected-error@-1 {{associated type in an internal protocol uses a private type in its requirement}}
}
private protocol PrivateProtocol {} // expected-note 2{{type declared here}}
private protocol PrivateProtocol {} // expected-note 4{{type declared here}}
struct GenericStruct<T> {
struct Inner where T : PrivateProtocol {}
@@ -96,3 +129,10 @@ struct GenericStruct<T> {
func nonGenericWhereClause() where T : PrivateProtocol {}
// expected-error@-1 {{instance method must be declared private because its generic requirement uses a private type}}
}
package struct PkgGenericStruct<T> {
package struct Inner where T : PrivateProtocol {}
// expected-error@-1 {{struct cannot be declared package because its generic requirement uses a private type}}
package func nonGenericWhereClause() where T : PrivateProtocol {}
// expected-error@-1 {{instance method cannot be declared package because its generic requirement uses a private type}}
}

View File

@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -package-name myPkg
// CHECK PARSING
private // expected-note {{modifier already specified here}}
@@ -16,9 +16,16 @@ func triplicateAttrChanged() {}
private // expected-note 3 {{modifier already specified here}}
public // expected-error {{duplicate modifier}}
package // expected-error {{duplicate modifier}}
internal // expected-error {{duplicate modifier}}
func quadruplicateAttrChanged() {}
private // expected-note 4 {{modifier already specified here}}
public // expected-error {{duplicate modifier}}
package // expected-error {{duplicate modifier}}
internal // expected-error {{duplicate modifier}}
fileprivate // expected-error {{duplicate modifier}}
func quadruplicateAttrChanged() {}
func quintuplicateAttrChanged() {}
private(set)
public
@@ -28,6 +35,27 @@ fileprivate(set)
public
var customSetter2 = 0
// FIXME: rdar://104931420 folowing should not error
// expected-error @+2{{cannot find 'package' in scope}}{{none}}
// expected-error @+1{{cannot find 'set' in scope; did you mean 'Set'?}}{{9-12=Set}}
package(set)
public
var customSetter3 = 0
// FIXME: rdar://104931420 folowing should not error
// expected-error @+1{{expected expression}}
public
package(set)
var customSetter4 = 0
private(set)
package
var customSetter5 = 0
internal(set)
package
var customSetter6 = 0
private(set) // expected-note {{modifier already specified here}}
public(set) // expected-error {{duplicate modifier}}
var customSetterDuplicateAttr = 0
@@ -87,10 +115,16 @@ private enum TestEnum {
private case Foo, Bar // expected-error {{'private' modifier cannot be applied to this declaration}} {{3-11=}}
}
package enum PkgTestEnum {
package case Foo, Bar // expected-error {{'package' modifier cannot be applied to this declaration}} {{3-11=}}
}
private protocol TestProtocol {
private associatedtype Foo // expected-error {{'private' modifier cannot be applied to this declaration}} {{3-11=}}
internal var Bar: Int { get } // expected-error {{'internal' modifier cannot be used in protocols}} {{3-12=}}
// expected-note@-1 {{protocol requirements implicitly have the same access as the protocol itself}}
package func cat() // expected-error {{'package' modifier cannot be used in protocols}} {{3-11=}}
// expected-note@-1 {{protocol requirements implicitly have the same access as the protocol itself}}
public func baz() // expected-error {{'public' modifier cannot be used in protocols}} {{3-10=}}
// expected-note@-1 {{protocol requirements implicitly have the same access as the protocol itself}}
}
@@ -98,6 +132,7 @@ private protocol TestProtocol {
public(set) func publicSetFunc() {} // expected-error {{'public' modifier cannot be applied to this declaration}} {{1-13=}}
public(set) var defaultVis = 0 // expected-error {{internal variable cannot have a public setter}}
internal(set) private var privateVis = 0 // expected-error {{private variable cannot have an internal setter}}
private(set) var defaultVisOK = 0
private(set) public var publicVis = 0
@@ -160,18 +195,49 @@ internal protocol EmptyProto2 {}
private extension Properties : EmptyProto {} // expected-error {{'private' modifier cannot be used with extensions that declare protocol conformances}} {{1-9=}}
private(set) extension Properties : EmptyProto2 {} // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-14=}}
package protocol EmptyProto3 {}
package protocol EmptyProto4 {}
public protocol EmptyProto5 {}
private extension Properties : EmptyProto3 {} // expected-error {{'private' modifier cannot be used with extensions that declare protocol conformances}} {{1-9=}}
private(set) extension Properties : EmptyProto4 {} // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-14=}}
package extension Properties : EmptyProto5 {} // expected-error {{'package' modifier cannot be used with extensions that declare protocol conformances}} {{1-9=}}
public struct PublicStruct {}
package struct PackageStruct {} // expected-note * {{declared here}}
internal struct InternalStruct {} // expected-note * {{declared here}}
private struct PrivateStruct {} // expected-note * {{declared here}}
protocol InternalProto { // expected-note * {{declared here}}
associatedtype Assoc
}
package protocol PackageProto { // expected-note * {{declared here}}
associatedtype Assoc
}
public extension InternalProto {} // expected-error {{extension of internal protocol cannot be declared public}} {{1-8=}}
public extension PackageProto {} // expected-error {{extension of package protocol cannot be declared public}} {{1-8=}}
package extension PackageProto {} // no-error
package extension InternalProto {} // expected-error {{extension of internal protocol cannot be declared package}} {{1-9=}}
package extension PackageProto where Assoc == PublicStruct {}
package extension PackageProto where Assoc == PackageStruct {}
package extension PackageProto where Assoc == InternalStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses an internal type}}
package extension PackageProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses a private type}}
internal extension PackageProto where Assoc == PublicStruct {}
internal extension PackageProto where Assoc == PackageStruct {}
internal extension PackageProto where Assoc == InternalStruct {}
internal extension PackageProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared internal because its generic requirement uses a private type}}
private extension PackageProto where Assoc == PublicStruct {}
private extension PackageProto where Assoc == PackageStruct {}
private extension PackageProto where Assoc == InternalStruct {}
private extension PackageProto where Assoc == PrivateStruct {}
internal extension InternalProto where Assoc == PublicStruct {}
internal extension InternalProto where Assoc == PackageStruct {}
internal extension InternalProto where Assoc == InternalStruct {}
internal extension InternalProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared internal because its generic requirement uses a private type}}
private extension InternalProto where Assoc == PublicStruct {}
private extension InternalProto where Assoc == PackageStruct {}
private extension InternalProto where Assoc == InternalStruct {}
private extension InternalProto where Assoc == PrivateStruct {}
@@ -180,15 +246,29 @@ public protocol PublicProto {
}
public extension PublicProto {}
public extension PublicProto where Assoc == PublicStruct {}
public extension PublicProto where Assoc == PackageStruct {} // expected-error {{extension cannot be declared public because its generic requirement uses a package type}}
public extension PublicProto where Assoc == InternalStruct {} // expected-error {{extension cannot be declared public because its generic requirement uses an internal type}}
public extension PublicProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared public because its generic requirement uses a private type}}
package extension PublicProto where Assoc == PublicStruct {}
package extension PublicProto where Assoc == PackageStruct {}
package extension PublicProto where Assoc == InternalStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses an internal type}}
package extension PublicProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses a private type}}
internal extension PublicProto where Assoc == PublicStruct {}
internal extension PublicProto where Assoc == PackageStruct {}
internal extension PublicProto where Assoc == InternalStruct {}
internal extension PublicProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared internal because its generic requirement uses a private type}}
private extension PublicProto where Assoc == PublicStruct {}
private extension PublicProto where Assoc == PackageStruct {}
private extension PublicProto where Assoc == InternalStruct {}
private extension PublicProto where Assoc == PrivateStruct {}
extension PublicProto where Assoc == PackageStruct {
public func foo() {} // expected-error {{cannot declare a public instance method in an extension with package requirements}} {{3-9=package}}
open func bar() {} // expected-error {{cannot declare an open instance method in an extension with package requirements}} {{3-7=package}}
}
extension PublicProto where Assoc == InternalStruct {
public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
open func bar() {} // expected-error {{cannot declare an open instance method in an extension with internal requirements}} {{3-7=internal}}
@@ -200,10 +280,23 @@ extension InternalProto where Assoc == PublicStruct {
public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
open func bar() {} // expected-error {{cannot declare an open instance method in an extension with internal requirements}} {{3-7=internal}}
}
extension InternalProto where Assoc == PackageStruct {
public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
open func bar() {} // expected-error {{cannot declare an open instance method in an extension with internal requirements}} {{3-7=internal}}
}
public struct GenericStruct<Param> {}
public extension GenericStruct where Param: InternalProto {} // expected-error {{extension cannot be declared public because its generic requirement uses an internal type}}
extension GenericStruct where Param: InternalProto {
public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
}
public extension GenericStruct where Param: PackageProto {} // expected-error {{extension cannot be declared public because its generic requirement uses a package type}}
extension GenericStruct where Param: PackageProto {
public func foo() {} // expected-error {{cannot declare a public instance method in an extension with package requirements}} {{3-9=package}}
}
package struct PkgGenericStruct<Param> {}
package extension PkgGenericStruct where Param: InternalProto {} // expected-error {{extension cannot be declared package because its generic requirement uses an internal type}}
extension PkgGenericStruct where Param: InternalProto {
package func foo() {} // expected-error {{cannot declare a package instance method in an extension with internal requirements}} {{3-10=internal}}
}

View File

@@ -1,16 +1,19 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-swift-frontend -typecheck -disable-access-control %s
// RUN: %target-swift-frontend -typecheck -disable-access-control -package-name myPkg %s
public protocol ProtoWithReqs {
associatedtype Assoc
associatedtype AssocA // expected-note * {{type declared here}}
associatedtype AssocB // expected-note * {{type declared here}}
func foo()
}
public struct Adopter<T> : ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'Assoc' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
extension Adopter {
typealias Assoc = Int
typealias AssocA = Int
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-3=public }}
typealias AssocB = String
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-3=public }}
func foo() {}
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-3=public }}
@@ -18,9 +21,12 @@ extension Adopter {
public struct Adopter2<T> : ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'Assoc' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public extension Adopter2 {
internal typealias Assoc = Int
internal typealias AssocA = Int
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-12=}}
package typealias AssocB = String
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-12=}}
fileprivate func foo() {}
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-15=}}
@@ -28,64 +34,169 @@ public extension Adopter2 {
public struct Adopter3<T> : ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'Assoc' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
internal extension Adopter3 {
typealias Assoc = Int
typealias AssocA = Int
// expected-note@-1 {{move the type alias to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
typealias AssocB = String
// expected-note@-1 {{move the type alias to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
func foo() {}
// expected-note@-1 {{move the instance method to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
}
public struct Adopter4<T> : ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
package extension Adopter4 {
internal typealias AssocA = Int
// expected-note@-1 {{mark the type alias as 'package' to satisfy the requirement}} {{3-12=}}
typealias AssocB = String // package modifier is redundant for package extension
fileprivate func foo() {}
// expected-note@-1 {{mark the instance method as 'package' to satisfy the requirement}} {{3-15=}}
}
package protocol PkgProtoWithReqs {
associatedtype AssocA // expected-note * {{type declared here}}
associatedtype AssocB // expected-note * {{type declared here}}
func foo()
}
package struct Adopter5<T> : PkgProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
extension Adopter5 {
typealias AssocA = Int
// expected-note@-1 {{mark the type alias as 'package' to satisfy the requirement}} {{3-3=package }}
typealias AssocB = String
// expected-note@-1 {{mark the type alias as 'package' to satisfy the requirement}} {{3-3=package }}
func foo() {}
// expected-note@-1 {{mark the instance method as 'package' to satisfy the requirement}} {{3-3=package }}
}
public class AnotherAdopterBase {
typealias Assoc = Int
typealias AssocA = Int
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-3=public }}
package typealias AssocB = String
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-11=public }}
func foo() {}
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-3=public }}
}
public class AnotherAdopterSub : AnotherAdopterBase, ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'Assoc' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public class AnotherAdopterBase2 {}
public extension AnotherAdopterBase2 {
internal typealias Assoc = Int
internal typealias AssocA = Int
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-12=}}
package typealias AssocB = String
// expected-note@-1 {{mark the type alias as 'public' to satisfy the requirement}} {{3-11=}}
fileprivate func foo() {}
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-15=}}
}
public class AnotherAdopterSub2 : AnotherAdopterBase2, ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'Assoc' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public class AnotherAdopterBase3 {}
internal extension AnotherAdopterBase3 {
typealias Assoc = Int
typealias AssocA = Int
// expected-note@-1 {{move the type alias to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
typealias AssocB = String
// expected-note@-1 {{move the type alias to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
func foo() {}
// expected-note@-1 {{move the instance method to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
}
public class AnotherAdopterSub3 : AnotherAdopterBase3, ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'Assoc' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public class AnotherAdopterBase4 {}
package extension AnotherAdopterBase4 {
typealias AssocA = Int
// expected-note@-1 {{move the type alias to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
typealias AssocB = String
// expected-note@-1 {{move the type alias to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
func foo() {}
// expected-note@-1 {{move the instance method to another extension where it can be declared 'public' to satisfy the requirement}} {{none}}
}
public class AnotherAdopterSub4 : AnotherAdopterBase4, ProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
package class AnotherAdopterBase5 {}
internal extension AnotherAdopterBase5 {
typealias AssocA = Int
// expected-note@-1 {{move the type alias to another extension where it can be declared 'package' to satisfy the requirement}} {{none}}
typealias AssocB = String
// expected-note@-1 {{move the type alias to another extension where it can be declared 'package' to satisfy the requirement}} {{none}}
func foo() {}
// expected-note@-1 {{move the instance method to another extension where it can be declared 'package' to satisfy the requirement}} {{none}}
}
package class AnotherAdopterSub5 : AnotherAdopterBase5, PkgProtoWithReqs {}
// expected-error@-1 {{method 'foo()' must be declared package because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocA' must be declared package because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
// expected-error@-3 {{type alias 'AssocB' must be declared package because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
public protocol ReqProvider {}
extension ReqProvider {
func foo() {}
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-3=public }}
package typealias AssocB = String
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-11=public }}
}
public struct AdoptViaProtocol : ProtoWithReqs, ReqProvider {
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public typealias Assoc = Int
// expected-error@-2 {{type alias 'AssocB' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public typealias AssocA = Int
}
package protocol PkgReqProvider {}
extension PkgReqProvider {
private func foo() {}
// expected-note@-1 {{mark the instance method as 'package' to satisfy the requirement}} {{3-11=public }}
typealias AssocB = String
// expected-note@-1 {{mark the instance method as 'package' to satisfy the requirement}} {{3-3=public }}
}
package struct PkgAdoptViaProtocol : PkgProtoWithReqs, PkgReqProvider {
// expected-error@-1 {{method 'foo()' must be declared package because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocB' must be declared package because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
package typealias AssocA = Int
}
public protocol ReqProvider2 {}
extension ProtoWithReqs where Self : ReqProvider2 {
func foo() {}
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-3=public }}
typealias AssocB = String
// expected-note@-1 {{mark the instance method as 'public' to satisfy the requirement}} {{3-3=public }}
}
public struct AdoptViaCombinedProtocol : ProtoWithReqs, ReqProvider2 {
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in public protocol 'ProtoWithReqs'}} {{none}}
public typealias Assoc = Int
// expected-error@-2 {{type alias 'AssocB' must be declared public because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
public typealias AssocA = Int
}
package protocol PkgReqProvider2 {}
extension PkgProtoWithReqs where Self : PkgReqProvider2 {
func foo() {}
// expected-note@-1 {{mark the instance method as 'package' to satisfy the requirement}} {{3-3=public }}
typealias AssocB = String
// expected-note@-1 {{mark the instance method as 'package' to satisfy the requirement}} {{3-3=public }}
}
package struct PkgAdoptViaCombinedProtocol : PkgProtoWithReqs, PkgReqProvider2 {
// expected-error@-1 {{method 'foo()' must be declared public because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
// expected-error@-2 {{type alias 'AssocB' must be declared package because it matches a requirement in package protocol 'PkgProtoWithReqs'}} {{none}}
public typealias AssocA = Int
}
public protocol PublicInitProto {
@@ -105,6 +216,24 @@ public struct NonPublicMemberwiseInitStruct: PublicInitProto {
public var value: Int
}
package protocol PackageInitProto {
var value: Int { get }
init(value: Int)
}
package struct NonPackageInitStruct: PackageInitProto {
package var value: Int
init(value: Int) {
// expected-error@-1 {{initializer 'init(value:)' must be declared package because it matches a requirement in package protocol 'PackageInitProto'}}
// expected-note@-2 {{mark the initializer as 'package' to satisfy the requirement}}
self.value = value
}
}
package struct NonPackageMemberwiseInitStruct: PackageInitProto {
// expected-error@-1 {{initializer 'init(value:)' must be declared package because it matches a requirement in package protocol 'PackageInitProto'}}
public var value: Int
}
// https://github.com/apple/swift/issues/57595
public protocol PublicEmptyInit {
@@ -113,3 +242,10 @@ public protocol PublicEmptyInit {
public struct Buggy: PublicEmptyInit {
// expected-error@-1 {{initializer 'init()' must be declared public because it matches a requirement in public protocol 'PublicEmptyInit'}}
}
package protocol PkgEmptyInit {
init()
}
public struct PkgBuggy: PkgEmptyInit {
// expected-error@-1 {{initializer 'init()' must be declared public because it matches a requirement in package protocol 'PkgEmptyInit'}}
}

View File

@@ -1,7 +1,8 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -package-name myPkg
public class OuterClass {
class InnerClass {}
package class PkgInnerClass {}
}
public protocol PublicProto2 {
@@ -9,6 +10,15 @@ public protocol PublicProto2 {
associatedtype U
}
package class PkgOuterClass {
class InnerClass {}
}
package protocol PackageProto {
associatedtype T
associatedtype U
}
// FIXME: With the current design, the below should not diagnose.
//
// However, it does, because we look at the bound decl in the
@@ -22,8 +32,36 @@ extension PublicProto2 where Self.T : OuterClass, Self.U == Self.T.InnerClass {
// expected-error@-1 {{cannot declare a public instance method in an extension with internal requirements}}
}
package extension PublicProto2 where Self.T : OuterClass, Self.U == Self.T.PkgInnerClass {
public func cannotBePublic() {}
// expected-error@-1 {{cannot declare a public instance method in an extension with package requirements}}
}
extension PackageProto where Self.T : OuterClass, Self.U == Self.T.InnerClass {
package func cannotBePublic() {}
// expected-error@-1 {{cannot declare a package instance method in an extension with internal requirements}}
}
public extension OuterClass {
open convenience init(x: ()) { self.init() }
// expected-warning@-1 {{'open' modifier conflicts with extension's default access of 'public'}}
// expected-error@-2 {{only classes and overridable class members can be declared 'open'; use 'public'}}
}
internal extension OuterClass {
open convenience init(x: (), y: ()) { self.init() }
// expected-warning@-1 {{'open' modifier conflicts with extension's default access of 'internal'}}
// expected-error@-2 {{only classes and overridable class members can be declared 'open'; use 'public'}}
}
package extension OuterClass {
open convenience init(x: (), y: (), z: ()) { self.init() }
// expected-warning@-1 {{'open' modifier conflicts with extension's default access of 'package'}}
// expected-error@-2 {{only classes and overridable class members can be declared 'open'; use 'public'}}
}
package extension PkgOuterClass {
open convenience init(x: ()) { self.init() }
// expected-warning@-1 {{'open' modifier conflicts with extension's default access of 'package'}}
// expected-error@-2 {{only classes and overridable class members can be declared 'open'; use 'public'}}
}

View File

@@ -162,4 +162,4 @@ public struct TestGenericSubscripts {
@usableFromInline func nonGenericWhereClause() where T : InternalProtocol {}
// expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' instance method must be '@usableFromInline' or public}}
}
}

View File

@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -typecheck -verify %s -disable-availability-checking
// RUN: %target-swift-frontend -typecheck -verify %s -disable-availability-checking -package-name myPkg
// REQUIRES: concurrency
actor SomeActor { }
@@ -107,17 +107,23 @@ extension SomeActor {
// -----------------------------------------------------------------------
@globalActor
private struct PrivateGA { // expected-note 2 {{type declared here}}
private struct PrivateGA { // expected-note 3 {{type declared here}}
actor Actor {}
static let shared = Actor()
}
@globalActor
internal struct InternalGA { // expected-note 1 {{type declared here}}
internal struct InternalGA { // expected-note 2 {{type declared here}}
actor Actor {}
static let shared = Actor()
}
@globalActor
package struct PackageGA { // expected-note 1 {{type declared here}}
package actor Actor {}
package static let shared = Actor()
}
@globalActor
public struct PublicGA {
public actor Actor {}
@@ -126,12 +132,20 @@ public struct PublicGA {
@PrivateGA private struct PrivateStructPrivateGA {}
@InternalGA private struct PrivateStructInternalGA {}
@PackageGA private struct PrivateStructPackageGA {}
@PublicGA private struct PrivateStructPublicGA {}
@PrivateGA internal struct InternalStructPrivateGA {} // expected-error {{internal struct 'InternalStructPrivateGA' cannot have private global actor 'PrivateGA'}}
@InternalGA internal struct InternalStructInternalGA {}
@PackageGA internal struct InternalStructPackageGA {}
@PublicGA internal struct InternalStructPublicGA {}
@PrivateGA package class PackageClassPrivateGA {} // expected-error {{package class 'PackageClassPrivateGA' cannot have private global actor 'PrivateGA'}}
@InternalGA package class PackageClassInternalGA {} // expected-error {{package class 'PackageClassInternalGA' cannot have internal global actor 'InternalGA'}}
@PackageGA package struct PackageClassPackageGA {}
@PublicGA package class PackageClassPublicGA {}
@PrivateGA open class OpenClassPrivateGA {} // expected-error {{open class 'OpenClassPrivateGA' cannot have private global actor 'PrivateGA'}}
@InternalGA open class OpenClassInternalGA {} // expected-error {{open class 'OpenClassInternalGA' cannot have internal global actor 'InternalGA'}}
@PackageGA open class OpenClassPackageGA {} // expected-error {{open class 'OpenClassPackageGA' cannot have package global actor 'PackageGA'}}
@PublicGA open class OpenClassPublicGA {}

View File

@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -typecheck %s -verify
// RUN: %target-swift-frontend -typecheck %s -verify -package-name myPkg
// MARK: - Correct type eraser
@@ -64,6 +64,18 @@ class MoreRestrictive: B6 { // expected-note {{type eraser declared here}}
@_typeEraser(MoreRestrictive) // expected-error {{internal type eraser 'MoreRestrictive' cannot have more restrictive access than protocol 'B6' (which is public)}}
public protocol B6 {}
class MoreRestrictive1: B66 { // expected-note {{type eraser declared here}}
init<T: B66>(erasing t: T) {}
}
@_typeEraser(MoreRestrictive1) // expected-error {{internal type eraser 'MoreRestrictive1' cannot have more restrictive access than protocol 'B66' (which is package)}}
package protocol B66 {}
package class PkgMoreRestrictive: PB6 { // expected-note {{type eraser declared here}}
init<T: B6>(erasing t: T) {}
}
@_typeEraser(PkgMoreRestrictive) // expected-error {{package type eraser 'PkgMoreRestrictive' cannot have more restrictive access than protocol 'PB6' (which is public)}}
public protocol PB6 {}
typealias FnAlias = () -> Void
@_typeEraser(FnAlias) // expected-error {{type eraser must be a class, struct, or enum}}
protocol B7 {}
@@ -119,6 +131,20 @@ public class UnviableInits: E1 {
@_typeEraser(UnviableInits) // expected-error {{type eraser 'UnviableInits' has no viable initializer of the form 'init<T: E1>(erasing: T)'}}
public protocol E1 {}
public class UnviableInits11: E11 {
public init<T: E11>(erasing t: T) where T: Hashable {} // expected-note {{'init(erasing:)' cannot have unsatisfied requirements when 'T' = 'some E11'}}
package init<T: E11>(erasing t: T) {} // expected-note {{package 'init(erasing:)' cannot have more restrictive access than protocol 'E11' (which is public)}}
}
@_typeEraser(UnviableInits11) // expected-error {{type eraser 'UnviableInits11' has no viable initializer of the form 'init<T: E11>(erasing: T)'}}
public protocol E11 {}
package class PkgUnviableInits: PE1 {
package init<T: PE1>(erasing t: T) where T: Hashable {} // expected-note {{'init(erasing:)' cannot have unsatisfied requirements when 'T' = 'some PE1'}}
init<T: PE1>(erasing t: T) {} // expected-note {{internal 'init(erasing:)' cannot have more restrictive access than protocol 'PE1' (which is package)}}
}
@_typeEraser(PkgUnviableInits) // expected-error {{type eraser 'PkgUnviableInits' has no viable initializer of the form 'init<T: PE1>(erasing: T)'}}
package protocol PE1 {}
class FailableInit: E2 {
init?<T: E2>(erasing t: T) {} // expected-note {{'init(erasing:)' cannot be failable}}
}

View File

@@ -1,16 +1,30 @@
// RUN: %empty-directory(%t)
// Package name should have valid characters
// RUN: not %target-swift-frontend -module-name Logging -package-name My-Logging%Pkg %s -emit-module -emit-module-path %t/Logging.swiftmodule 2>&1 | %FileCheck %s -check-prefix CHECK-BAD
// RUN: not --crash %target-swift-frontend -module-name Logging -package-name My-Logging%Pkg %s -emit-module -emit-module-path %t/Logging.swiftmodule 2> %t/resultA.output
// RUN: %FileCheck %s -input-file %t/resultA.output -check-prefix CHECK-BAD
// CHECK-BAD: package name "My-Logging%Pkg" is not a valid identifier
// CHECK-BAD: decl has a package access level but no -package-name was passed
// CHECK-BAD: non-public decl has no formal access scope
// Package name cannot be a standard library name
// RUN: not %target-swift-frontend -module-name Logging -package-name Swift %s -emit-module -emit-module-path %t/Logging.swiftmodule 2>&1 | %FileCheck %s -check-prefix CHECK-STDLIB
// CHECK-STDLIB: package name "Swift" is reserved for the standard library
// Package name should not be empty
// RUN: not --crash %target-swift-frontend -typecheck %s -package-name "" 2>&1 | %FileCheck %s -check-prefix CHECK-EMPTY
// CHECK-EMPTY: package name "" is not a valid identifier
// CHECK-EMPTY: decl has a package access level but no -package-name was passed
// CHECK-EMPTY: non-public decl has no formal access scope
// If package access level is used but no package-name is passed, it should error
// RUN: not --crash %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s -check-prefix CHECK-MISSING
// CHECK-MISSING: decl has a package access level but no -package-name was passed
// CHECK-MISSING: non-public decl has no formal access scope
// Package name can be same as the module name
// RUN: %target-swift-frontend -module-name Logging -package-name Logging %s -emit-module -emit-module-path %t/Logging.swiftmodule
// RUN: test -f %t/Logging.swiftmodule
public func log() {}
// Package name can be a standard library name
// RUN: %target-swift-frontend -module-name Logging -package-name Swift %s -emit-module -emit-module-path %t/Logging.swiftmodule
// RUN: test -f %t/Logging.swiftmodule
package func log() {}