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 > <[email protected]> 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 > [email protected] > https://lists.swift.org/mailman/listinfo/swift-evolution _______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
