Skip to content

Commit 48da44e

Browse files
authored
Merge pull request #4343 from jrose-apple/doc-updates
[docs] Update LibraryEvolution and Lexicon
2 parents be5636a + 5e1a3c3 commit 48da44e

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

docs/Lexicon.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ source code, tests, and commit messages. See also the `LLVM lexicon`_.
8989
different order, or when header files are modified to use forward
9090
declarations instead of direct includes.
9191

92+
LGTM
93+
"Looks good to me." Used in code review to indicate approval with no further
94+
comments.
95+
9296
main module
9397
The module for the file or files currently being compiled.
9498

docs/LibraryEvolution.rst

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
:Author: Jordan Rose
77
:Author: John McCall
88

9+
.. note::
10+
11+
This document uses some Sphinx-specific features which are not available on
12+
GitHub. For proper rendering, download and build the docs yourself. Jordan
13+
Rose also posts occasional snapshots at
14+
https://jrose-apple.github.io/swift-library-evolution/.
15+
916
One of Swift's primary design goals is to allow efficient execution of code
1017
without sacrificing load-time abstraction of implementation.
1118

@@ -167,7 +174,7 @@ document.
167174
Publishing Versioned API
168175
========================
169176

170-
A library's API is already marked with the ``public`` attribute, but if a
177+
A library's API is already marked with the ``public`` modifier, but if a
171178
client wants to work with multiple releases of the library, the API needs
172179
versioning information as well. A *versioned entity* represents anything with a
173180
runtime presence that a client may rely on; its version records when the entity
@@ -182,9 +189,12 @@ version of the library where the entity may be used.
182189
See `New Conformances`_, below.
183190

184191
In a versioned library, any top-level public entity from the list above may not
185-
be made ``public`` without an appropriate version. A public entity declared
186-
within a versioned type (or an extension of a versioned type) will default to
187-
having the same version as the type.
192+
be made ``public`` (or ``open``) without an appropriate version. A public
193+
entity declared within a versioned type (or an extension of a versioned type)
194+
will default to having the same version as the type.
195+
196+
In this document, the term "public" includes classes and members marked
197+
``open``.
188198

189199
Code within a library may generally use all other entities declared within the
190200
library (barring their own availability checks), since the entire library is
@@ -286,8 +296,6 @@ The following changes are permitted:
286296
- Adding a default argument expression to a parameter.
287297
- Changing or removing a default argument is a `binary-compatible
288298
source-breaking change`.
289-
- The ``@noreturn`` attribute may be added to a function. ``@noreturn`` is a
290-
`versioned attribute`.
291299
- The ``@discardableResult`` and ``@warn_unqualified_access`` attributes may
292300
be added to a function without any additional versioning information.
293301

@@ -299,7 +307,6 @@ No other changes are permitted; the following are particularly of note:
299307
- A versioned function may not add, remove, or reorder parameters, whether or
300308
not they have default arguments.
301309
- A versioned function that throws may not become non-throwing or vice versa.
302-
- ``@noreturn`` may not be removed from a function.
303310
- The ``@noescape`` attribute may not be added to or removed from a parameter.
304311
It is not a `versioned attribute` and so there is no way to guarantee that it
305312
is safe when a client deploys against older versions of the library.
@@ -952,16 +959,17 @@ Omitted from this list is the free addition of new members. Here classes are a
952959
little more restrictive than structs; they only allow the following changes:
953960

954961
- Adding a new convenience initializer.
955-
- Adding a new designated initializer, if the class is not publicly
956-
subclassable.
962+
- Adding a new designated initializer, if the class is not ``open``.
957963
- Adding a deinitializer.
958964
- Adding new, non-overriding method, subscript, or property.
959-
- Adding a new overriding member, though if the class is publicly-subclassable
960-
the type of the member may not deviate from the member it overrides.
961-
Changing the type could be incompatible with existing overrides in subclasses.
965+
- Adding a new overriding member, though if the class is ``open`` the type of
966+
the member may not deviate from the member it overrides. Changing the type
967+
could be incompatible with existing overrides in subclasses.
962968

963969
Finally, classes allow the following changes that do not apply to structs:
964970

971+
- A class may be marked ``open`` if it is not already marked ``final``.
972+
- A class may be marked ``final`` if it is not already marked ``open``.
965973
- Removing an explicit deinitializer. (A class with no declared deinitializer
966974
effectively has an implicit deinitializer.)
967975
- "Moving" a method, subscript, or property up to its superclass. The
@@ -972,8 +980,10 @@ Finally, classes allow the following changes that do not apply to structs:
972980
removed as long as the generic parameters, formal parameters, and return type
973981
*exactly* match the overridden declaration. Any existing callers should
974982
automatically use the superclass implementation.
975-
- ``@noreturn`` may be only added to a method if it is not publicly
976-
overridable.
983+
- Within an ``open`` class, any public method, subscript, or property may be
984+
marked ``open`` if it is not already marked ``final``.
985+
- Any public method, subscript, or property may be marked ``final`` if it is not
986+
already marked ``open``.
977987
- ``@IBOutlet``, ``@IBAction``, and ``@IBInspectable`` may be added to a member
978988
without providing any extra version information. Removing any of these is
979989
a `binary-compatible source-breaking change` if the member remains ``@objc``,
@@ -996,14 +1006,21 @@ Finally, classes allow the following changes that do not apply to structs:
9961006
NSCollectionViewItem be a subclass of NSResponder or not? How would the
9971007
compiler be able to enforce this?
9981008

