Skip to content

Commit 1803d3e

Browse files
authored
Merge pull request #84223 from egorzhdan/egorzhdan/crash-any-cast
[cxx-interop] Fix a crash when calling `std::any_cast` from Swift
2 parents 8ffe033 + 18c5e23 commit 1803d3e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
module StdAny {
2+
header "std-any.h"
3+
requires cplusplus
4+
export *
5+
}
6+
17
module StdNumeric {
28
header "std-numeric.h"
39
requires cplusplus
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <any>
2+
#include <string>
3+
4+
inline std::any getStdAnyString() {
5+
return std::string("abc210");
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
2+
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++17)
3+
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++20)
4+
5+
// REQUIRES: executable_test
6+
7+
// In Microsoft STL, all overloads of std::any_cast return a dependent templated
8+
// type, which Swift isn't able to instantiate.
9+
// UNSUPPORTED: OS=windows-msvc
10+
11+
import StdlibUnittest
12+
import StdAny
13+
import CxxStdlib
14+
15+
var StdAnyTestSuite = TestSuite("StdAny")
16+
17+
StdAnyTestSuite.test("std.any_cast<std.string>") {
18+
let a1 = getStdAnyString()
19+
let c1 = std.any_cast(a1) as std.string
20+
expectEqual(c1, std.string("abc210"))
21+
}
22+
23+
runAllTests()

0 commit comments

Comments
 (0)