Skip to content

Commit 17926b9

Browse files
committed
[ConstraintLocator] Statically enforce that all custom path elements inherit from
StoredPointerElement or StoredIntegerElement.
1 parent b6b3d2c commit 17926b9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/Sema/ConstraintLocator.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,36 @@ class LocatorPathElt::ArgumentAttribute final : public StoredIntegerElement<1> {
780780
}
781781
};
782782

783+
namespace details {
784+
template <typename CustomPathElement>
785+
class PathElement {
786+
static constexpr bool hasStoredValueImpl(...) { return false; }
787+
788+
template <unsigned N>
789+
static constexpr bool hasStoredValueImpl(StoredIntegerElement<N> *) { return true; }
790+
791+
template <typename T>
792+
static constexpr bool hasStoredValueImpl(StoredPointerElement<T> *) { return true; }
793+
794+
public:
795+
static constexpr bool hasStoredValue() {
796+
return hasStoredValueImpl(static_cast<CustomPathElement *>(nullptr));
797+
}
798+
};
799+
}
800+
801+
template <typename CustomPathElement>
802+
constexpr bool isValidCustomPathElement() {
803+
return details::PathElement<CustomPathElement>::hasStoredValue();
804+
}
805+
806+
// All custom path element classes must inherit from StoredIntegerElement or StoredPointerElement
807+
#define CUSTOM_LOCATOR_PATH_ELT(Name) \
808+
static_assert(isValidCustomPathElement<LocatorPathElt::Name>(), \
809+
"Custom path elements must inherit from StoredIntegerElement or StoredPointerElement");
810+
#include "ConstraintLocatorPathElts.def"
811+
812+
783813
/// A simple stack-only builder object that constructs a
784814
/// constraint locator without allocating memory.
785815
///

0 commit comments

Comments
 (0)