1009+
.. admonition:: TODO
1010+
1011+
Both ``final`` and ``open`` may be applied to a declaration after it has
1012+
been made public. However, these need to be treated as
1013+
`versioned attributes <versioned attribute>`. It's not clear what syntax
1014+
should be used for this.
1015+
9991016
.. _NSCollectionViewItem: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSCollectionViewItem_Class/index.html
10001017

10011018
Other than those detailed above, no other changes to a class or its members
10021019
are permitted. In particular:
10031020

1004-
- ``final`` may not be added to *or* removed from a class or any of its members.
1005-
The presence of ``final`` enables optimization; its absence means there may
1006-
be subclasses/overrides that would be broken by the change.
1021+
- ``open`` may not be removed from a class or its members.
1022+
- ``final`` may not be removed from a class or its members. (The presence of
1023+
``final`` enables optimization.)
10071024
- ``dynamic`` may not be added to *or* removed from any members. Existing
10081025
clients would not know to invoke the member dynamically.
10091026
- A ``final`` override of a member may *not* be removed, even if the type
@@ -1013,9 +1030,6 @@ are permitted. In particular:
10131030
any existing members.
10141031
- ``@NSManaged`` may not be added to or removed from any existing members.
10151032

1016-
.. note:: These restrictions tie in with the ongoing discussions about
1017-
"``final``-by-default" and "non-publicly-subclassable-by-default".
1018-
10191033
.. admonition:: TODO
10201034

10211035
The ``@NSManaged`` attribute as it is in Swift 2 exposes implementation
@@ -1026,10 +1040,10 @@ are permitted. In particular:
10261040
Initializers
10271041
------------
10281042

1029-
New designated initializers may not be added to a publicly-subclassable class.
1030-
This would change the inheritance of convenience initializers, which existing
1031-
subclasses may depend on. A publicly-subclassable class also may not change
1032-
a convenience initializer into a designated initializer or vice versa.
1043+
New designated initializers may not be added to an ``open`` class. This would
1044+
change the inheritance of convenience initializers, which existing subclasses
1045+
may depend on. An ``open`` class also may not change a convenience initializer
1046+
into a designated initializer or vice versa.
10331047

10341048
A new ``required`` initializer may be added to a class only if it is a
10351049
convenience initializer; that initializer may only call existing ``required``
@@ -1054,9 +1068,6 @@ top-level functions, but the potential for overrides complicates things a little
10541068
- Adding a default argument expression to a parameter.
10551069
- Changing or removing a default argument is a `binary-compatible
10561070
source-breaking change`.
1057-
- The ``@noreturn`` attribute may be added to a public method only if it is
1058-
``final`` or the class is not publicly subclassable. ``@noreturn`` is a
1059-
`versioned attribute`.
10601071
- The ``@discardableResult`` and ``@warn_unqualified_access`` attributes may
10611072
be added to a method without any additional versioning information.
10621073

@@ -1069,9 +1080,6 @@ If an inlineable method is overridden, the overriding method does not need to
10691080
also be inlineable. Clients may only inline a method when they can devirtualize
10701081
the call. (This does permit speculative devirtualization.)
10711082

1072-
Any method that overrides a ``@noreturn`` method must also be marked
1073-
``@noreturn``.
1074-
10751083

10761084
Properties
10771085
----------
@@ -1080,8 +1088,7 @@ Class and instance properties allow *most* of the modifications permitted for
10801088
struct properties, but the potential for overrides complicates things a little.
10811089
Variable properties (those declared with ``var``) allow the following changes:
10821090

1083-
- Adding (but not removing) a computed setter to a ``final`` property or a
1084-
property in a non-publicly-subclassable class.
1091+
- Adding (but not removing) a computed setter to a non-``open`` property.
10851092
- Adding or removing a non-public, non-versioned setter.
10861093
- Changing from a stored property to a computed property, or vice versa, as
10871094
long as a previously versioned setter is not removed.
@@ -1094,7 +1101,7 @@ Variable properties (those declared with ``var``) allow the following changes:
10941101
- Adding or removing ``unowned`` from a variable.
10951102
- Adding or removing ``@NSCopying`` to/from a variable.
10961103

1097-
Adding a public setter to a computed property that may be overridden is a
1104+
Adding a public setter to an ``open`` property is a
10981105
`binary-compatible source-breaking change`; any existing overrides will not
10991106
know what to do with the setter and will likely not behave correctly.
11001107

@@ -1117,12 +1124,12 @@ Subscripts
11171124
Subscripts behave much like properties; they inherit the rules of their struct
11181125
counterparts with a few small changes:
11191126

