mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[stdlib] Spot fix for https://bugs.swift.org/browse/SR-5401
The simplest way to keep the string buffer alive is simply to always grab its iterator, even if we're not going to use it. Thanks @milseman for that idea and @jckarter for the diagnosis help!
This commit is contained in:
@@ -366,6 +366,22 @@ StringTests.test("[String].joined() -> String") {
|
||||
_ = s == "" // should compile without error
|
||||
}
|
||||
|
||||
StringTests.test("UnicodeScalarView.Iterator.Lifetime") {
|
||||
// Tests that String.UnicodeScalarView.Iterator is maintaining the lifetime of
|
||||
// an underlying String buffer. https://bugs.swift.org/browse/SR-5401
|
||||
//
|
||||
// WARNING: it is very easy to write this test so it produces false negatives
|
||||
// (i.e. passes even when the code is broken). The array, for example, seems
|
||||
// to be a requirement. So perturb this test with care!
|
||||
let sources = ["𝓣his 𝓘s 𝓜uch 𝓛onger 𝓣han 𝓐ny 𝓢mall 𝓢tring 𝓑uffer"]
|
||||
for s in sources {
|
||||
// Append something to s so that it creates a dynamically-allocated buffer.
|
||||
let i = (s + "X").unicodeScalars.makeIterator()
|
||||
expectEqualSequence(s.unicodeScalars, IteratorSequence(i).dropLast(),
|
||||
"Actual Contents: \(Array(IteratorSequence(i)))")
|
||||
}
|
||||
}
|
||||
|
||||
var CStringTests = TestSuite("CStringTests")
|
||||
|
||||
func getNullUTF8() -> UnsafeMutablePointer<UInt8>? {
|
||||
|
||||
Reference in New Issue
Block a user