22
22
#include < cstdint>
23
23
#include < stdlib.h>
24
24
#include < cstdint>
25
- #include < optional>
26
25
#if defined(_WIN32)
27
26
#include < malloc.h>
28
27
#endif
@@ -198,8 +197,6 @@ extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
198
197
199
198
extern " C" int $ss5ErrorMp; // external global %swift.protocol, align 4
200
199
201
- extern " C" int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
202
-
203
200
extern " C"
204
201
const void * _Nullable
205
202
swift_getTypeByMangledNameInContext (
@@ -222,6 +219,7 @@ struct SymbolicP {
222
219
223
220
inline const void *_Nullable testErrorCall () {
224
221
static swift::SymbolicP errorSymbol;
222
+ static int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
225
223
errorSymbol._1 = 2 ;
226
224
errorSymbol._2 = static_cast <uint32_t >(reinterpret_cast <uintptr_t >(&got_ss5ErrorMp) - reinterpret_cast <uintptr_t >(&errorSymbol._2 ));
227
225
errorSymbol._3 [0 ] = ' _' ;
@@ -255,27 +253,27 @@ class Error {
255
253
opaqueValue = other.opaqueValue ;
256
254
}
257
255
256
+ // FIXME: Return a Swift::Optional instead.
258
257
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)] ;
261
260
const void *em = testErrorCall ();
262
261
void *ep = getPointerToOpaquePointer ();
263
262
auto metadata = swift::TypeMetadataTrait<T>::getTypeMetadata ();
264
263
265
264
// Dynamic cast will release the error, so we need to retain it.
266
265
swift::swift_errorRetain (ep);
267
266
bool dynamicCast =
268
- swift::swift_dynamicCast (ptr , &ep, em, metadata,
267
+ swift::swift_dynamicCast (buffer , &ep, em, metadata,
269
268
/* take on success destroy on failure*/ 6 );
270
269
271
270
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);
275
273
});
276
- return std::optional (result);
277
274
}
278
- return std::nullopt;
275
+ abort ();
276
+ // FIXME: return nil.
279
277
}
280
278
281
279
private:
0 commit comments