<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 23, 2017, at 3:07 PM, Tony Allevato 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="">This could be solved by extending the existing string literal handling and letting type inference do the rest. The real problem here is that `UInt8(ascii: X)` is annoying to write when you're dealing with a large amount of low-level data.<div class=""><br class=""></div><div class="">If UInt8 conformed to ExpressibleByUnicodeScalarLiteral, you could get most of the way there—you'd just have to have it fail at runtime for anything outside 0...127. But then you could write `let c: UInt8 = "x"` and it would just work.</div><div class=""><br class=""></div><div class="">Failing at runtime is undesirable though</div></div></div></blockquote><br class=""></div><div>There is another way to handle this: we can diagnose it the same way we diagnose statically identifiable overflow of arithmetic operations:</div><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 10px; line-height: normal; font-family: Monaco; background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">(swift) 128 as Int8</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #33bbc8" class="">&lt;REPL Input&gt;:1:1: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c33720" class="">error: </span><span style="font-variant-ligatures: no-common-ligatures" class="">integer literal '128' overflows when stored into 'Int8'</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">128 as Int8</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(52, 188, 38);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">^</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">(swift) 1+127 as Int8</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #33bbc8" class="">&lt;REPL Input&gt;:1:2: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c33720" class="">error: </span><span style="font-variant-ligatures: no-common-ligatures" class="">arithmetic operation '1 + 127' (on type 'Int8') results in an overflow</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">1+127 as Int8</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(52, 188, 38);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">~^~~~</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div></div><div>The way this happens is through constant folding at the SIL level, which emits diagnostics when they constant fold the “should trap” bit on these operations to true. &nbsp;The code in question is in&nbsp;lib/SILOptimizer/Mandatory/ConstantPropagation.cpp</div><div class=""><br class=""></div>-Chris<div class=""><br class=""></div></body></html>