[llvm] f0d2a55 - Restore isa<Ty>(X) asserts inside cast<Ty>(X)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 8 07:32:48 PDT 2022


Author: Philip Reames
Date: 2022-06-08T07:32:37-07:00
New Revision: f0d2a55d3aa68827ad708a16650bb0b341ca65bb

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

LOG: Restore isa<Ty>(X) asserts inside cast<Ty>(X)

PLEASE DO NOT REVERT without careful consideration, and preferably prior
discussion.

cast<Ty>(X) is a "checked cast". Its entire purpose is explicitly documented
(https://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast
templates) as catching bad casts by asserting that the cast is valid.
Unfortunately, in a recent rewrite of our casting infrastructure about three
months back, these asserts got dropped.

This is discussed in more detail on discourse in https://discourse.llvm.org/t/cast-x-is-broken-implications-and-proposal-to-address/63033.

Differential Revision: https://reviews.llvm.org/D127231

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h
index 37f85da868b2d..21c981ad3255e 100644
--- a/llvm/include/llvm/Support/Casting.h
+++ b/llvm/include/llvm/Support/Casting.h
@@ -563,21 +563,25 @@ LLVM_NODISCARD inline bool isa(const From &Val) {
 
 template <typename To, typename From>
 LLVM_NODISCARD inline decltype(auto) cast(const From &Val) {
+  assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
   return CastInfo<To, const From>::doCast(Val);
 }
 
 template <typename To, typename From>
 LLVM_NODISCARD inline decltype(auto) cast(From &Val) {
+  assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
   return CastInfo<To, From>::doCast(Val);
 }
 
 template <typename To, typename From>
 LLVM_NODISCARD inline decltype(auto) cast(From *Val) {
+  assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
   return CastInfo<To, From *>::doCast(Val);
 }
 
 template <typename To, typename From>
 LLVM_NODISCARD inline decltype(auto) cast(std::unique_ptr<From> &&Val) {
+  assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!");
   return CastInfo<To, std::unique_ptr<From>>::doCast(std::move(Val));
 }
 


        


More information about the llvm-commits mailing list