Skip to content

Commit 7dc8873

Browse files
committed
[cxx-interop] Simplify std::string construction in the overlay
libc++ provides a 2-parameter constructor of `std::string`, so we can apply the logic under `#if os(Windows)` on Apple platforms too. This avoids deserialization issues when building some complex projects that are still under investigation. rdar://145939013
1 parent 44bd40d commit 7dc8873

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

stdlib/public/Cxx/std/String.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ extension std.string {
2222
@_alwaysEmitIntoClient
2323
public init(_ string: String) {
2424
self = unsafe string.withCString(encodedAs: UTF8.self) { buffer in
25-
#if os(Windows)
26-
// Use the 2 parameter constructor.
27-
// The MSVC standard library has a enable_if template guard
28-
// on the 3 parameter constructor, and thus it's not imported into Swift.
29-
unsafe std.string(buffer, string.utf8.count)
30-
#else
25+
// MSVC STL has a enable_if template guard on the 3-parameter constructor,
26+
// and thus it's not imported into Swift.
27+
// libc++ provides both 2-parameter and 3-parameter constructors.
28+
// libstdc++ only provides the 3-parameter constructor.
29+
30+
// Note that we might be compiling with libc++ on Linux, even if it's not
31+
// the default stdlib on a particular distro.
32+
#if os(Linux)
3133
unsafe std.string(buffer, string.utf8.count, .init())
34+
#else
35+
unsafe std.string(buffer, string.utf8.count)
3236
#endif
3337
}
3438
}
@@ -42,14 +46,11 @@ extension std.string {
4246

4347
@_alwaysEmitIntoClient
4448
public init(_ string: UnsafePointer<CChar>) {
45-
#if os(Windows)
46-
// Use the 2 parameter constructor.
47-
// The MSVC standard library has a enable_if template guard
48-
// on the 3 parameter constructor, and thus it's not imported into Swift.
49-
unsafe self.init(string, UTF8._nullCodeUnitOffset(in: string))
50-
#else
49+
#if os(Linux)
5150
unsafe self.init(string, UTF8._nullCodeUnitOffset(in: string), .init())
52-
#endif
51+
#else
52+
unsafe self.init(string, UTF8._nullCodeUnitOffset(in: string))
53+
#endif
5354
}
5455

5556
@_alwaysEmitIntoClient

0 commit comments

Comments
 (0)