Skip to content

Commit 8876655

Browse files
authored
[stdlib] Reverting the String.init?(_: String) behavior (#9659)
1 parent c8f8e59 commit 8876655

File tree

5 files changed

+11
-23
lines changed

5 files changed

+11
-23
lines changed

stdlib/public/core/String.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,6 @@ public protocol StringProtocol
110110
) rethrows -> Result
111111
}
112112

113-
extension StringProtocol /* : LosslessStringConvertible */ {
114-
// This overload is for the Swift 3 compatibility.
115-
// In Swift 4, conformance is satisfied by the non-failable initializer, so
116-
// that `String("") as String?` is still possible, but `String("")!` is not.
117-
@available(swift, obsoleted: 4,
118-
message: "Please use the non-failable initializer.")
119-
public init?(_ description: String) {
120-
self.init(description.characters)
121-
}
122-
}
123-
124113
/// Call body with a pointer to zero-terminated sequence of
125114
/// `TargetEncoding.CodeUnit` representing the same string as `source`, when
126115
/// `source` is interpreted as being encoded with `SourceEncoding`.

stdlib/public/core/StringRangeReplaceableCollection.swift.gyb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ extension String : StringProtocol {
3737
}
3838

3939
// FIXME(strings): doc comment
40-
// This initializer satisfies the conformance to LosslessStringConvertible
41-
// and disambiguates between the following intitializers, now that String
42-
// conforms to Collection:
40+
// This initializer disambiguates between the following intitializers, now
41+
// that String conforms to Collection:
4342
// - init<T>(_ value: T) where T : LosslessStringConvertible
4443
// - init<S>(_ characters: S) where S : Sequence, S.Element == Character
45-
public init(_ other: String) {
44+
public init<T : StringProtocol>(_ other: T) {
45+
self.init(other.characters)
46+
}
47+
48+
// This initializer satisfies the LosslessStringConvertible conformance
49+
public init?(_ other: String) {
4650
self.init(other._core)
4751
}
4852

stdlib/public/core/Substring.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ extension String {
461461
extension Substring {
462462
@available(swift, obsoleted: 4)
463463
public subscript(bounds: Range<Index>) -> String {
464-
return String(self[bounds])
464+
return String(characters[bounds])
465465
}
466466

467467
@available(swift, obsoleted: 4)
468468
public subscript(bounds: ClosedRange<Index>) -> String {
469-
return String(self[bounds] as Substring)
469+
return String(characters[bounds])
470470
}
471471
}

test/stdlib/StringAPI.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,6 @@ StringTests.test("CompareStringsWithUnpairedSurrogates")
361361
)
362362
}
363363

364-
StringTests.test("String.init(_:String)/default type") {
365-
var s = String("")
366-
expectType(String.self, &s)
367-
}
368-
369364
StringTests.test("[String].joined() -> String") {
370365
let s = ["hello", "world"].joined()
371366
_ = s == "" // should compile without error

test/stdlib/StringCompatibility.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Tests.test("ClosedRange/Subsript/ExpectedType") {
3939

4040
Tests.test("String.init(_:String)/default type") {
4141
var s = String("")
42-
expectType(String.self, &s)
42+
expectType(String?.self, &s)
4343
}
4444

4545
Tests.test("LosslessStringConvertible/generic") {

0 commit comments

Comments
 (0)