|
| 1 | +# Scope and introduction |
| 2 | + |
| 3 | +This document defines the policy for applying access control modifiers |
| 4 | +and related naming conventions for the Swift standard library and |
| 5 | +overlays. |
| 6 | + |
| 7 | +In this document, \"stdlib\" refers to the core standard library and |
| 8 | +overlays for system frameworks written in Swift. |
| 9 | + |
| 10 | +Swift has four levels of access control — `private`, `fileprivate`, `internal` |
| 11 | +and `public`. As currently implemented, access control is only concerned with |
| 12 | +API-level issues, not ABI. The stdlib does not have a stable ABI, and is |
| 13 | +compiled in \"non-resilient\" mode with inlining into user code; thus, |
| 14 | +all stdlib symbols are considered ABI and stdlib clients should be |
| 15 | +recompiled after *any* change to the stdlib. |
| 16 | + |
| 17 | +# `public` |
| 18 | + |
| 19 | +User-visible APIs should be marked public. |
| 20 | + |
| 21 | +Unfortunately, the compiler has bugs and limitations that the stdlib |
| 22 | +must work around by defining additional public symbols not intended for |
| 23 | +direct consumption by users. For example: |
| 24 | + |
| 25 | +```swift |
| 26 | +// Workaround. |
| 27 | +public protocol _Pointer { |
| 28 | + // ... |
| 29 | +} |
| 30 | + |
| 31 | +// Symbol intended for use outside stdlib. |
| 32 | +public struct UnsafeRawPointer: _Pointer { |
| 33 | + // ... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +These symbols are hidden using the [leading underscore |
| 38 | +rule](#leading-underscore-rule). |
| 39 | + |
| 40 | +Because Swift does not yet support a notion of SPI, any implementation |
| 41 | +details that are shared across the stdlib\'s various sub-modules must |
| 42 | +also be public. These names, too, use the [leading underscore |
| 43 | +rule](#leading-underscore-rule). |
| 44 | + |
| 45 | +To document the reason for marking symbols public, we use comments: |
| 46 | + |
| 47 | +- symbols used in tests: |
| 48 | + |
| 49 | + public // @testable |
| 50 | + func _foo() { ... } |
| 51 | + |
| 52 | +- symbols that are SPIs for the module X: |
| 53 | + |
| 54 | + public // SPI(X) |
| 55 | + func _foo() { ... } |
| 56 | + |
| 57 | +# `internal` |
| 58 | + |
| 59 | +In Swift, `internal` is an implied default everywhere — except within `public` |
| 60 | +extensions and protocols. Therefore, `internal` should be used explicitly |
| 61 | +everywhere in the stdlib to avoid confusion. |
| 62 | + |
| 63 | +### Note |
| 64 | + |
| 65 | +> No declaration should omit an access. |
| 66 | +
|
| 67 | +To create a \"single point of truth\" about whether a name is intended |
| 68 | +for user consumption, the following names should all use the [leading |
| 69 | +underscore rule](#leading-underscore-rule): |
| 70 | + |
| 71 | +- module-scope `private` and `internal` |
| 72 | + symbols: |
| 73 | + |
| 74 | +```swift |
| 75 | +var _internalStdlibConstant: Int { ... } |
| 76 | +``` |
| 77 | + |
| 78 | +- `private` and `internal` symbols nested within `public` types: |
| 79 | + |
| 80 | +```swift |
| 81 | +public struct Dictionary { |
| 82 | + var _representation: _DictionaryRepresentation |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +# `private` |
| 87 | + |
| 88 | +# Leading Underscore Rule |
| 89 | + |
| 90 | +Variables, functions and typealiases should have names that start with |
| 91 | +an underscore: |
| 92 | + |
| 93 | +```swift |
| 94 | +var _value: Int |
| 95 | +func _bridgeSomethingToAnything(_ something: AnyObject) -> AnyObject |
| 96 | +typealias _InternalTypealias = HeapBuffer<Int, Int> |
| 97 | +``` |
| 98 | + |
| 99 | +To apply the rule to an initializer, one of its label arguments *or* |
| 100 | +internal parameter names must start with an underscore: |
| 101 | + |
| 102 | +``` |
| 103 | +public struct Foo { |
| 104 | + init(_count: Int) {} |
| 105 | + init(_ _otherInitializer: Int) {} |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Note |
| 110 | + |
| 111 | +> The identifier that consists of a single underscore `_` is not |
| 112 | +considered to be a name that starts with an underscore. For example, |
| 113 | +this initializer is public: |
| 114 | + |
| 115 | +```swift |
| 116 | +public struct Foo { |
| 117 | + init(\_ count: Int) {} |
| 118 | + |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +The compiler and IDE tools may use the leading underscore rule, combined |
| 123 | +with additional heuristics, to hide stdlib symbols that users don\'t |
| 124 | +need to see. |
| 125 | + |
| 126 | +Users are prohibited to use leading underscores symbols in their own |
| 127 | +source code, even if these symbols are visible through compiler |
| 128 | +diagnostics or IDE tools. |
0 commit comments