Skip to content

Commit e0e9eac

Browse files
committed
Updated SE-0047 to reflect declaration modification and update example cases, plus a few minor tweaks
1 parent 0739b5f commit e0e9eac

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

proposals/0047-nonvoid-warn.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* Review manager: [Chris Lattner](https://github.com/lattner)
77

88
## Introduction
9-
109
In Swift's current incarnation, annotating methods and functions with `@warn_unused_result` informs the compiler that a non-void return type *should be consumed*. It is an affirmative declaration. In its absence, ignored results do not raise warnings or errors.
1110

1211
In its present form, this declaration attribute primarily differentiate between mutating and non-mutating pairs. It offers an optional `mutable_variant` for when an expected return value is not consumed. For example, when `sort` is called with an unused result, the compiler suggests using `sortInPlace` for unused results.
@@ -54,7 +53,7 @@ In the four examples mentioned here, this proposal makes the following recommend
5453
* Collections example: This could be argued either way. In such a situation, should be left unmarked. Leaving the implementation unmarked encourages an act from the consumer to either handle the result or actively dimiss the warning.
5554
* `printf` example: importing C calls is left as an exercise for the Swift team
5655

57-
*Note: This proposal does not ignore the positive utility of pairing in-place/procedural and value-returning/functional implementations.and retains the ability to enable Xcode system to cross reference between the two.*
56+
*Note: This proposal does not ignore the positive utility of pairing in-place/procedural and value-returning/functional implementations and retains the ability to enable Xcode system to cross reference between the two. We propose that the declaration attribute surrenders that responsibility and passes it to the rich documentation comment system.*
5857

5958
## Detail Design
6059

@@ -64,31 +63,25 @@ Under this proposal, the Swift compiler emits a warning when any method or funct
6463
_ = discardableResult()
6564
```
6665

67-
While this workaround makes it clear that the consumption of the result is intentionally discarded, it offers no traceable intent as to whether the API designer meant for this use to be valid. Including an explicite attribute ensures the discardable return value use is one that has been considered and approved by the API author.
66+
While this workaround makes it clear that the consumption of the result is intentionally discarded, it offers no traceable intent as to whether the API designer meant for this use to be valid. Including an explicit attribute ensures the discardable return value use is one that has been considered and approved by the API author.
6867

6968
The approach takes the following form:
7069

7170
```swift
72-
func f() -> T {} // defaults to warn on unused result
73-
func g() -> @discardable T {} // may be called as a procedure as g()
74-
// without emitting a compiler warning
71+
@discardableResult func f() -> T {} // may be called as a procedure as g()
72+
// without emitting a compiler warning
73+
func g() -> T {} // defaults to warn on unused result
7574
func h() {} // Void return type, does not fall under the umbrella of this proposal
7675
```
7776

78-
Decorating the return type makes it clear that it's the result that can be optionally treated as discardable rather than the function whose role it is to police its use.
79-
80-
`@discardable T` offers a covariant specialization of `T` and Void, enabling it to satisfy non-discardable requirements:
77+
The following examples demonstrate the `@discardableResult` behavior:
8178

8279
```swift
83-
func f() -> @discardable T {}
84-
func g() -> T {}
85-
func h() {} // h() -> Void
86-
87-
let c1: () -> T = f // no compiler warning
88-
let c2: () -> Void = f // no compiler warning
89-
let c3: () -> @discardable T = f // no compiler warning
90-
let c4: () -> @discardable T = g // compiler warning about incompatible types
91-
let c5: () -> @discardable T = h // compiler warning about incompatible types
80+
let c1: () -> T = f // no compiler warning, OK
81+
let c2: () -> Void = f // compiler error, not allowed
82+
let c3 = f // assignment does not preserve @discardableResult attribute
83+
c3() // warning unused result
84+
_ = c3() // no compiler warning, OK
9285
```
9386

9487
### Mutating Variants
@@ -102,19 +95,24 @@ This proposal recommends introducing two further comment fields, specifically `m
10295

10396
The `message` argument that formerly provided a textual warning when a function or method was called with an unused result will be discarded entirely and subsumed by document comments. Under this scheme, whatever attribute name is chosen to modify function names or return types will not use arguments.
10497

105-
## Conventional Alternatives Considered
98+
## Alternative Names Considered
10699

107-
Other keywords considered for decorating the type included: `@ignorable`, `@incidental`, `@elective`, `@discretionary`, `@voluntary`, `@voidable`, `@throwaway`, and `@_` (underscore).
100+
Alternative names considered included names were considered: `@allowUnusedResult`, `@optionalResult`, `@suppressUnusedResultWarning`, `@noWarnUnusedResult`, `@ignorableResult`, `@incidentalResult`, and `@discretionaryResult`.
108101

109-
Our alternative approach takes a prefix form, marking the declaration with an attribute:
102+
## Future directions
103+
104+
The Swift Evolution community also discussed decorating the type rather than the declaration.
105+
Decorating the return type makes it clear that it's the result that can be optionally treated as discardable rather than the function whose role it is to police its use.
110106

111107
```swift
112-
@attribute func f() -> T {}
108+
func f() -> @discardable T {} // may be called as a procedure as f()
109+
// without emitting a compiler warning
113110
```
114111

115-
The attribute retains the placement of `@warn_unused_result`, for example `@allowUnusedResult`.
112+
This approach was discarded to reduce the type system impact and complexity of the proposal. When not coordinated with the base function type, currying or "taking the address" of a function could effectively remove the @discardableResult attribute. This means some use of an otherwise `@discardable` function value would have to use `_ =`. While this approach was considered more elegant, the additional implementation costs means that it's best to delay adopting type decoration
113+
until such time as there's a strong motivation to use such an approach.
116114

117-
For prefix attributes, the following names were considered: `@allowUnusedResult`, `@optionalResult`, `@suppressUnusedResultWarning`, `@discardableResult`, `@noWarnUnusedResult`, `@ignorableResult`, `@incidentalResult`, and `@discretionaryResult`.
115+
Keywords considered for decorating the type included: `@discardable`, `@ignorable`, `@incidental`, `@elective`, `@discretionary`, `@voluntary`, `@voidable`, `@throwaway`, and `@_` (underscore).
118116

119117
## Unconventional Alternatives
120118

@@ -143,10 +141,10 @@ surmounted, this would offer a simple, elegant solution.
143141

144142
## Snake Case
145143

146-
It should be noted that this proposal, if accepted, removes two of the last remaining instances of snake_case in the Swift language. This further brings the language into a coherent and universal use of lowercase and camelcase variants.
144+
It should be noted that this proposal, if accepted, removes two of the last remaining instances of snake_case in the Swift language. This further brings the language into a coherent and universal use of lowercase and camel case variants.
147145

148146
## Acknowledgements
149147

150148
Changing the behavior of non-void functions to use default warnings for unused results was initially introduced by Adrian Kashivskyy. Additional thanks go out to Chris Lattner, Gwendal Roué, Dmitri Gribenko, Jeff Kelley, David Owens,
151149
Stephen Cellis, Ankit Aggarwal, Paul Ossenbruggen,
152-
for their feedback on this topic.
150+
for their feedback on this topic.

0 commit comments

Comments
 (0)