<div dir="ltr">They all crash when I test it with Xcode 9.2 (default toolchain and dev snapshot 2017-12-09, command line app), in what environment are you seeing the non-crashing behavior?<div><br></div><div>My guess is that all of them crashing is the intended behavior, since the operations result in an overflow..</div><div><br><div>/Jens</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 12, 2017 at 9:06 PM, C. Keith Ray via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Should these be consistent? </div><div><br></div><div>All crashing or none crashing?</div><div><br></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(62,30,129);background-color:rgb(255,255,255)">print<span style="color:#000000">(</span>abs<span style="color:#000000">(</span><span style="color:#703daa">Int8</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) </span><span style="color:#008400">// crash</span></div></div><div><span style="color:#008400"><br></span></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(62,30,129);background-color:rgb(255,255,255)">print<span style="color:#000000">(</span>abs<span style="color:#000000">(</span><span style="color:#703daa">Int16</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) // crash</span></div></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(62,30,129);background-color:rgb(255,255,255)"><span style="color:#000000"><br></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(62,30,129);background-color:rgb(255,255,255)"><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="color:#3e1e81">print</span><span style="color:#000000">(</span><span style="color:#3e1e81">abs</span><span style="color:#000000">(</span><span style="color:#703daa">Int32</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) </span>// prints -2147483648</div><div><br></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><span style="font-family:Menlo;font-size:14px;text-decoration:underline">print</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">(</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline">abs</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">(</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(112,61,170)">Int64</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">.</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(112,61,170)">min</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">)) </span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,132,0)">// cra</span><span style="font-family:Menlo;font-size:14px;color:rgb(0,132,0)">sh</span></div></div></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">Should this be reported by Radar or another mechanism?</span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">I'm using this as a replacement:</span></div><div><span style="color:#000000"><br></span></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(112,61,170);background-color:rgb(255,255,255)"><span style="color:#ba2da2">func</span><span style="color:#000000"> safeAbs<T: </span>SignedInteger<span style="color:#000000"> & </span>FixedWidthInteger<span style="color:#000000">>(</span><span style="color:#ba2da2">_</span><span style="color:#000000"> i: </span><span style="color:#4f8187">T</span><span style="color:#000000">) -> </span><span style="color:#4f8187">T</span><span style="color:#000000"> {</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">if</span> i >= <span style="color:#272ad8">0</span> {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> i</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"> } </span><span style="color:#ba2da2">else</span><span style="color:#000000"> </span><span style="color:#ba2da2">if</span><span style="color:#000000"> i == </span><span style="color:#4f8187">T</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000"> { </span>// can't negate this value</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"> </span>// return i // unconverted, violates postcondition, assert(result >= 0)</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"> </span>// return 0 // or return an arbitrary "safe" value</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><span style="color:#000000"> </span><span style="color:#ba2da2">return</span><span style="color:#000000"> </span><span style="color:#272ad8">-(1</span><span style="color:#000000">+i) </span>// or return approximately the right value</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> } <span style="color:#ba2da2">else</span> {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> -i</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(49,89,93);background-color:rgb(255,255,255)"><span style="color:#3e1e81">print</span><span style="color:#000000">(</span>safeAbs<span style="color:#000000">(</span><span style="color:#703daa">Int8</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) // prints </span><b>127 </b></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(49,89,93)">safeAbs</span><span style="color:rgb(0,0,0)">(</span><font color="#703daa">Int16</font><span style="color:rgb(0,0,0)">.</span><font color="#703daa">min</font><span style="color:rgb(0,0,0)">)) // </span>prints 32767</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(49,89,93)">safeAbs</span><span style="color:rgb(0,0,0)">(</span><font color="#703daa">Int32</font><span style="color:rgb(0,0,0)">.</span><font color="#703daa">min</font><span style="color:rgb(0,0,0)">)) // </span>prints 2147483647</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(49,89,93)">safeAbs</span><span style="color:rgb(0,0,0)">(</span><font color="#703daa">Int64</font><span style="color:rgb(0,0,0)">.</span><font color="#703daa">min</font><span style="color:rgb(0,0,0)">)) // </span>prints 9223372036854775807</div></div><div><span style="color:#000000"><br></span></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>