Skip to content

Commit 34c2b0e

Browse files
committed
[cxx-interop] Make String.init(std.string) unlabeled
`Swift.String` can be initialized from any other type with an unlabeled initializer, which is either going to use the `CustomStringConvertible` conformance, or reflection. We would like clients to use the most suitable initializer, which is the one that takes `std.string` as a parameter. For instance, that allows us to attach a doc comment to the initializer. This change makes the initializer unlabeled to make sure it is chosed by overload resolution when a client invokes `String(myCxxString)`.
1 parent 1745c98 commit 34c2b0e

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

benchmark/cxx-source/CxxStringConversion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public func run_swiftToCxx(_ n: Int) {
5454
public func run_cxxToSwift(_ n: Int) {
5555
let str = cxxString!
5656
for _ in 0..<n {
57-
let x = String(cxxString: str)
57+
let x = String(str)
5858
blackHole(x)
5959
}
6060
}

stdlib/public/Cxx/std/String.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ extension std.string: ExpressibleByStringLiteral {
3131

3232
extension std.string: CustomDebugStringConvertible {
3333
public var debugDescription: String {
34-
return "std.string(\(String(cxxString: self)))"
34+
return "std.string(\(String(self)))"
3535
}
3636
}
3737

3838
extension std.string: CustomStringConvertible {
3939
public var description: String {
40-
return String(cxxString: self)
40+
return String(self)
4141
}
4242
}
4343

@@ -49,7 +49,7 @@ extension String {
4949
/// (`"\u{FFFD}"`).
5050
///
5151
/// - Complexity: O(*n*), where *n* is the number of bytes in the C++ string.
52-
public init(cxxString: std.string) {
52+
public init(_ cxxString: std.string) {
5353
let buffer = UnsafeBufferPointer<CChar>(
5454
start: cxxString.__c_strUnsafe(),
5555
count: cxxString.size())

test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var StdStringOverlayTestSuite = TestSuite("std::string overlay")
1010

1111
StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
1212
let cxx1 = std.string()
13-
let swift1 = String(cxxString: cxx1)
13+
let swift1 = String(cxx1)
1414
expectEqual(swift1, "")
1515

1616
let cxx2 = std.string("something123")
17-
let swift2 = String(cxxString: cxx2)
17+
let swift2 = String(cxx2)
1818
expectEqual(swift2, "something123")
1919

2020
let cxx3: std.string = "literal"
@@ -23,25 +23,25 @@ StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
2323
// Non-ASCII characters are represented by more than one CChar.
2424
let cxx4: std.string = "тест"
2525
expectEqual(cxx4.size(), 8)
26-
let swift4 = String(cxxString: cxx4)
26+
let swift4 = String(cxx4)
2727
expectEqual(swift4, "тест")
2828

2929
let cxx5: std.string = "emoji_🤖"
3030
expectEqual(cxx5.size(), 10)
31-
let swift5 = String(cxxString: cxx5)
31+
let swift5 = String(cxx5)
3232
expectEqual(swift5, "emoji_🤖")
3333

3434
let cxx6 = std.string("xyz\0abc")
3535
expectEqual(cxx6.size(), 7)
36-
let swift6 = String(cxxString: cxx6)
36+
let swift6 = String(cxx6)
3737
expectEqual(swift6, "xyz\0abc")
3838

3939
var cxx7 = std.string()
4040
let bytes: [UInt8] = [0xE1, 0xC1, 0xAC]
4141
for byte in bytes {
4242
cxx7.push_back(CChar(bitPattern: byte))
4343
}
44-
let swift7 = String(cxxString: cxx7)
44+
let swift7 = String(cxx7)
4545
expectEqual(swift7, "���")
4646
}
4747

0 commit comments

Comments
 (0)