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

Jordan Rose jordan_rose at apple.com
Wed May 17 16:28:38 CDT 2017


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.

Jordan


> On May 17, 2017, at 14:22, Guillaume Lessard via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Back to the initial example, here’s a variant that shows a “non-mutable” pointer lying; try it in a playground.
> 
> ```
> func get(_ pointer: UnsafePointer<Int>, at index: Int) -> Int
> {
>  let mutator = UnsafeMutablePointer(mutating: pointer)
>  mutator[index] += 1
>  return pointer[index]
> }
> 
> let constantArray = [Int](repeating: 0, count: 3)
> 
> print(constantArray)	// [0,0,0]
> 
> let mutated = get(constantArray, at: 2) // returns 1
> 
> print(constantArray) // [0,0,1] oops!
> ```
> 
> I’d argue that this case should also require an ampersand and a mutable array.
> 
> Cheers,
> Guillaume Lessard
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list