[PATCH] D140082: [ADT] Deprecate llvm::Optional::transform

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 14 18:47:05 PST 2022


kazu created this revision.
Herald added a project: All.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

I've migrated all known users of llvm::Optional::transform.  This
patch deprecates it to facilitate the migration from llvm::Optional to
std::optional.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140082

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


Index: llvm/unittests/ADT/OptionalTest.cpp
===================================================================
--- llvm/unittests/ADT/OptionalTest.cpp
+++ llvm/unittests/ADT/OptionalTest.cpp
@@ -573,40 +573,6 @@
   EXPECT_EQ(2u, MoveOnly::Destructions);
 }
 
-TEST(OptionalTest, Transform) {
-  Optional<int> A;
-
-  Optional<int> B = A.transform([&](int N) { return N + 1; });
-  EXPECT_FALSE(B.has_value());
-
-  A = 3;
-  Optional<int> C = A.transform([&](int N) { return N + 1; });
-  EXPECT_TRUE(C.has_value());
-  EXPECT_EQ(4, C.value());
-}
-
-TEST(OptionalTest, MoveTransform) {
-  Optional<MoveOnly> A;
-
-  MoveOnly::ResetCounts();
-  Optional<int> B =
-      std::move(A).transform([&](const MoveOnly &M) { return M.val + 2; });
-  EXPECT_FALSE(B.has_value());
-  EXPECT_EQ(0u, MoveOnly::MoveConstructions);
-  EXPECT_EQ(0u, MoveOnly::MoveAssignments);
-  EXPECT_EQ(0u, MoveOnly::Destructions);
-
-  A = MoveOnly(5);
-  MoveOnly::ResetCounts();
-  Optional<int> C =
-      std::move(A).transform([&](const MoveOnly &M) { return M.val + 2; });
-  EXPECT_TRUE(C.has_value());
-  EXPECT_EQ(7, C.value());
-  EXPECT_EQ(0u, MoveOnly::MoveConstructions);
-  EXPECT_EQ(0u, MoveOnly::MoveAssignments);
-  EXPECT_EQ(0u, MoveOnly::Destructions);
-}
-
 struct EqualTo {
   template <typename T, typename U> static bool apply(const T &X, const U &Y) {
     return X == Y;
Index: llvm/include/llvm/ADT/Optional.h
===================================================================
--- llvm/include/llvm/ADT/Optional.h
+++ llvm/include/llvm/ADT/Optional.h
@@ -286,6 +286,7 @@
 
   /// Apply a function to the value if present; otherwise return std::nullopt.
   template <class Function>
+  LLVM_DEPRECATED("Use llvm::transformOptional", "llvm::transformOptional")
   auto transform(const Function &F) const & -> Optional<decltype(F(value()))> {
     if (*this)
       return F(value());
@@ -301,6 +302,7 @@
 
   /// Apply a function to the value if present; otherwise return std::nullopt.
   template <class Function>
+  LLVM_DEPRECATED("Use llvm::transformOptional", "llvm::transformOptional")
   auto transform(
       const Function &F) && -> Optional<decltype(F(std::move(*this).value()))> {
     if (*this)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140082.483062.patch
Type: text/x-patch
Size: 2209 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221215/28b15784/attachment.bin>


More information about the llvm-commits mailing list