swift
86ccc364 - [move-function] Allow for move to be applied to inout function arguments.

Commit
3 years ago
[move-function] Allow for move to be applied to inout function arguments. These are verified like a var except that the parameter will have an implicit read use on all function exit terminators. This ensures that if a programmer moves a value out of the inout parameter, a new value must be assigned before function exit. This is important since otherwise the convention would be invalidated. Example: ``` func f(_ x: inout T) { // Move a value out of x. x does not have a value within it anymore. let value = _move(x) } // So we emit an error saying there is a use here since an inout must have // a valid object within it upon function exit due to convention guarantees. ``` As an added side-effect of this, one can now use move as on self in mutating contexts. Thus one can move the self value out of the self inout binding using _move and if one does not replace self with a new value by end of function, you will get a compile time error, e.x.: ``` struct S { var buffer: Klass mutating func doSomething() { let b = move(self).buffer // ... do some stuff, maybe get a different buffer ... let maybeDifferentBuffer = maybeNewBuffer(b) // If we do not re-initialize S with a new value by uncommenting // the following line, we will get a compile time error. // self = S(differentBuffer) } } ``` (cherry picked from commit e6faa3048855741db1744ed1ced26d83c3a2cf90)
Author
Committer
Parents
Loading