[swift-evolution] [Pitch] Raw mode string literals

Chris Lattner clattner at nondot.org
Thu Nov 23 17:17:03 CST 2017



> On Nov 23, 2017, at 3:07 PM, Tony Allevato via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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.
> 
> 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.
> 
> Failing at runtime is undesirable though

There is another way to handle this: we can diagnose it the same way we diagnose statically identifiable overflow of arithmetic operations:

(swift) 128 as Int8
<REPL Input>:1:1: error: integer literal '128' overflows when stored into 'Int8'
128 as Int8
^
(swift) 1+127 as Int8
<REPL Input>:1:2: error: arithmetic operation '1 + 127' (on type 'Int8') results in an overflow
1+127 as Int8
~^~~~

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.  The code in question is in lib/SILOptimizer/Mandatory/ConstantPropagation.cpp

-Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171123/f4fdb714/attachment.html>


More information about the swift-evolution mailing list