[llvm] 411512a - Optional: Avoid value() uses

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 17 14:30:40 PST 2022


Author: Fangrui Song
Date: 2022-12-17T22:30:35Z
New Revision: 411512a7ce9c960bb2ac5b976d45f7ca1a2f63de

URL: https://github.com/llvm/llvm-project/commit/411512a7ce9c960bb2ac5b976d45f7ca1a2f63de
DIFF: https://github.com/llvm/llvm-project/commit/411512a7ce9c960bb2ac5b976d45f7ca1a2f63de.diff

LOG: Optional: Avoid value() uses

prerequisite to deprecate the new API value() whose counterpart in
std::optional has undesired exception checking semantics and is
unavailable in older Xcode.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/Optional.h
    llvm/unittests/ADT/OptionalTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h
index 2b8940cdf7e80..331c681342ad1 100644
--- a/llvm/include/llvm/ADT/Optional.h
+++ b/llvm/include/llvm/ADT/Optional.h
@@ -277,18 +277,18 @@ template <typename T> class Optional {
   constexpr bool has_value() const { return Storage.has_value(); }
   constexpr const T *operator->() const { return &Storage.value(); }
   T *operator->() { return &Storage.value(); }
-  constexpr const T &operator*() const & { return value(); }
-  T &operator*() & { return value(); }
+  constexpr const T &operator*() const & { return Storage.value(); }
+  T &operator*() & { return Storage.value(); }
 
   template <typename U> constexpr T value_or(U &&alt) const & {
-    return has_value() ? value() : std::forward<U>(alt);
+    return has_value() ? operator*() : std::forward<U>(alt);
   }
 
   T &&value() && { return std::move(Storage.value()); }
   T &&operator*() && { return std::move(Storage.value()); }
 
   template <typename U> T value_or(U &&alt) && {
-    return has_value() ? std::move(value()) : std::forward<U>(alt);
+    return has_value() ? std::move(operator*()) : std::forward<U>(alt);
   }
 };
 

diff  --git a/llvm/unittests/ADT/OptionalTest.cpp b/llvm/unittests/ADT/OptionalTest.cpp
index bc344a2d8c8a0..af4cf2ec46610 100644
--- a/llvm/unittests/ADT/OptionalTest.cpp
+++ b/llvm/unittests/ADT/OptionalTest.cpp
@@ -34,9 +34,9 @@ void OptionalWorksInConstexpr() {
                 "Default construction and hasValue() are contexpr");
   constexpr auto y1 = Optional<int>(3);
   constexpr Optional<int> y2{3};
-  static_assert(y1.value() == y2.value() && y1.value() == 3,
+  static_assert(*y1 == *y2 && *y1 == 3,
                 "Construction with value and getValue() are constexpr");
-  static_assert(y1.value() == y2.value() && y1.value() == 3,
+  static_assert(*y1 == *y2 && *y1 == 3,
                 "Construction with value and getValue() are constexpr");
   static_assert(Optional<int>{3} >= 2 && Optional<int>{1} < Optional<int>{2},
                 "Comparisons work in constexpr");


        


More information about the llvm-commits mailing list