From f0d2a55d3aa68827ad708a16650bb0b341ca65bb Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Wed, 8 Jun 2022 07:11:12 -0700 Subject: [PATCH] Restore isa(X) asserts inside cast(X) PLEASE DO NOT REVERT without careful consideration, and preferably prior discussion. cast(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 --- llvm/include/llvm/Support/Casting.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h index 37f85da868b2..21c981ad3255 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 LLVM_NODISCARD inline decltype(auto) cast(const From &Val) { + assert(isa(Val) && "cast() argument of incompatible type!"); return CastInfo::doCast(Val); } template LLVM_NODISCARD inline decltype(auto) cast(From &Val) { + assert(isa(Val) && "cast() argument of incompatible type!"); return CastInfo::doCast(Val); } template LLVM_NODISCARD inline decltype(auto) cast(From *Val) { + assert(isa(Val) && "cast() argument of incompatible type!"); return CastInfo::doCast(Val); } template LLVM_NODISCARD inline decltype(auto) cast(std::unique_ptr &&Val) { + assert(isa(Val) && "cast() argument of incompatible type!"); return CastInfo>::doCast(std::move(Val)); }