Merge pull request #84223 from egorzhdan/egorzhdan/crash-any-cast

[cxx-interop] Fix a crash when calling `std::any_cast` from Swift
This commit is contained in:
Egor Zhdan
2025-10-10 12:58:54 +01:00
committed by GitHub
3 changed files with 35 additions and 0 deletions

View File

@@ -1,3 +1,9 @@
module StdAny {
header "std-any.h"
requires cplusplus
export *
}
module StdNumeric {
header "std-numeric.h"
requires cplusplus

View File

@@ -0,0 +1,6 @@
#include <any>
#include <string>
inline std::any getStdAnyString() {
return std::string("abc210");
}

View File

@@ -0,0 +1,23 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++17)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -std=c++20)
// REQUIRES: executable_test
// In Microsoft STL, all overloads of std::any_cast return a dependent templated
// type, which Swift isn't able to instantiate.
// UNSUPPORTED: OS=windows-msvc
import StdlibUnittest
import StdAny
import CxxStdlib
var StdAnyTestSuite = TestSuite("StdAny")
StdAnyTestSuite.test("std.any_cast<std.string>") {
let a1 = getStdAnyString()
let c1 = std.any_cast(a1) as std.string
expectEqual(c1, std.string("abc210"))
}
runAllTests()