Skip to content

Commit 9c44e01

Browse files
committed
[cxx-interop][stdlib] windows - use new hash inline functions like other platforms
The PR #77857 added windows-specific workaround for #77856, that happened after #77843. Unfortunately this caused a new issue on windows - #78119. It looks like windows is suffering from a similar serialization issue as libstdc++, although its even more complex as the callAsFunction is not only a derived function from a base class, the base class although has a static call operator. In any case, the libstdc++ callAsFunction deserialization fix should align with the static operator () deserialization too, so for now make windows use the same workaround as other platforms to avoid the deserialization crash (77856). This change was tested on i686 windows too, ensuring that IR verifier crash no longer happens
1 parent 8715c34 commit 9c44e01

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

stdlib/public/Cxx/cxxshim/libcxxstdlibshim.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
/// Used for std::string conformance to Swift.Hashable
66
typedef std::hash<std::string> __swift_interopHashOfString;
7-
inline std::size_t __swift_interopComputeHashOfString(std::string str) {
7+
inline std::size_t __swift_interopComputeHashOfString(const std::string &str) {
88
return __swift_interopHashOfString()(str);
99
}
1010

1111
/// Used for std::u16string conformance to Swift.Hashable
1212
typedef std::hash<std::u16string> __swift_interopHashOfU16String;
13-
inline std::size_t __swift_interopComputeHashOfU16String(std::u16string str) {
13+
inline std::size_t __swift_interopComputeHashOfU16String(const std::u16string &str) {
1414
return __swift_interopHashOfU16String()(str);
1515
}
1616

1717
/// Used for std::u32string conformance to Swift.Hashable
1818
typedef std::hash<std::u32string> __swift_interopHashOfU32String;
19-
inline std::size_t __swift_interopComputeHashOfU32String(std::u32string str) {
19+
inline std::size_t __swift_interopComputeHashOfU32String(const std::u32string &str) {
2020
return __swift_interopHashOfU32String()(str);
2121
}
2222

stdlib/public/Cxx/std/String.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ extension std.string: Hashable {
198198
@_alwaysEmitIntoClient
199199
public func hash(into hasher: inout Hasher) {
200200
// Call std::hash<std::string>::operator()
201-
#if os(Windows) // FIXME: https://github.com/swiftlang/swift/issues/77856
202-
let cxxHash = __swift_interopHashOfString().callAsFunction(self)
203-
#else
204201
let cxxHash = __swift_interopComputeHashOfString(self)
205-
#endif
206202
hasher.combine(cxxHash)
207203
}
208204
}
@@ -211,11 +207,7 @@ extension std.u16string: Hashable {
211207
@_alwaysEmitIntoClient
212208
public func hash(into hasher: inout Hasher) {
213209
// Call std::hash<std::u16string>::operator()
214-
#if os(Windows) // FIXME: https://github.com/swiftlang/swift/issues/77856
215-
let cxxHash = __swift_interopHashOfU16String().callAsFunction(self)
216-
#else
217210
let cxxHash = __swift_interopComputeHashOfU16String(self)
218-
#endif
219211
hasher.combine(cxxHash)
220212
}
221213
}
@@ -224,11 +216,7 @@ extension std.u32string: Hashable {
224216
@_alwaysEmitIntoClient
225217
public func hash(into hasher: inout Hasher) {
226218
// Call std::hash<std::u32string>::operator()
227-
#if os(Windows) // FIXME: https://github.com/swiftlang/swift/issues/77856
228-
let cxxHash = __swift_interopHashOfU32String().callAsFunction(self)
229-
#else
230219
let cxxHash = __swift_interopComputeHashOfU32String(self)
231-
#endif
232220
hasher.combine(cxxHash)
233221
}
234222
}

test/Interop/Cxx/stdlib/Inputs/module.modulemap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ module StdString {
4040
export *
4141
}
4242

43+
module StdStringAndVector {
44+
header "std-string-and-vector.h"
45+
requires cplusplus
46+
export *
47+
}
48+
4349
module StdStringView {
4450
header "std-string-view.h"
4551
requires cplusplus
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <string>
2+
#include <vector>
3+
4+
struct Item {
5+
std::vector<std::string> keys;
6+
std::vector<std::string> values;
7+
};
8+
9+
inline Item get_item() {
10+
return {};
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=default -Xcc -std=c++20 -O)
2+
//
3+
// REQUIRES: executable_test
4+
5+
// Tests optimizations related to CxxStdlib.
6+
7+
import StdlibUnittest
8+
import CxxStdlib
9+
import StdStringAndVector
10+
11+
var StdStringOptTestSuite = TestSuite("StdStringWithOpts")
12+
13+
StdStringOptTestSuite.test("std::string with Hashable conformance optimized") {
14+
let item = get_item()
15+
let dict = Dictionary(uniqueKeysWithValues: zip(item.keys, item.values).lazy)
16+
17+
expectEqual(dict.count, 0)
18+
}
19+
20+
runAllTests()

0 commit comments

Comments
 (0)