diff --git a/src/tint/sem/constant.h b/src/tint/sem/constant.h index 4b0b0b927d..c0ba9e6b5c 100644 --- a/src/tint/sem/constant.h +++ b/src/tint/sem/constant.h @@ -48,6 +48,10 @@ class Constant { /// Elements is either a vector of AInts or AFloats using Elements = std::variant; + /// Helper that resolves to either AInt or AFloat based on the element type T. + template + using ElementFor = std::conditional_t>, AFloat, AInt>; + /// Helper that resolves to either AInts or AFloats based on the element type T. template using ElementVectorFor = std::conditional_t>, AFloats, AInts>; @@ -164,7 +168,7 @@ Constant::Constant(const sem::Type* ty, std::initializer_list els) ElementVectorFor elements; elements.reserve(els.size()); for (auto el : els) { - elements.emplace_back(AFloat(el)); + elements.emplace_back(ElementFor(el)); } elems_ = Elements{std::move(elements)}; } diff --git a/src/tint/sem/constant_test.cc b/src/tint/sem/constant_test.cc index c334827501..ed9fef8bf0 100644 --- a/src/tint/sem/constant_test.cc +++ b/src/tint/sem/constant_test.cc @@ -29,16 +29,19 @@ using ConstantTest = TestHelper; TEST_F(ConstantTest, ConstructorInitializerList) { { - Constant c(create(), {1_a}); - c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1_a)); }); + auto i = AInt(AInt::kHighest); + Constant c(create(), {i}); + c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(i)); }); } { - Constant c(create(), {1_i}); - c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1_a)); }); + auto i = i32(i32::kHighest); + Constant c(create(), {i}); + c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(i)); }); } { - Constant c(create(), {1_u}); - c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1_a)); }); + auto i = u32(u32::kHighest); + Constant c(create(), {i}); + c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(i)); }); } { Constant c(create(), {false}); @@ -49,16 +52,19 @@ TEST_F(ConstantTest, ConstructorInitializerList) { c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1_a)); }); } { - Constant c(create(), {1.0_a}); - c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1.0_a)); }); + auto f = AFloat(AFloat::kHighest); + Constant c(create(), {f}); + c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(f)); }); } { - Constant c(create(), {1.0_f}); - c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1.0_a)); }); + auto f = f32(f32::kHighest); + Constant c(create(), {f}); + c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(f)); }); } { - Constant c(create(), {1.0_h}); - c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(1.0_a)); }); + auto f = f16(f16::kHighest); + Constant c(create(), {f}); + c.WithElements([&](auto&& vec) { EXPECT_THAT(vec, testing::ElementsAre(f)); }); } }