1120-
- Adding (but not removing) a public setter to a ``final`` subscript or a
1121-
subscript is permitted in a non-publicly-subclassable class.
1127+
- Adding (but not removing) a public setter to a non-``open`` subscript is
1128+
permitted.
11221129
- Adding or removing a non-public, non-versioned setter is permitted.
11231130
- Changing the body of an accessor is permitted.
11241131

1125-
Adding a public setter to a subscript that may be overridden is a
1132+
Adding a public setter to an ``open`` subscript is a
11261133
`binary-compatible source-breaking change`; any existing overrides will not
11271134
know what to do with the setter and will likely not behave correctly.
11281135

@@ -1245,28 +1252,28 @@ multiple kinds of flexibility.
12451252
Versioning Internal Declarations
12461253
================================
12471254

1248-
The initial discussion on versioning focused on ``public`` APIs, making sure
1255+
The initial discussion on versioning focused on public APIs, making sure
12491256
that a client knows what features they can use when a specific version of a
12501257
library is present. Inlineable functions have much the same constraints, except
12511258
the inlineable function is the client and the entities being used may not be
1252-
``public``.
1259+
public.
12531260

12541261
Adding a versioning annotation to an ``internal`` entity promises that the
12551262
entity will be available at link time in the containing module's binary. This
12561263
makes it safe to refer to such an entity from an inlineable function. If the
1257-
entity is ever made ``public``, its availability should not be changed; not
1258-
only is it safe for new clients to rely on it, but *existing* clients require
1259-
its presence as well.
1264+
entity is ever made ``public`` or ``open``, its availability should not be
1265+
changed; not only is it safe for new clients to rely on it, but *existing*
1266+
clients require its presence as well.
12601267

12611268
.. note::
12621269

12631270
Why isn't this a special form of ``public``? Because we don't want it to
12641271
imply everything that ``public`` does, such as requiring overrides to be
12651272
``public``.
12661273

1267-
Because a versioned class member may eventually be made ``public``, it must be
1268-
assumed that new overrides may eventually appear from outside the module unless
1269-
the member is marked ``final`` or the class is not publicly subclassable.
1274+
Because a versioned class member may eventually be made ``open``, it must be
1275+
assumed that new overrides may eventually appear from outside the module if the
1276+
class is marked ``open`` unless the member is marked ``final``.
12701277

12711278
Non-public conformances are never considered versioned, even if both the
12721279
conforming type and the protocol are versioned. A conformance is considered
@@ -1611,8 +1618,8 @@ of ``Summonable`` from the base class, but even that may not be possible if
16111618
there are incompatible associated types involved (because changing a member
16121619
typealias is not a safe change).
16131620

1614-
One solution is to disallow adding a conformance for an existing protocol to a
1615-
publicly-subclassable class.
1621+
One solution is to disallow adding a conformance for an existing protocol to an
1622+
``open`` class.
16161623

16171624

16181625
Recompiling changes a protocol's implementation
@@ -1689,10 +1696,10 @@ document that affect language semantics:
16891696

16901697
- `SE-0030 Property Behaviors`_
16911698
- (draft) `Overridable methods in extensions`_
1692-
- (planned) Making classes "sealed" by default
1699+
- `SE-0117 Allow Allow distinguishing between public access and public overridability <SE-0117>`_
16931700
- (planned) Restricting retroactive modeling (protocol conformances for types you don't own)
16941701
- (planned) Default implementations in protocols
1695-
- (planned) Generalized existentials (values of protocol type)
1702+
- (planned) `Generalized existentials (values of protocol type) <Generics>`_
16961703
- (planned) Open and closed enums
16971704
- (planned) Removing the "constant" guarantee for 'let' across module boundaries
16981705
- (planned) Syntax for declaring "versioned" entities and their features
@@ -1705,6 +1712,8 @@ document that affect language semantics:
17051712

17061713
.. _SE-0030 Property Behaviors: https://github.com/apple/swift-evolution/blob/master/proposals/0030-property-behavior-decls.md
17071714
.. _Overridable methods in extensions: https://github.com/jrose-apple/swift-evolution/blob/overridable-members-in-extensions/proposals/nnnn-overridable-members-in-extensions.md
1715+
.. _SE-0117: https://github.com/apple/swift-evolution/blob/master/proposals/0117-non-public-subclassable-by-default.md
1716+
.. _Generics: https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials
17081717

17091718
This does not mean all of these proposals need to be accepted, only that their
17101719
acceptance or rejection will affect this document.
@@ -1723,7 +1732,7 @@ Glossary
17231732
API
17241733
An `entity` in a library that a `client` may use, or the collection of all
17251734
such entities in a library. (If contrasting with `SPI`, only those entities
1726-
that are available to arbitrary clients.) Marked ``public`` in
1735+
that are available to arbitrary clients.) Marked ``public`` or ``open`` in
17271736
Swift. Stands for "Application Programming Interface".
17281737

17291738
availability context

0 commit comments

Comments
 (0)