Skip to content

Commit a4b7f37

Browse files
author
Max Moiseev
committed
[stdlib] Reverting the String.init?(_: String) behavior
1 parent caf5f68 commit a4b7f37

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
@@ -107,17 +107,6 @@ public protocol StringProtocol
107107
) rethrows -> Result
108108
}
109109

110-
extension StringProtocol /* : LosslessStringConvertible */ {
111-
// This overload is for the Swift 3 compatibility.
112-
// In Swift 4, conformance is satisfied by the non-failable initializer, so
113-
// that `String("") as String?` is still possible, but `String("")!` is not.
114-
@available(swift, obsoleted: 4,
115-
message: "Please use the non-failable initializer.")
116-
public init?(_ description: String) {
117-
self.init(description.characters)
118-
}
119-
}
120-
121110
/// Call body with a pointer to zero-terminated sequence of
122111
/// `TargetEncoding.CodeUnit` representing the same string as `source`, when
123112
/// `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
@@ -459,11 +459,11 @@ extension String {
459459
extension Substring {
460460
@available(swift, obsoleted: 4)
461461
public subscript(bounds: Range<Index>) -> String {
462-
return String(self[bounds])
462+
return String(characters[bounds])
463463
}
464464

465465
@available(swift, obsoleted: 4)
466466
public subscript(bounds: ClosedRange<Index>) -> String {
467-
return String(self[bounds] as Substring)
467+
return String(characters[bounds])
468468
}
469469
}

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)