mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
* [Swiftify] Emit Mutable[Raw]Span when possible Previously wrappers would use UnsafeMutable[Raw]Pointer for mutable pointers, and Span for non-const std::span, to prevent the compiler from complaining that MutableSpan didn't exist. Now that MutableSpan has landed we can finally emit MutableSpan without causing compilation errors. While we had (disabled) support for MutableSpan syntax already, some unexpected semantic errors required additional changes: - Mutable[Raw]Span parameters need to be inout (for mutation) - inout ~Escapable paramters need explicit lifetime annotations - MutableSpan cannot be directly bitcast to std::span, because it is ~Copyable, so they need unwrapping to UnsafeMutableBufferPointer rdar://147883022 * [Swiftify] Wrap if-expressions in Immediately Called Closures When parameters in swiftified wrapper functions are nullable, we use separate branches for the nil and nonnil cases, because `withUnsafeBufferPointer` (and similar) cannot be called on nil. If-expressions have some limitations on where they are allowed in the grammar, and cannot be passed as arguments to a function. As such, when the return value is also swiftified, we get an error when trying to pass the if-expression to the UnsafeBufferPointer/Span constructor. While it isn't pretty, the best way forward seems to be by wrapping the if-expressions in Immediately Called Closures. The closures have the side-effect of acting as a barrier for 'unsafe': unsafe keywords outside the closure do not "reach" unsafe expressions inside the closure. We therefore have to emit "unsafe" where unsafe expressions are used, rather than just when returning. rdar://148153063