@@ -595,17 +595,6 @@ template <> struct CanTypeWrapperTraits<TYPE> { \
595595BEGIN_CAN_TYPE_WRAPPER (TYPE, BASE) \
596596END_CAN_TYPE_WRAPPER (TYPE, BASE)
597597
598- // Disallow direct uses of isa/cast/dyn_cast on Type to eliminate a
599- // certain class of bugs.
600- template <class X > inline bool
601- isa (const Type&) = delete ; // Use TypeBase::is instead.
602- template <class X > inline typename llvm::cast_retty<X, Type>::ret_type
603- cast (const Type&) = delete ; // Use TypeBase::castTo instead.
604- template <class X > inline typename llvm::cast_retty<X, Type>::ret_type
605- dyn_cast (const Type&) = delete ; // Use TypeBase::getAs instead.
606- template <class X > inline typename llvm::cast_retty<X, Type>::ret_type
607- dyn_cast_or_null (const Type&) = delete ;
608-
609598// Permit direct uses of isa/cast/dyn_cast on CanType and preserve
610599// canonicality.
611600template <class X > inline bool isa (CanType type) {
@@ -656,16 +645,6 @@ namespace llvm {
656645 return OS;
657646 }
658647
659- // A Type casts like a TypeBase*.
660- template <> struct simplify_type <const ::swift::Type> {
661- typedef ::swift::TypeBase *SimpleType;
662- static SimpleType getSimplifiedValue (const ::swift::Type &Val) {
663- return Val.getPointer ();
664- }
665- };
666- template <> struct simplify_type < ::swift::Type>
667- : public simplify_type<const ::swift::Type> {};
668-
669648 // Type hashes just like pointers.
670649 template <> struct DenseMapInfo <swift::Type> {
671650 static swift::Type getEmptyKey () {
@@ -716,4 +695,37 @@ namespace llvm {
716695 };
717696} // end namespace llvm
718697
698+ // / Disallow uses of `isa`/`cast`/`dyn_cast` directly on `Type` to eliminate a
699+ // / certain class of bugs.
700+ namespace llvm {
701+
702+ template <class To >
703+ struct CastInfo <To, const swift::Type> {
704+ // FIXME: Without this 'false' indirection, clang from 5.9 toolchain
705+ // triggers the static assert directly on the template. Try removing once
706+ // Linux CI is updated to 6.0.
707+ static constexpr bool False () { return false ; }
708+ static_assert (
709+ False (), "don't use isa/cast/dyn_cast directly on a 'Type' value; "
710+ " instead, use ' isa/cast/dyn_cast<X>(type.getPointer())' to "
711+ " cast the exact type, or use ' type->is/getAs/castTo<X>()' "
712+ " to cast the desugared type, which is usually the right choice" );
713+ };
714+ template <class To>
715+ struct CastInfo<To, swift::Type> : public CastInfo<To, const swift::Type> {};
716+
717+ /// These specializations exist to avoid unhelpful instantiation errors in
718+ /// addition to the above static assertion.
719+ template <>
720+ struct simplify_type<const swift::Type> {
721+ typedef ::swift::TypeBase *SimpleType;
722+ static SimpleType getSimplifiedValue(const swift::Type &Val) {
723+ return Val.getPointer();
724+ }
725+ };
726+ template <>
727+ struct simplify_type<swift::Type> : public simplify_type<const swift::Type> {};
728+
729+ } // namespace llvm
730+
719731#endif
0 commit comments