File: accessibility_package.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (115 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -verify -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 -verify -module-name LibGood %t/LibGood.swift -emit-module -emit-module-path %t/LibGood.swiftmodule -package-name myLib -I %t
// RUN: test -f %t/LibGood.swiftmodule

// RUN: %target-swift-frontend -verify -module-name Client %t/Client.swift -emit-module -emit-module-path %t/Client.swiftmodule -package-name client -I %t

// RUN: %target-swift-frontend -typecheck -verify %t/LibSamePackage.swift -package-name myLib -I %t
// RUN: %target-swift-frontend -typecheck -verify %t/LibOtherPackage.swift -package-name "otherLib" -I %t

//--- Utils.swift
package protocol PackageProto {
  var pkgVar: Double { get set }
  func pkgFunc()
}

package class PackageKlass {
  package init() {}
  package private(set) var pkgVar: Double = 1.0
  package func pkgFunc() {}
}

class InternalKlass {}

public class PublicKlass {
  public init() {}
  public var publicVar: Int = 1
  public package(set) var publicGetPkg: Int = 2
  public internal(set) var publicGetInternal: Int = 3
  public func publicFunc() {}
  package func pkgFunc() {}
}

open class OpenKlass {
  public init() {}
  open var openVar: String = ""
  open func openFunc() {}
  public func publicFunc() {}
  package func packageFunc() {}
}

//--- LibSamePackage.swift
import Utils

// Test accessing public and package decls
public func test() {
  let x = PublicKlass()
  x.publicFunc()
  x.pkgFunc() // OK
  x.publicGetPkg = 3 // OK
  x.publicGetInternal = 4 // expected-error {{cannot assign to property: 'publicGetInternal' setter is inaccessible}}

  let y = PackageKlass() // OK
  y.pkgVar = 1.5  // expected-error {{cannot assign to property: 'pkgVar' setter is inaccessible}}
  y.pkgFunc() // OK
}

// Test conformance to a package protocol
package struct LibStruct : PackageProto { // OK
  package var pkgVar: Double = 1.0
  package func pkgFunc() {}
}

// Test subclassing / overrides
class SubOpenKlass: OpenKlass {
  override open func openFunc() {}
  override public func publicFunc() {} // expected-error {{overriding non-open instance method outside of its defining module}}
  override package func packageFunc() {} // expected-error {{overriding non-open instance method outside of its defining module}}
}
class SubPublicKlass: PublicKlass {} // expected-error {{cannot inherit from non-open class 'PublicKlass' outside of its defining module}}
class SubPackageKlass: PackageKlass {} // expected-error {{cannot inherit from non-open class 'PackageKlass' outside of its defining module}}


//--- LibOtherPackage.swift
import Utils

// Test accessing package decls
public func test() {
  let x = PublicKlass()
  x.publicFunc() // OK
  x.pkgFunc() // expected-error {{'pkgFunc' is inaccessible due to 'package' protection level}}
  let y = PackageKlass() // expected-error {{cannot find 'PackageKlass' in scope}}
}

package struct LibStruct : PackageProto {} // expected-error {{cannot find type 'PackageProto' in scope}}

//--- LibGood.swift
import Utils

public func libFunc() {
  _ = LibStruct()
}

public struct LibStruct: PackageProto {
  public init() {}
  package var pkgVar: Double = 1.0
  package func pkgFunc() {}
  public var publicVar: String = ""
  public func publicFunc() {}
}

//--- Client.swift
import LibGood

func clientFunc() {
  let lib = LibStruct()
  _ = lib.pkgVar // expected-error {{'pkgVar' is inaccessible due to 'package' protection level}}
  _ = lib.publicVar
  lib.pkgFunc() // expected-error {{'pkgFunc' is inaccessible due to 'package' protection level}}
  lib.publicFunc()
}