Open norio-nomura opened 5 years ago
It does not on Swift 4.
It crashes when {{String}} is created using range subscript with {{String.Index}} that does not point aligned to {{UnicodeScalar}}'s boundaries.
I'm not sure this is supposed to be legal to begin with, but if it's not, there should be a proper error message. cc @milseman
The String documentation suggests it should be legal, although it only mentions subscript, not range subscript:
https://github.com/apple/swift/blob/swift-5.0-branch/stdlib/public/core/String.swift
My bolding:
/// Note that an index into one view may not have an exact corresponding
/// position in another view. For example, the `flag` string declared above
/// comprises a single character, but is composed of eight code units when
/// encoded as UTF-8. The following code creates constants for the first and
/// second positions in the `flag.utf8` view. Accessing the `utf8` view with
/// these indices yields the first and second code UTF-8 units.
///
/// let firstCodeUnit = flag.startIndex
/// let secondCodeUnit = flag.utf8.index(after: firstCodeUnit)
/// // flag.utf8[firstCodeUnit] == 240
/// // flag.utf8[secondCodeUnit] == 159
///
/// When used to access the elements of the `flag` string itself, however, the
/// `secondCodeUnit` index does not correspond to the position of a specific
/// character. Instead of only accessing the specific UTF-8 code unit, that
/// index is treated as the position of the character at the index's encoded
/// offset. In the case of `secondCodeUnit`, that character is still the flag
/// itself.
///
/// // flag[firstCodeUnit] == "\<flag>"
/// // flag[secondCodeUnit] == "\<flag>"
If we compare the behaviour of Swift 4.2 and Swift 5 surrounding this example:
let flag = "<flag>"
let firstCodeUnit = flag.startIndex
let secondCodeUnit = flag.utf8.index(after: firstCodeUnit)
flag[secondCodeUnit] // Swift 4.2: "<flag>", Swift 5: "<flag>"
let flag1 = String(flag[secondCodeUnit..<flag.endIndex]) // Swift 4.2: "<flag>", Swift 5: "<prints some octals>"
Note: replaced actual flag character from documentation by \<flag> as it broke JIRA Save.
I am not sure it makes sense that the subscript and range subscript behave differently in this regard.
Environment
`swift-5.0-DEVELOPMENT-SNAPSHOT-2019-01-29-a`Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Standard Library | |Labels | Bug, RunTimeCrash | |Assignee | None | |Priority | Medium | md5: 65fada3d4d59ceab3c21324a17955acaIssue Description:
Reproduced log: