[PATCH] D147991: [LLVM][Casting.h] Fix dyn_cast for std::unique_ptr.
Alex Bezzubikov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 19 21:32:26 PDT 2023
zuban32 updated this revision to Diff 542299.
zuban32 added a comment.
- Make it reliable for unsuccessful casting cases, fix the test
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
@@ -45,6 +45,10 @@
static bool classof(const base *B) { return true; }
};
+struct derived_nocast : public base {
+ static bool classof(const base *B) { return false; }
+};
+
template <> struct isa_impl<foo, bar> {
static inline bool doit(const bar &Val) {
dbgs() << "Classof: " << &Val << "\n";
@@ -212,6 +216,18 @@
// EXPECT_EQ(F4, null_foo);
foo *F5 = B1.daz();
EXPECT_NE(F5, null_foo);
+
+ auto BP = std::make_unique<const bar>();
+ auto FP = dyn_cast<foo>(BP);
+ static_assert(std::is_same_v<std::unique_ptr<const foo>, decltype(FP)>,
+ "Incorrect deduced return type!");
+ EXPECT_NE(FP.get(), nullptr);
+ EXPECT_EQ(BP.get(), nullptr);
+
+ auto BP2 = std::make_unique<base>();
+ auto DP = dyn_cast<derived_nocast>(BP2);
+ EXPECT_EQ(DP.get(), nullptr);
+ EXPECT_NE(BP2.get(), nullptr);
}
// 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
@@ -352,10 +352,10 @@
static inline CastResultType castFailed() { return CastResultType(nullptr); }
- static inline CastResultType doCastIfPossible(std::unique_ptr<From> &&f) {
- if (!Self::isPossible(f))
+ static inline CastResultType doCastIfPossible(std::unique_ptr<From> &f) {
+ if (!Self::isPossible(f.get()))
return castFailed();
- return doCast(f);
+ return doCast(std::move(f));
}
};
@@ -665,10 +665,9 @@
}
template <typename To, typename From>
-[[nodiscard]] inline decltype(auto) dyn_cast(std::unique_ptr<From> &&Val) {
+[[nodiscard]] inline decltype(auto) dyn_cast(std::unique_ptr<From> &Val) {
assert(detail::isPresent(Val) && "dyn_cast on a non-existent value");
- return CastInfo<To, std::unique_ptr<From>>::doCastIfPossible(
- std::forward<std::unique_ptr<From> &&>(Val));
+ return CastInfo<To, std::unique_ptr<From>>::doCastIfPossible(Val);
}
/// isa_and_present<X> - Functionally identical to isa, except that a null value
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147991.542299.patch
Type: text/x-patch
Size: 2289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230720/3e3e766a/attachment.bin>
More information about the llvm-commits
mailing list