[swift-evolution] Proposal: Extend the &x -> UnsafePointer behavior to work with immutable values

Joe Groff jgroff at apple.com
Wed Dec 16 13:54:04 CST 2015

> On Dec 16, 2015, at 11:44 AM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
> # Introduction
> Swift allows you to pass a &x ref to a function taking Unsafe[Mutable]Pointer, as long as x is mutable.
> # Problem
> There's no way to pass an immutable UnsafePointer that points to immutable data. Any such immutable data has to be copied into a mutable variable before the &x ref works.

This isn't strictly true; you can pass [x].

> # Solution
> Allow for using &x with immutable values if and only if the reference is passed as a parameter to a function expecting UnsafePointer

Seems reasonable. As I mentioned on swift-dev, '&x' in Swift indicates "this call mutates x with inout semantics", not the C sense of "I'm passing a pointer", so doing the conversion without an '&' seems more Swift-ish to me. The semantics of the implicit const pointer conversion also only allow the C call to treat the parameter as if it were an immutable value operand; any capture of or mutation through the variable is UB.


More information about the swift-evolution mailing list