You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The syntax above allows developers to explicitly annotate lifetime dependencies in their code.
261
-
But because the possibilities are limited, we can usually allow the compiler to infer a suitable dependency.
262
-
The detailed rules are below, but generally we require that the return type be nonescapable and that there be one “obvious” source for the dependency.
261
+
The syntax above allows developers to explicitly annotate lifetime dependencies in their code. But because the possibilities are limited, we can usually allow the compiler to infer a suitable dependency. The detailed rules are below, but generally we require that the return type be nonescapable and that there be an “obvious” source for the dependency.
263
262
264
-
In particular, we can infer a lifetime dependency on `self` for any method that returns a nonescapable value.
265
-
As above, the details vary depending on whether `self` is escapable or nonescapable:
263
+
#### Self dependence
264
+
265
+
We can infer a lifetime dependency on `self` for any method that returns a nonescapable value. As above, the details vary depending on whether `self` is escapable or nonescapable:
For free or static functions or initializers, we can infer a lifetime dependency when the return value is nonescapable and there is only one obvious argument that can serve as the source of the dependency.
290
-
For example:
289
+
#### Same-type dependence
291
290
292
-
```swift
293
-
structNEType: ~Escapable { ... }
291
+
For any function or method that returns a nonescapable type, we infer a copied lifetime dependency on all parameters of the same type.
294
292
295
-
// If there is only one argument with an explicit parameter convention:
We expect these implicit inferences to cover most cases, with the explicit form only occasionally being necessary in practice.
303
+
This is particularly helpful for Generic APIs. With this rule, indicating that a generic parameter is `~Escapable` should usually be sufficient to infer the correct lifetime dependence.
308
304
309
305
### Dependent parameters
310
306
@@ -672,20 +668,35 @@ The implications of mutation modifiers and argument type on the resulting lifeti
672
668
673
669
### Inference Rules
674
670
675
-
If there is no explicit lifetime dependency, we will automatically infer one according to the following rules:
671
+
If there is no explicit lifetime dependency on the nonescapable result of a method or function, we will attempt to infer dependencies automatically according the following rules:
676
672
677
-
**For methods where the return value is nonescapable**, we will infer a dependency against self, depending on the mutation type of the function.
678
-
Note that this is not affected by the presence, type, or modifier of any other arguments to the method.
673
+
1. For methods where the return value is nonescapable, we will infer a dependency against `self`. If `self` is nonescapable, then we infer a copying dependency. If `self` is escapable, and the method is `borrowing` or `mutating`, then we infer a scoped dependency.
679
674
680
-
**For a free or static functions or initializers with at least one argument,** we will infer a lifetime dependency when the return value is nonescapable and exactly one argument that satisfies any of the following:
681
-
- is nonescapable, or
682
-
- is non-BitwiseCopyable and has an explicit `borrowing`, or `inout` convention
675
+
2. For methods, functions, and initializers where the return value is nonescapable, we infer a copied lifetime dependency on all parameters of the same (nonescapable) type, including the implicit `self` parameter.
683
676
684
-
In this case, the compiler will infer a dependency on the unique argument identified by these conditions.
677
+
3. For functions and initializers that have a nonescapable return value and a single parameter, we infer dependence on that parameter. If the parameter is nonescapable, then we infer a copying dependency; otherwise, we infer a scoped dependency.
678
+
679
+
For all inference rules, the type of dependence is the same as an explicit `dependsOn(argument)` on the same argument without any `scoped` qualifier based on the argument's type.
685
680
686
681
**In no other case** will a function, method, or initializer implicitly gain a lifetime dependency.
687
682
If a function, method, or initializer has a nonescapable return value, does not have an explicit lifetime dependency annotation, and does not fall into one of the cases above, then that will be a compile-time error.
688
683
684
+
We infer dependencies according to all applicable rules. Here, both rule #1 and #2 apply:
0 commit comments