Skip to content

Commit 8636720

Browse files
committed
Get closer to a working dynamic cast for error
1 parent 5b8a2e2 commit 8636720

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

lib/PrintAsClang/_SwiftCxxInteroperability.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <cstdint>
2323
#include <stdlib.h>
2424
#include <cstdint>
25-
#include <optional>
2625
#if defined(_WIN32)
2726
#include <malloc.h>
2827
#endif
@@ -198,8 +197,6 @@ extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
198197

199198
extern "C" int $ss5ErrorMp; // external global %swift.protocol, align 4
200199

201-
extern "C" int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
202-
203200
extern "C"
204201
const void * _Nullable
205202
swift_getTypeByMangledNameInContext(
@@ -222,6 +219,7 @@ struct SymbolicP {
222219

223220
inline const void *_Nullable testErrorCall() {
224221
static swift::SymbolicP errorSymbol;
222+
static int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
225223
errorSymbol._1 = 2;
226224
errorSymbol._2 = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&got_ss5ErrorMp) - reinterpret_cast<uintptr_t>(&errorSymbol._2));
227225
errorSymbol._3[0] = '_';
@@ -255,27 +253,27 @@ class Error {
255253
opaqueValue = other.opaqueValue;
256254
}
257255

256+
// FIXME: Return a Swift::Optional instead.
258257
template<class T>
259-
std::optional<T> as() {
260-
char *ptr = (char*)malloc(100);
258+
T as() {
259+
alignas(alignof(T)) char buffer[sizeof(T)];
261260
const void *em = testErrorCall();
262261
void *ep = getPointerToOpaquePointer();
263262
auto metadata = swift::TypeMetadataTrait<T>::getTypeMetadata();
264263

265264
// Dynamic cast will release the error, so we need to retain it.
266265
swift::swift_errorRetain(ep);
267266
bool dynamicCast =
268-
swift::swift_dynamicCast(ptr, &ep, em, metadata,
267+
swift::swift_dynamicCast(buffer, &ep, em, metadata,
269268
/*take on success destroy on failure*/ 6);
270269

271270
if (dynamicCast) {
272-
auto result =
273-
swift::_impl::implClassFor<T>::type::returnNewValue([&](char *dest) {
274-
swift::_impl::implClassFor<T>::type::initializeWithTake(dest, ptr);
271+
return swift::_impl::implClassFor<T>::type::returnNewValue([&](char *dest) {
272+
swift::_impl::implClassFor<T>::type::initializeWithTake(dest, buffer);
275273
});
276-
return std::optional(result);
277274
}
278-
return std::nullopt;
275+
abort();
276+
// FIXME: return nil.
279277
}
280278

281279
private:

0 commit comments

Comments
 (0)