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
This commit is contained in:
Philip Reames 2022-06-08 07:11:12 -07:00 committed by Philip Reames
parent 33ead6e444
commit f0d2a55d3a

View File

@ -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));
}