From 171c542bf791f57400f3211180a94c671c6d58dd Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Wed, 30 Nov 2022 16:02:45 +0000 Subject: [PATCH] tint: improve compile error when calling builder::Val with the wrong type Now that Val() creates a Vecor of Scalar, we get horrible template spew if the input value is of the wrong type. Bug: tint:1581 Change-Id: I464d369e25f6374d3ffce0ee4dc21723b7e533a9 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112323 Kokoro: Kokoro Reviewed-by: Ben Clayton Commit-Queue: Antonio Maiorano --- src/tint/resolver/resolver_test_helper.h | 2 ++ src/tint/traits.h | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/tint/resolver/resolver_test_helper.h b/src/tint/resolver/resolver_test_helper.h index 68a70178a9..fe33c265d0 100644 --- a/src/tint/resolver/resolver_test_helper.h +++ b/src/tint/resolver/resolver_test_helper.h @@ -31,6 +31,7 @@ #include "src/tint/sem/expression.h" #include "src/tint/sem/statement.h" #include "src/tint/sem/variable.h" +#include "src/tint/traits.h" #include "src/tint/utils/vector.h" namespace tint::resolver { @@ -793,6 +794,7 @@ constexpr bool IsValue = std::is_same_v; /// Creates a Value of DataType from a scalar `v` template Value Val(T v) { + static_assert(traits::IsTypeIn, "v must be a Number of bool"); return Value::Create(utils::Vector{v}); } diff --git a/src/tint/traits.h b/src/tint/traits.h index 8dcc65b3d6..441067e6cc 100644 --- a/src/tint/traits.h +++ b/src/tint/traits.h @@ -160,6 +160,22 @@ template using SliceTuple = std::remove_pointer_t(Range()))>; +namespace detail { +/// Base template for IsTypeIn +template +struct IsTypeIn; + +/// Specialization for IsTypeIn +template class TypeContainer, class... Ts> +struct IsTypeIn> : std::disjunction...> {}; +} // namespace detail + +/// Evaluates to true if T is one of the types in the TypeContainer's template arguments. +/// Works for std::variant, std::tuple, std::pair, or any class template where all parameters are +/// types. +template +static constexpr bool IsTypeIn = detail::IsTypeIn::value; + } // namespace tint::traits #endif // SRC_TINT_TRAITS_H_