[llvm] [Support] Remove AlignedCharArrayUnion from Expected and ErrorOr, NFCI. (PR #127407)

Jonas Hahnfeld via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 22 03:46:47 PST 2025


https://github.com/hahnjo updated https://github.com/llvm/llvm-project/pull/127407

>From 0c829d6fbfbd3e384fb9ad9d08bb583b69d8b08e Mon Sep 17 00:00:00 2001
From: Jonas Hahnfeld <hahnjo at hahnjo.de>
Date: Sun, 16 Feb 2025 17:30:19 +0100
Subject: [PATCH 1/3] Remove unused include

---
 llvm/include/llvm/Support/TrailingObjects.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/include/llvm/Support/TrailingObjects.h b/llvm/include/llvm/Support/TrailingObjects.h
index 9f7c421a87f4e..07cf08df45a6a 100644
--- a/llvm/include/llvm/Support/TrailingObjects.h
+++ b/llvm/include/llvm/Support/TrailingObjects.h
@@ -46,7 +46,6 @@
 #ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
 #define LLVM_SUPPORT_TRAILINGOBJECTS_H
 
-#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Alignment.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MathExtras.h"

>From 433aa9e1e829a2ab7fb2bd54408a9cedc360a114 Mon Sep 17 00:00:00 2001
From: Jonas Hahnfeld <hahnjo at hahnjo.de>
Date: Sun, 16 Feb 2025 20:04:36 +0100
Subject: [PATCH 2/3] [Support] Remove AlignedCharArrayUnion from Expected and
 ErrorOr, NFCI.

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.
---
 llvm/include/llvm/Support/Error.h   | 5 ++---
 llvm/include/llvm/Support/ErrorOr.h | 5 ++---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index c1b809a09bb80..ddb4f9b7eec87 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -16,7 +16,6 @@
 #include "llvm-c/Error.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Config/abi-breaking.h"
-#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -727,8 +726,8 @@ template <class T> class [[nodiscard]] Expected {
   }
 
   union {
-    AlignedCharArrayUnion<storage_type> TStorage;
-    AlignedCharArrayUnion<error_type> ErrorStorage;
+    storage_type TStorage;
+    error_type ErrorStorage;
   };
   bool HasError : 1;
 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
diff --git a/llvm/include/llvm/Support/ErrorOr.h b/llvm/include/llvm/Support/ErrorOr.h
index 97c7abe1f20c5..7f2b0fbce05fd 100644
--- a/llvm/include/llvm/Support/ErrorOr.h
+++ b/llvm/include/llvm/Support/ErrorOr.h
@@ -15,7 +15,6 @@
 #ifndef LLVM_SUPPORT_ERROROR_H
 #define LLVM_SUPPORT_ERROROR_H
 
-#include "llvm/Support/AlignOf.h"
 #include <cassert>
 #include <system_error>
 #include <type_traits>
@@ -252,8 +251,8 @@ class ErrorOr {
   }
 
   union {
-    AlignedCharArrayUnion<storage_type> TStorage;
-    AlignedCharArrayUnion<std::error_code> ErrorStorage;
+    storage_type TStorage;
+    std::error_code ErrorStorage;
   };
   bool HasError : 1;
 };

>From 30c6ee2545f80ec57b673886d9b1e6f9352ca97a Mon Sep 17 00:00:00 2001
From: Jonas Hahnfeld <hahnjo at hahnjo.de>
Date: Sat, 22 Feb 2025 12:18:31 +0100
Subject: [PATCH 3/3] [Support] Remove reinterpret_cast's from Expected and
 ErrorOr

---
 llvm/include/llvm/Support/Error.h   | 8 ++++----
 llvm/include/llvm/Support/ErrorOr.h | 9 +++++----
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index ddb4f9b7eec87..f0580bdd50ee6 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -679,22 +679,22 @@ template <class T> class [[nodiscard]] Expected {
 
   storage_type *getStorage() {
     assert(!HasError && "Cannot get value when an error exists!");
-    return reinterpret_cast<storage_type *>(&TStorage);
+    return &TStorage;
   }
 
   const storage_type *getStorage() const {
     assert(!HasError && "Cannot get value when an error exists!");
-    return reinterpret_cast<const storage_type *>(&TStorage);
+    return &TStorage;
   }
 
   error_type *getErrorStorage() {
     assert(HasError && "Cannot get error when a value exists!");
-    return reinterpret_cast<error_type *>(&ErrorStorage);
+    return &ErrorStorage;
   }
 
   const error_type *getErrorStorage() const {
     assert(HasError && "Cannot get error when a value exists!");
-    return reinterpret_cast<const error_type *>(&ErrorStorage);
+    return &ErrorStorage;
   }
 
   // Used by ExpectedAsOutParameter to reset the checked flag.
diff --git a/llvm/include/llvm/Support/ErrorOr.h b/llvm/include/llvm/Support/ErrorOr.h
index 7f2b0fbce05fd..d195cbb04ec7d 100644
--- a/llvm/include/llvm/Support/ErrorOr.h
+++ b/llvm/include/llvm/Support/ErrorOr.h
@@ -233,21 +233,22 @@ class ErrorOr {
 
   storage_type *getStorage() {
     assert(!HasError && "Cannot get value when an error exists!");
-    return reinterpret_cast<storage_type *>(&TStorage);
+    return &TStorage;
   }
 
   const storage_type *getStorage() const {
     assert(!HasError && "Cannot get value when an error exists!");
-    return reinterpret_cast<const storage_type *>(&TStorage);
+    return &TStorage;
   }
 
   std::error_code *getErrorStorage() {
     assert(HasError && "Cannot get error when a value exists!");
-    return reinterpret_cast<std::error_code *>(&ErrorStorage);
+    return &ErrorStorage;
   }
 
   const std::error_code *getErrorStorage() const {
-    return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
+    assert(HasError && "Cannot get error when a value exists!");
+    return &ErrorStorage;
   }
 
   union {



More information about the llvm-commits mailing list