<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 23, 2015, at 5:35 PM, Árpád Goretity via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi everyone,<div class=""><br class=""></div><div class="">I was recently trying to use a C API (LLVM for the record) that required passing an array to a function in the form of a pointer and a size. I couldn't find a straightforward way to pass a null pointer to the function in question conditionally (when the array is empty), since the following – simplified – code doesn't currently typecheck:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; // C function with signature: void foo(T *ptr, unsigned size)</div><div class="">&nbsp; &nbsp; // imported into Swift as: (UnsafeMutablePointer&lt;T&gt;, UInt32) -&gt; ()</div><div class="">&nbsp; &nbsp; var arr: [T] = []</div><div class="">&nbsp; &nbsp; foo(arr.count &gt; 0 ? &amp;arr[0] : nil, UInt32(arr.count))</div><div class=""><br class=""></div><div class="">The error is: result values in '? :' expression have mismatching types 'inout T' and '_'&nbsp;</div></div></div></blockquote><div><br class=""></div><div>The diagnostic here sucks. The inout-to-pointer is only available to function argument expressions; it's not even considered in a ternary expression here, so the type checker can't find any way to match 'nil' and an inout.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">Since the inout operator (&amp;) can only be used in function call arguments (so it's not exactly C's address-of), I believe that there's no easy way of elegantly passing a null pointer when the array is empty. (Yes, I could write two almost-identical calls, but meh…) And even if there is one (and I'm just missing it), the fact that the above code does not work seems inconsistent to me.</div><div class=""><div class=""><br class=""></div><div class="">I also realized that this specific issue generalizes to the (in)ability of passing one-past-end pointers – which would be equally valid and even more convenient in the above case, as the callee does not dereference the passed pointer when the count is 0, but in general, it can be applied to functions accepting [begin, end + 1) ranges.</div><div class=""><br class=""></div><div class="">The problem here is that a one-past-end pointer does not reside at a valid index (pretty much by definition), so bounds checking kicks in and kills the program.</div></div></div></div></blockquote><div><br class=""></div><div>Past-the-end indices are valid pointers (and valid in Swift collections in general). That's not the problem. `&amp;arr[0]` fails because it's providing a temporary buffer connected only to the *element* &amp;arr[0], rather than a buffer representing the entire array. This won't do what you expect for any Swift array, even if it's non-empty.</div><div><br class=""></div><div>In general, we can't support fully first-class pointers into managed Swift entities like Array and properties, without breaking the encapsulation of those abstractions. We can provide scoped operations like `withUnsafePointer` that give you a pointer to a possibly-temporary buffer that represents the value of that array or value for the duration of a block. When you say `CFunctionThatTakesPointer(&amp;a)`, Swift's really wrapping that call in the equivalent of `withUnsafeMutableBufferPointer` on your behalf. You can see how that would be problematic if the wrapping needs to be conditional, such as if it appeared in a ternary or &amp;&amp;/|| expression. I think the thing to do is to make two calls:</div><div><br class=""></div><div>if arr.empty {</div><div>&nbsp; foo(nil, 0)</div><div>} else {</div><div>&nbsp; foo(&amp;arr, arr.count)</div><div>}</div><div><br class=""></div><div>since preparing the buffer for the pointer itself isn't necessarily free, and you'd want to avoid that work if you don't need it.</div><div><br class=""></div><div>It might be OK to have the pointer produced for an empty array be null to begin with, which would avoid the need for this conditional at all. In most cases, you can't safely dereference a pointer to nothing anyway. I'm also sympathetic to the idea of disconnecting "address-of" and "inout", since it often leads to confusion like this.</div><div><br class=""></div><div>-Joe</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="">My proposed solutions:</div><div class=""><br class=""></div><div class="">&nbsp;– Extend type inference for unsafe pointers and nil, so that when a value is passed by address to a function, it's not only the result of an &amp;-expression that has its type inferred to be (or implicitly converted to) Unsafe[Mutable]Pointer, but if there's a nil somewhere around, such as the one in the example above, it gets promoted to that type too, just like NULL in C or nullptr in C++.</div><div class=""><br class=""></div><div class="">&nbsp;– Stop overloading the inout '&amp;' operator and using it for C-style address-of operations. I could imagine a similar, but distinct operator or even a library function (something along the lines of unsafeAddressOf) that specifically yields the physical address of its operand as an unsafe C pointer, and which is thus first-class in the sense that it may be used anywhere other expressions may be, not just as immediate call arguments.</div><div class=""><br class=""></div><div class="">&nbsp;– Make array bounds checking more lenient when passing pointers to array elements into C functions. Bounds checking should, in these cases, allow indexing the one-past-end element of an array if (and only if) it is the argument of the address-of operator.</div><div class=""><br class=""></div><div class="">Comments and questions are welcome (you might need clarification, as it's 2:35 AM here when I'm writing this…)</div><div class=""><br class=""></div><div class="">Cheers,</div><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><font face="monospace" size="3" class="">Author of the Sparkling language</font><div class=""><font face="monospace" size="3" class=""><a href="http://h2co3.org/" target="_blank" class="">http://h2co3.org/</a><br class=""></font></div><div class=""><font face="monospace" size="3" class=""><br class=""></font></div></div></div>
</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RoDF4MveSEMYBIqIJA6ub1g8cOZ-2BVYvqV-2FqygPhjPn9zj0ys8nJu9ylq-2BdpicsJKuLXKmY0ceHMJ1MYMJ0AbZy99rB0ShOU1MkVJeonVWJIqQ8xVyx-2BpKLr-2FXrmJFOmAW-2BItyhWolW0IfGKA9ynicamDMibuiZq-2BnDKyd-2BAHd3p9ZZZat8eWK-2BJcIZvufPYvT9igASz4wKx86pMeUKkh2SadB0a6ayWr8VFe5VSNOaw-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>