Skip to content

Commit ccaf847

Browse files
authored
Add package access modifier [SE-0386] (#288)
Fixes: swiftlang/swift-book#215 Fixes: rdar://117171554
2 parents 8e8520d + 8781b5d commit ccaf847

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

TSPL.docc/LanguageGuide/AccessControl.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ you may not need to specify explicit access control levels at all.
2323
> (properties, types, functions, and so on)
2424
> are referred to as “entities” in the sections below, for brevity.
2525
26-
## Modules and Source Files
26+
## Modules, Source Files, and Packages
2727

28-
Swift's access control model is based on the concept of modules and source files.
28+
Swift's access control model is based on the concept of
29+
modules, source files, and packages.
2930

3031
A *module* is a single unit of code distribution ---
3132
a framework or application that's built and shipped as a single unit
@@ -44,17 +45,36 @@ A *source file* is a single Swift source code file within a module
4445
Although it's common to define individual types in separate source files,
4546
a single source file can contain definitions for multiple types, functions, and so on.
4647

48+
A *package* is a group of modules that you develop as a unit.
49+
You define the modules that form a package
50+
as part of configuring the build system you're using,
51+
not as part of your Swift source code.
52+
For example, if you use Swift Package Manager to build your code,
53+
you define a package in your `Package.swift` file
54+
using APIs from the [PackageDescription][] module,
55+
and if you use Xcode, you specify the package name
56+
in the Package Access Identifier build setting.
57+
58+
[PackageDescription](https://developer.apple.com/documentation/packagedescription)
59+
4760
## Access Levels
4861

49-
Swift provides five different *access levels* for entities within your code.
62+
Swift provides six different *access levels* for entities within your code.
5063
These access levels are relative to the source file in which an entity is defined,
51-
and also relative to the module that source file belongs to.
64+
the module that source file belongs to,
65+
and the package that the module belongs to.
5266

5367
- *Open access* and *public access*
5468
enable entities to be used within any source file from their defining module,
5569
and also in a source file from another module that imports the defining module.
5670
You typically use open or public access when specifying the public interface to a framework.
5771
The difference between open and public access is described below.
72+
- *Package access*
73+
enables entities to be used within
74+
any source files from their defining package
75+
but not in any source file outside of that package.
76+
You typically use package access
77+
within an app or framework that's structured into multiple modules.
5878
- *Internal access*
5979
enables entities to be used within any source file from their defining module,
6080
but not in any source file outside of that module.
@@ -790,14 +810,15 @@ the constant, variable, property, or subscript they belong to.
790810
You can give a setter a *lower* access level than its corresponding getter,
791811
to restrict the read-write scope of that variable, property, or subscript.
792812
You assign a lower access level by writing
793-
`fileprivate(set)`, `private(set)`, or `internal(set)`
813+
`fileprivate(set)`, `private(set)`, `internal(set)`, or `package(set)`
794814
before the `var` or `subscript` introducer.
795815

796816
> Note: This rule applies to stored properties as well as computed properties.
797817
> Even though you don't write an explicit getter and setter for a stored property,
798818
> Swift still synthesizes an implicit getter and setter for you
799819
> to provide access to the stored property's backing storage.
800-
> Use `fileprivate(set)`, `private(set)`, and `internal(set)` to change the access level
820+
> Use `fileprivate(set)`, `private(set)`, `internal(set)`, and `package(set)`
821+
> to change the access level
801822
> of this synthesized setter in exactly the same way as for an explicit setter
802823
> in a computed property.
803824

TSPL.docc/ReferenceManual/Attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ The compiler is allowed to replace calls to an inlinable symbol
938938
with a copy of the symbol's implementation at the call site.
939939

940940
Inlinable code
941-
can interact with `public` symbols declared in any module,
941+
can interact with `open` and `public` symbols declared in any module,
942942
and it can interact with `internal` symbols
943943
declared in the same module
944944
that are marked with the `usableFromInline` attribute.

TSPL.docc/ReferenceManual/Declarations.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,18 @@ Access control is discussed in detail in <doc:AccessControl>.
38793879
Declarations marked with the `public` access-level modifier can also be accessed (but not subclassed)
38803880
by code in a module that imports the module that contains that declaration.
38813881

3882+
- term `package`:
3883+
Apply this modifier to a declaration
3884+
to indicate that the declaration can be accessed
3885+
only by code in the same package as the declaration.
3886+
A package is a unit of code distribution
3887+
that you define in the build system you're using.
3888+
When the build system compiles code,
3889+
it specifies the package name
3890+
by passing the `-package-name` flag to the Swift compiler.
3891+
Two modules are part of the same package
3892+
if the build system specifies the same package name when building them.
3893+
38823894
- term `internal`:
38833895
Apply this modifier to a declaration to indicate the declaration can be accessed
38843896
only by code in the same module as the declaration.
@@ -3921,6 +3933,7 @@ as discussed in <doc:AccessControl#Getters-and-Setters>.
39213933
> *access-level-modifier***`private`** | **`private`** **`(`** **`set`** **`)`** \
39223934
> *access-level-modifier***`fileprivate`** | **`fileprivate`** **`(`** **`set`** **`)`** \
39233935
> *access-level-modifier***`internal`** | **`internal`** **`(`** **`set`** **`)`** \
3936+
> *access-level-modifier***`package`** | **`package`** **`(`** **`set`** **`)`** \
39243937
> *access-level-modifier***`public`** | **`public`** **`(`** **`set`** **`)`** \
39253938
> *access-level-modifier***`open`** | **`open`** **`(`** **`set`** **`)`**
39263939
>

TSPL.docc/ReferenceManual/LexicalStructure.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ so they must be escaped with backticks in that context.
384384
`nonmutating`,
385385
`optional`,
386386
`override`,
387+
`package`,
387388
`postfix`,
388389
`precedence`,
389390
`prefix`,

TSPL.docc/ReferenceManual/SummaryOfTheGrammar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ make the same change here also.
997997
> *access-level-modifier***`private`** | **`private`** **`(`** **`set`** **`)`** \
998998
> *access-level-modifier***`fileprivate`** | **`fileprivate`** **`(`** **`set`** **`)`** \
999999
> *access-level-modifier***`internal`** | **`internal`** **`(`** **`set`** **`)`** \
1000+
> *access-level-modifier***`package`** | **`package`** **`(`** **`set`** **`)`** \
10001001
> *access-level-modifier***`public`** | **`public`** **`(`** **`set`** **`)`** \
10011002
> *access-level-modifier***`open`** | **`open`** **`(`** **`set`** **`)`**
10021003
>

0 commit comments

Comments
 (0)