[swift-evolution] [Pitch] Don't require & for UnsafeRawPointer

Jordan Rose jordan_rose at apple.com
Wed May 17 16:57:09 CDT 2017


> On May 17, 2017, at 14:53, Guillaume Lessard <glessard at tffenterprises.com> wrote:
> 
> 
>> On May 17, 2017, at 15:28, Jordan Rose <jordan_rose at apple.com> wrote:
>> 
>> This can come up even without &. It is illegal to use UnsafeMutablePointer.init(mutating:) on something that was not a mutable pointer to begin with, and the program is not guaranteed to do anything sensible if you break that rule.
> 
> “Illegal" or no, if it can be done it will de done. I just think it would be more sensible to *not* allow taking pointers to non-mutable variables at all.

That's what "unsafe" means. To take one specific use case, there's a big difference between withUnsafeBufferPointer and withUnsafeMutableBufferPointer on an Array: the latter triggers copy-on-write behavior before handing you the pointer. That's not an acceptable price to pay just for pointer access to the contents of the array.

(Another possible rule would be to drop UnsafeMutablePointer.init(mutating:), but unfortunately there are too many C APIs out there that don't use 'const'.)

Jordan



More information about the swift-evolution mailing list