<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>This is expected behaviors of collections and their slices. The previous result was a bug.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I agree that collections in general and their slices need more documentation.<br><br>Sent from my iPhone</div><div><br>On Aug 24, 2017, at 11:28 AM, Ryan Lovelett &lt;<a href="mailto:swift-dev@ryan.lovelett.me">swift-dev@ryan.lovelett.me</a>&gt; wrote:<br><br></div><blockquote type="cite"><div>


<title></title>

<div>I think your onto it Philippe but then why is it behaving that way?<br></div>
<div><br></div>
<div>I updated my earlier script (attached) and added 2 things.<br></div>
<div><br></div>
<div>&nbsp; 1. print("Start: \(data.startIndex), End: \(data.endIndex), Count: \(data.count)") after the&nbsp;data.removeFirst(4).<br></div>
<div>&nbsp; 2. The startIndex offset you suggested.<br></div>
<div><br></div>
<div>It no longer crashes. Hooray!<br></div>
<div><br></div>
<div>Swift 3.1:<br></div>
<div><br></div>
<div>Start: 0, End: 2, Count: 2<br></div>
<div>Base64: QWE=, String: Aa<br></div>
<div><br></div>
<div>Swift 3.2:<br></div>
<div><br></div>
<div>Start: 4, End: 6, Count: 2<br></div>
<div>Base64: QWE=, String: Aa<br></div>
<div><br></div>
<div>Oh wait. Woah. That's a really subtle change that has some very real consequences.<br></div>
<div><br></div>
<div>Is this expected and/or documented? Because I certainly would not have expected that drastic of a change. I'll obviously need to go read a lot more of the changes between Swift 3.1 and Swift 3.2 if these sorts of changes are in scope.<br></div>
<div><br></div>
<div><br></div>
<div>On Thu, Aug 24, 2017, at 02:10 PM, Philippe Hausler wrote:<br></div>
<blockquote type="cite"><div>I see the issue.. the latest version traps (appropriately so).<br></div>
<div><div><br></div>
<div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(237, 232, 229);background-color:rgb(43, 43, 43);"><span class="colour" style="color:rgb(253, 67, 132)">let</span> str = <span class="colour" style="color:rgb(238, 219, 4)">String</span>(bytes: <span class="colour" style="color:rgb(118, 224, 242)">data</span>[<span class="colour" style="color:rgb(245, 127, 255)">0</span>..&lt;<span class="colour" style="color:rgb(245, 127, 255)">2</span>], encoding: .<span class="colour" style="color:rgb(118, 224, 242)">utf8</span>)!<br></div>
<div><div><br></div>
<div>The sub-range of the slice you have is incorrectly indexed. Try this out (I am presuming this is what you mean):<br></div>
<div><br></div>
<div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(237, 232, 229);background-color:rgb(43, 43, 43);"><span class="colour" style="color:rgb(253, 67, 132)">let</span> str = <span class="colour" style="color:rgb(238, 219, 4)">String</span>(bytes: <span class="colour" style="color:rgb(118, 224, 242)">data</span>[<span class="colour" style="color:rgb(118, 224, 242)">data</span>.<span class="colour" style="color:rgb(118, 224, 242)">startIndex</span>..&lt;(<span class="colour" style="color:rgb(118, 224, 242)">data</span>.<span class="colour" style="color:rgb(118, 224, 242)">startIndex</span> + <span class="colour" style="color:rgb(245, 127, 255)">2</span>)], encoding: .<span class="colour" style="color:rgb(118, 224, 242)">utf8</span>)!<br></div>
</div>
<div><div><br></div>
<blockquote type="cite"><div>On Aug 24, 2017, at 11:07 AM, Philippe Hausler &lt;<a href="mailto:phausler@apple.com">phausler@apple.com</a>&gt; wrote:<br></div>
<div><br></div>
<div><div><div>Is there a radar or <a href="http://bugs.swift.org">bugs.swift.org</a> ticket filed on this?<br></div>
<div><br></div>
<div>I presume because of the import this is a Darwin thing and not a linux thing.<br></div>
<div><br></div>
<blockquote type="cite"><div>On Aug 24, 2017, at 11:05 AM, Michael Gottesman via swift-dev &lt;<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a>&gt; wrote:<br></div>
<div><br></div>
<div><br></div>
<blockquote type="cite"><div>On Aug 24, 2017, at 10:47 AM, Ryan Lovelett via swift-dev &lt;<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a>&gt; wrote:<br></div>
<div><br></div>
<div>I've found what I believe is a bug. Though I'm unclear if the bug is in<br></div>
<div>Swift 3.1 or Swift 3.2/4.0. All I can say for sure is the behavior is<br></div>
<div>quite drastically different between the two.<br></div>
<div><br></div>
<div>For the code below (and attached):<br></div>
<div><br></div>
<div> &nbsp;import Cocoa<br></div>
<div><br></div>
<div> &nbsp;var data = Data(bytes: [0x50, 0x4B, 0x01, 0x02, 0x41, 0x61])<br></div>
<div> &nbsp;data.removeFirst(4)<br></div>
<div> &nbsp;let base64 = data.base64EncodedString()<br></div>
<div> &nbsp;let str = String(bytes: data[0..&lt;2], encoding: .utf8)!<br></div>
<div> &nbsp;print("Base64: \(base64), String: \(str)")<br></div>
<div><br></div>
<div>If I compile and run that with the Swift included in Xcode 8.3.3 (e.g.,<br></div>
<div>swift ./data-bug.swift) it outputs: Base64: QWE=, String: Aa. Which is<br></div>
<div>what I expect.<br></div>
<div><br></div>
<div>With the Swift that is included with Xcode 9.0 beta 6 (9M214v) (e.g.,<br></div>
<div>swift -swift-version 3 ./data-bug.swift). It performs an illegal<br></div>
<div>hardware instruction and crashes. It also does this if I use use the<br></div>
<div>version 4 of the compiler.<br></div>
<div><br></div>
<div>Is this a bug? If so where is the bug? Was this always meant to not work<br></div>
<div>and Swift 3.1 just happened to work or is there now an issue in the<br></div>
<div>Swift 3.2 implementation?<br></div>
</blockquote><div><br></div>
<div>I have not engaged my brain with the particulars of the rest of the email, but high level question: does this happen without optimization? Or does it happen only with optimization?<br></div>
<div><br></div>
<div>Michael<br></div>
<div><br></div>
<blockquote type="cite"><div>&lt;data-bug.swift&gt;_______________________________________________<br></div>
<div>swift-dev mailing list<br></div>
<div><a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-dev">https://lists.swift.org/mailman/listinfo/swift-dev</a><br></div>
</blockquote><div><br></div>
<div>_______________________________________________<br></div>
<div>swift-dev mailing list<br></div>
<div><a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-dev">https://lists.swift.org/mailman/listinfo/swift-dev</a><br></div>
</blockquote></div>
</div>
</blockquote></div>
</div>
</div>
</div>
</blockquote><div><br></div>


</div></blockquote><blockquote type="cite"><div>&lt;data-bug.swift&gt;</div></blockquote></body></html>