[PATCH] D147991: [LLVM][Casting.h] Fix dyn_cast for std::unique_ptr.

Aleksandr Bezzubikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 11 17:36:43 PDT 2023


zuban32 updated this revision to Diff 512630.
zuban32 added a comment.

Correct merge base


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147991/new/

https://reviews.llvm.org/D147991

Files:
  llvm/include/llvm/Support/Casting.h
  llvm/unittests/Support/Casting.cpp


Index: llvm/unittests/Support/Casting.cpp
===================================================================
--- llvm/unittests/Support/Casting.cpp
+++ llvm/unittests/Support/Casting.cpp
@@ -212,6 +212,14 @@
   // EXPECT_EQ(F4, null_foo);
   foo *F5 = B1.daz();
   EXPECT_NE(F5, null_foo);
+
+  std::unique_ptr<const bar> BP(B2);
+  auto FP = dyn_cast<foo>(std::move(BP));
+  static_assert(std::is_same_v<std::unique_ptr<const foo>, decltype(FP)>,
+                "Incorrect deduced return type!");
+  EXPECT_NE(FP.get(), null_foo);
+  EXPECT_EQ(BP.get(), nullptr);
+  FP.release();
 }
 
 // All these tests forward to dyn_cast_if_present, so they also provde an
Index: llvm/include/llvm/Support/Casting.h
===================================================================
--- llvm/include/llvm/Support/Casting.h
+++ llvm/include/llvm/Support/Casting.h
@@ -353,9 +353,9 @@
   static inline CastResultType castFailed() { return CastResultType(nullptr); }
 
   static inline CastResultType doCastIfPossible(std::unique_ptr<From> &&f) {
-    if (!Self::isPossible(f))
+    if (!Self::isPossible(f.get()))
       return castFailed();
-    return doCast(f);
+    return doCast(std::forward<std::unique_ptr<From>>(f));
   }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147991.512630.patch
Type: text/x-patch
Size: 1227 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230412/7ed66915/attachment.bin>


More information about the llvm-commits mailing list