@@ -15,21 +15,33 @@ Previously pitched in:
15
15
16
16
> ** Differences to previous proposals**
17
17
18
- > This proposal expands on the previous proposals and incorperates the language
18
+ > This proposal expands on the previous proposals and incorporates the language
19
19
> steering groups feedback of exploring language features to solve the
20
- > motivating problem. It also provides a migration path for existing modules.
20
+ > motivating problem. It also reuses the existing ` @frozen ` and documents a
21
+ > migration path for existing modules.
22
+
23
+ Revisions:
24
+ - Introduced a second annotation ` @nonExtensible ` to allow a migration path into
25
+ both directions
26
+ - Added future directions for adding additional associated values
27
+ - Removed both the ` @extensible ` and ` @nonExtensible ` annotation in favour of
28
+ re-using the existing ` @frozen ` annotation
29
+ - Added future directions for exhaustive matching for larger compilation units
30
+ - Added alternatives considered section for a hypothetical
31
+ ` @preEnumExtensibility `
32
+ - Added a section for ` swift package diagnose-api-breaking-changes `
21
33
22
34
## Introduction
23
35
24
- This proposal addresses the long standing behavioural difference of ` enum ` s in
36
+ This proposal addresses the long standing behavioral difference of ` enum ` s in
25
37
Swift modules compiled with and without library evolution. This makes Swift
26
38
` enum ` s vastly more useful in public API of non-resilient Swift libraries.
27
39
28
40
## Motivation
29
41
30
42
When Swift was enhanced to add support for "library evolution" mode (henceforth
31
43
called "resilient" mode), the Swift project had to make a number of changes to
32
- support a movable scale between "maximally evolveable " and "maximally
44
+ support a movable scale between "maximally evolvable " and "maximally
33
45
performant". This is because it is necessary for an ABI stable library to be
34
46
able to add new features and API surface without breaking pre-existing compiled
35
47
binaries. While by-and-large this was done without introducing feature
@@ -42,9 +54,9 @@ evolving SE-0192, and this proposal would aim to address it.
42
54
enumerations. It has a wide ranging number of effects, including exposing their
43
55
size directly as part of the ABI and providing direct access to stored
44
56
properties. However, on enumerations it happens to also exert effects on the
45
- behaviour of switch statements.
57
+ behavior of switch statements.
46
58
47
- Consider the following simple library to your favourite pizza place:
59
+ Consider the following simple library to your favorite pizza place:
48
60
49
61
``` swift
50
62
public enum PizzaFlavor {
@@ -58,7 +70,7 @@ public func bakePizza(flavor: PizzaFlavor)
58
70
59
71
Depending on whether the library is compiled with library evolution mode
60
72
enabled, what the caller can do with the ` PizzaFlavor ` enum varies. Specifically,
61
- the behaviour in switch statements changes.
73
+ the behavior in switch statements changes.
62
74
63
75
In the _ standard_ , "non-resilient" mode, users of the library can write
64
76
exhaustive switch statements over the enum ` PizzaFlavor ` :
@@ -78,7 +90,7 @@ case .cheese:
78
90
This code will happily compile. If the author of the above switch statement was
79
91
missing a case (perhaps they forgot ` .hawaiian ` is a flavor), the compiler will
80
92
error, and force the user to either add a ` default: ` clause, or to express a
81
- behaviour for the missing case. The term for this is "exhaustiveness": in the
93
+ behavior for the missing case. The term for this is "exhaustiveness": in the
82
94
default "non-resilient" dialect, the Swift compiler will ensure that all switch
83
95
statements over enumerations cover every case that is present.
84
96
@@ -117,10 +129,10 @@ wants to improve the performance of using it, the author can annotate the enum
117
129
with ` @frozen ` . This annotation has a wide range of effects, but one of its
118
130
effects is to enable callers to perform exhaustive switches over the frozen
119
131
enumeration. Thus, resilient library authors that are interested in the
120
- exhaustive switching behaviour are able to opt-into it.
132
+ exhaustive switching behavior are able to opt-into it.
121
133
122
134
However, in Swift today it is not possible for the default, "non-resilient"
123
- dialect to opt-in to the extensible enumeration behaviour . That is, there is no
135
+ dialect to opt-in to the extensible enumeration behavior . That is, there is no
124
136
way for a Swift package to be able to evolve a public enumeration without
125
137
breaking the API. This is a substantial limitation, and greatly reduces the
126
138
utility of enumerations in non-resilient Swift. Over the past years, many
@@ -146,114 +158,119 @@ non-resilient Swift.
146
158
147
159
## Proposed solution
148
160
161
+ With the following proposed solution we want to achieve the following goals:
162
+ 1 . Align the differences between the two language dialects in a future language
163
+ mode
164
+ 2 . Provide developers a path to opt-in to the new behavior before the new
165
+ language mode so they can start declaring ** new** extensible enumerations
166
+ 3 . Provide a migration path to the new behavior without forcing new SemVer
167
+ majors
168
+
149
169
We propose to introduce a new language feature ` ExtensibleEnums ` that aligns the
150
- behaviour of enumerations in both language dialects. This will make ** public**
170
+ behavior of enumerations in both language dialects. This will make ** public**
151
171
enumerations in packages a safe default and leave maintainers the choice of
152
- extending them later on. We also propose to enable this new language feature by
153
- default with the next lagnuage mode.
154
-
155
- We also propose to introduce two new attributes.
156
- - ` @nonExtensible ` : For marking an enumeration as not extensible.
157
- - ` @extensible ` : For marking an enumeration as extensible.
172
+ extending them later on. This language feature will become enabled by default in
173
+ the next language mode.
158
174
159
175
Modules consuming other modules with the language feature enabled will be
160
- required to add an ` @unknown default: ` case to any switch state for enumerations
161
- that are not marked with ` @nonExtensible ` .
176
+ required to add an ` @unknown default: ` .
162
177
163
178
An example of using the language feature and the keywords is below:
164
179
165
180
``` swift
166
181
/// Module A
167
- @extensible // or language feature ExtensibleEnums is enabled
168
- enum MyEnum {
169
- case foo
170
- case bar
171
- }
172
-
173
- @nonExtensible
174
- enum MyFinalEnum {
175
- case justMe
182
+ public enum PizzaFlavor {
183
+ case hawaiian
184
+ case pepperoni
185
+ case cheese
176
186
}
177
187
178
188
/// Module B
179
- switch myEnum { // error: Switch covers known cases, but 'MyEnum' may have additional unknown values, possibly added in future versions
180
- case .foo : break
181
- case . bar : break
182
- }
183
-
184
- // The below produces no warnings since the enum is marked as nonExtensible
185
- switch myFinalEnum {
186
- case . justMe : break
189
+ switch pizzaFlavor { // error: Switch covers known cases, but 'MyEnum' may have additional unknown values, possibly added in future versions
190
+ case .hawaiian :
191
+ throw BadFlavorError ()
192
+ case . pepperoni :
193
+ try validateNoVegetariansEating ()
194
+ return . delicious
195
+ case . cheese :
196
+ return . delicious
187
197
}
188
198
```
189
199
190
- ## Detailed design
191
-
192
- ### Migration path
200
+ Additionally, we propose to re-use the existing ` @frozen ` annotation to allow
201
+ developers to mark enumerations as non-extensible in non-resilient modules
202
+ similar to how it works in resilient modules already.
193
203
194
- The proposed new language feature is the first langauge feature that has impact
195
- on the consumers of a module and not the module itself. Enabling the langauge
196
- feature in a non-resilient module with public enumerations is a source breaking
197
- change.
198
-
199
- The two proposed annotations ` @extensible/@nonExtensible ` give developers tools
200
- to opt-in to the new language feature or in the future language mode without
201
- breaking their consumers. This paves a path for a gradual migration. Developers
202
- can mark all of their exisiting public enumerations as ` @nonExtensible ` and then
203
- turn on the language feature. Similarly, developers can also mark new
204
- enumerations as ` @extensible ` without turning on the language feature yet.
205
-
206
- In a future language mode, individual modules can still be opted in one at a
207
- time into the new language mode and apply the annotations as needed to avoid
208
- source breakages.
204
+ ``` swift
205
+ /// Module A
206
+ @frozen
207
+ public enum PizzaFlavor {
208
+ case hawaiian
209
+ case pepperoni
210
+ case cheese
211
+ }
209
212
210
- When the language feature is turned on and a public enumeration is marked as
211
- ` @extensible ` it will produce a warning that the annotation isn't required.
213
+ /// Module B
214
+ // The below doesn't require an `@unknown default` since PizzaFlavor is marked as frozen
215
+ switch pizzaFlavor {
216
+ case .hawaiian :
217
+ throw BadFlavorError ()
218
+ case .pepperoni :
219
+ try validateNoVegetariansEating ()
220
+ return .delicious
221
+ case .cheese :
222
+ return .delicious
223
+ }
224
+ ```
212
225
213
- In non-resilient modules without the language feature turned on, adding the
214
- ` @extensible ` attribute to non-public enums will produce a warning since those
215
- enums can only be matched exhaustively.
226
+ Turning on the new language feature will be a semantically breaking change for
227
+ consumers of their module; hence, requiring a new SemVer major release of the
228
+ containing package. Some packages can release a new major and adopt the new
229
+ language feature right away; however, the ecosystem also contains packages that
230
+ try to avoid breaking API if at all possible. Such packages are often at the
231
+ very bottom of the dependency graph e.g. ` swift-collections ` or ` swift-nio ` . If
232
+ any of such packages releases a new major version it would effectively split the
233
+ ecosystem until all packages have adopted the new major.
234
+
235
+ Packages that want to avoid breaking their API can use the new language feature
236
+ and the ` @frozen ` attribute in combination to unlock to possibility to declare
237
+ ** new extensible** public enumerations but stay committed to the non-extensible
238
+ API of the already existing public enumerations. This is achieved by marking all
239
+ existing public enumerations with ` @frozen ` before turning on the language
240
+ feature.
216
241
217
242
### Implications on code in the same package
218
243
219
244
Code inside the same package still needs to exhaustively switch over
220
- enumerations defined in the same package. Switches over enums of the same
221
- package containing an ` @unknown default ` will produce a compiler warning.
222
-
223
- ### Impact on resilient modules & ` @frozen ` attribute
245
+ enumerations defined in the same package when the language feature is enabled.
246
+ Switches over enums of the same package containing an ` @unknown default ` will
247
+ produce a compiler warning.
224
248
225
- Explicitly enabling the language feature in resilient modules will produce a
226
- compiler warning since that is already the default behaviour. Using the
227
- ` @nonExtensible ` annotation will lead to a compiler error since users of
228
- resilient modules must use the ` @frozen ` attribute instead.
249
+ ### API breaking checker
229
250
230
- Since some modules support compiling in resilient and non-resilient modes,
231
- developers need a way to mark enums as non-extensible for both. ` @nonExtensible `
232
- produces an error when compiling with resiliency; hence, developers must use
233
- ` @frozen ` . To make supporting both modes easier ` @frozen ` will also work in
234
- non-resilient modules and make enumerations non extensible.
251
+ The behavior of ` swift package diagnose-api-breaking-changes ` is also updated
252
+ to understand if the language feature is enabled and only diagnose new enum
253
+ cases as a breaking change in non-frozen enumerations.
235
254
236
255
## Source compatibility
237
256
238
- - Enabling the language feature ` ExtensibleEnums ` in a module that contains
239
- public enumerations is a source breaking change unless all existing public
240
- enumerations are marked with ` @nonExtensible `
241
- - Adding an ` @extensible ` annotation to an exisitng public enum is a source
242
- breaking change in modules that have ** not** enabled the ` ExtensibleEnums `
243
- language features or are compiled with resiliency.
244
- - Changing the annotation from ` @nonExtensible/@frozen ` to ` @extensible ` is a
245
- source breaking change.
246
- - Changing the annotation from ` @extensible ` to ` @nonExtensible/@frozen ` is a
247
- source compatible change and will only result in a warning code that used
248
- ` @unknown default: ` clause. This allows developers to commit to the API of an
249
- enum in a non-source breaking way.
257
+ - Enabling the language feature ` ExtensibleEnums ` in a module compiled without
258
+ resiliency that contains public enumerations is a source breaking change unless
259
+ all existing public enumerations are marked with ` @frozen `
260
+ - Disabling the language feature ` ExtensibleEnums ` in a module compiled without
261
+ resiliency is a source compatible change since it implicitly marks all
262
+ enumerations as ` @frozen `
263
+ - Adding a ` @frozen ` annotation to an existing public enumeration is a source
264
+ compatible change
250
265
251
266
## ABI compatibility
252
- The new attributes do not affect the ABI, as it is a no-op when used in a resilient library.
267
+
268
+ The new language feature dos not affect the ABI, as it is already how modules
269
+ compiled with resiliency behave.
253
270
254
271
## Future directions
255
272
256
- ### ` @unkown case `
273
+ ### ` @unknown case `
257
274
258
275
Enums can be used for errors. Catching and pattern matching enums could add
259
276
support for an ` @unknown catch ` to make pattern matching of typed throws align
@@ -263,25 +280,57 @@ with `switch` pattern matching.
263
280
264
281
Adding additional associated values to an enum can also be seen as extending it
265
282
and we agree that this is interesting to explore in the future. However, this
266
- proposal focuses on solving the primary problem of the unusability of public
283
+ proposal focuses on solving the primary problem of the usability of public
267
284
enumerations in non-resilient modules.
268
285
286
+ ### Larger compilation units than packages
287
+
288
+ During the pitch it was brought up that a common pattern for application
289
+ developers is to split an application into multiple smaller packages. Those
290
+ packages are versioned together and want to have the same exhaustive matching
291
+ behavior as code within a single package. As a future direction, build and
292
+ package tooling could allow to define larger compilation units to express this.
293
+ Until then developers are encouraged to use ` @frozen ` attributes on their
294
+ enumerations to achieve the same effect.
295
+
296
+ ### Swift PM allowing multiple conflicting major versions in a single dependency graph
297
+
298
+ To reduce the impact of an API break on the larger ecosystem Swift PM could
299
+ allow multiple conflicting major versions of the same dependency in a single
300
+ dependency graph. This would allow a package to adopt the new language feature,
301
+ break their existing, and release a new major while having minimal impact on
302
+ the larger ecosystem.
303
+
269
304
## Alternatives considered
270
305
271
- ### Only provide the ` @extensible ` annotation
306
+ ### Provide an ` @extensible ` annotation
272
307
273
- We believe that the default behaviour in both language dialects should be that
308
+ We believe that the default behavior in both language dialects should be that
274
309
public enumerations are extensible. One of Swift's goals, is safe defaults and
275
310
the current non-extensible default in non-resilient modules doesn't achieve that
276
311
goal. That's why we propose a new language feature to change the default in a
277
312
future Swift language mode.
278
313
279
- ### Usign ` @frozen ` and introducing ` @nonFrozen `
280
-
281
- We considered names such as ` @nonFrozen ` for ` @extensible ` and using ` @frozen `
282
- for ` @nonExtensible ` ; however, we believe that _ frozen_ is a concept that
283
- includes more than exhaustive matching. It is heavily tied to resiliency and
284
- also has ABI impact. That's why decoupled annotations that only focus on the
285
- extensability is better suited. ` @exhaustive/@nonExhaustive ` would fit that bill
286
- as well but we believe that ` @extensible ` better expresses the intention of the
287
- author.
314
+ ### Introducing a new annotation instead of using ` @frozen `
315
+
316
+ An initial pitch proposed an new annotation instead of using `@frozen. The
317
+ problem with that approach was coming up with a reasonable behavior of how the
318
+ new annotation works in resilient modules and what the difference to ` @frozen `
319
+ is. Feedback during this and previous pitches was that ` @frozen ` has more
320
+ implications than just the non-extensibility of enumerations but also impact on
321
+ ABI. We understand the feedback but still believe it is better to re-use the
322
+ same annotation and clearly document the additional behavior when used in
323
+ resilient modules.
324
+
325
+ ### Introduce a ` @preEnumExtensibility ` annotation
326
+
327
+ We considered introducing an annotation that allows developers to mark
328
+ enumerations as pre-existing to the new language feature similar to how
329
+ ` @preconcurrency ` works. The problem with such an annotation is how the compiler
330
+ would handle this in consuming modules. It could either downgrade the warning
331
+ for the missing ` @unknown default ` case or implicitly synthesize one. However,
332
+ the only reasonable behavior for synthesized ` @unknown default ` case is to
333
+ ` fatalError ` . Furthermore, such an attribute becomes even more problematic to
334
+ handle when the module then extends the annotated enum; thus, making it possible
335
+ to hit the ` @unknown default ` case during runtime leading to potentially hitting
336
+ the ` fatalError ` .
0 commit comments