[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
Mon Apr 10 21:43:40 PDT 2023


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

Unlike isa<> and cast<>, current implementation of dyn_cast<> fails
to process a std::unique_ptr to a class supporting LLVM RTTI:

// A and B support LLVM RTTI
class A {...}
class B: public A {...}

void foo() {
...

  auto V = std::make_unique<A>();
  auto VB = dyn_cast<B>(std::move(V));
  if (VB)
    ...

...
}


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147991

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


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.512333.patch
Type: text/x-patch
Size: 565 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230411/ed6623ba/attachment.bin>


More information about the llvm-commits mailing list