Skip to content

Commit 9e82ee5

Browse files
authored
[Support] Remove AlignedCharArrayUnion from Expected and ErrorOr, NFCI. (#127407)
They were instantiated with only a single type and union-members themselves. By putting the types directly into a union, they are still left uninitialized by default.
1 parent 06cc968 commit 9e82ee5

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

llvm/include/llvm/Support/Error.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm-c/Error.h"
1717
#include "llvm/ADT/Twine.h"
1818
#include "llvm/Config/abi-breaking.h"
19-
#include "llvm/Support/AlignOf.h"
2019
#include "llvm/Support/Compiler.h"
2120
#include "llvm/Support/Debug.h"
2221
#include "llvm/Support/ErrorHandling.h"
@@ -680,22 +679,22 @@ template <class T> class [[nodiscard]] Expected {
680679

681680
storage_type *getStorage() {
682681
assert(!HasError && "Cannot get value when an error exists!");
683-
return reinterpret_cast<storage_type *>(&TStorage);
682+
return &TStorage;
684683
}
685684

686685
const storage_type *getStorage() const {
687686
assert(!HasError && "Cannot get value when an error exists!");
688-
return reinterpret_cast<const storage_type *>(&TStorage);
687+
return &TStorage;
689688
}
690689

691690
error_type *getErrorStorage() {
692691
assert(HasError && "Cannot get error when a value exists!");
693-
return reinterpret_cast<error_type *>(&ErrorStorage);
692+
return &ErrorStorage;
694693
}
695694

696695
const error_type *getErrorStorage() const {
697696
assert(HasError && "Cannot get error when a value exists!");
698-
return reinterpret_cast<const error_type *>(&ErrorStorage);
697+
return &ErrorStorage;
699698
}
700699

701700
// Used by ExpectedAsOutParameter to reset the checked flag.
@@ -727,8 +726,8 @@ template <class T> class [[nodiscard]] Expected {
727726
}
728727

729728
union {
730-
AlignedCharArrayUnion<storage_type> TStorage;
731-
AlignedCharArrayUnion<error_type> ErrorStorage;
729+
storage_type TStorage;
730+
error_type ErrorStorage;
732731
};
733732
bool HasError : 1;
734733
#if LLVM_ENABLE_ABI_BREAKING_CHECKS

llvm/include/llvm/Support/ErrorOr.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#ifndef LLVM_SUPPORT_ERROROR_H
1616
#define LLVM_SUPPORT_ERROROR_H
1717

18-
#include "llvm/Support/AlignOf.h"
1918
#include <cassert>
2019
#include <system_error>
2120
#include <type_traits>
@@ -234,26 +233,27 @@ class ErrorOr {
234233

235234
storage_type *getStorage() {
236235
assert(!HasError && "Cannot get value when an error exists!");
237-
return reinterpret_cast<storage_type *>(&TStorage);
236+
return &TStorage;
238237
}
239238

240239
const storage_type *getStorage() const {
241240
assert(!HasError && "Cannot get value when an error exists!");
242-
return reinterpret_cast<const storage_type *>(&TStorage);
241+
return &TStorage;
243242
}
244243

245244
std::error_code *getErrorStorage() {
246245
assert(HasError && "Cannot get error when a value exists!");
247-
return reinterpret_cast<std::error_code *>(&ErrorStorage);
246+
return &ErrorStorage;
248247
}
249248

250249
const std::error_code *getErrorStorage() const {
251-
return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
250+
assert(HasError && "Cannot get error when a value exists!");
251+
return &ErrorStorage;
252252
}
253253

254254
union {
255-
AlignedCharArrayUnion<storage_type> TStorage;
256-
AlignedCharArrayUnion<std::error_code> ErrorStorage;
255+
storage_type TStorage;
256+
std::error_code ErrorStorage;
257257
};
258258
bool HasError : 1;
259259
};

llvm/include/llvm/Support/TrailingObjects.h

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
4747
#define LLVM_SUPPORT_TRAILINGOBJECTS_H
4848

49-
#include "llvm/Support/AlignOf.h"
5049
#include "llvm/Support/Alignment.h"
5150
#include "llvm/Support/Compiler.h"
5251
#include "llvm/Support/MathExtras.h"

0 commit comments

Comments
 (0)