diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 99e56366c4..56cda30dcb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -416,6 +416,7 @@ set(TINT_TEST_SRCS validator/validator_control_block_test.cc validator/validator_function_test.cc validator/validator_test.cc + validator/validator_type_test.cc ) ## Tint library diff --git a/src/validator/validator_type_test.cc b/src/validator/validator_type_test.cc new file mode 100644 index 0000000000..37737389f3 --- /dev/null +++ b/src/validator/validator_type_test.cc @@ -0,0 +1,87 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "gtest/gtest.h" +#include "src/ast/array_accessor_expression.h" +#include "src/ast/struct.h" +#include "src/ast/struct_member.h" +#include "src/ast/struct_member_decoration.h" +#include "src/ast/type/array_type.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/struct_type.h" +#include "src/ast/type_constructor_expression.h" +#include "src/validator/validator_impl.h" +#include "src/validator/validator_test_helper.h" + +namespace tint { +namespace { + +class ValidatorTypeTest : public ValidatorTestHelper, public testing::Test {}; + +TEST_F(ValidatorTypeTest, RuntimeArrayIsLast_Pass) { + // struct Foo { + // vf: f32; + // rt: array; + // }; + + ast::type::F32Type f32; + ast::type::ArrayType arr(&f32); + ast::StructMemberList members; + { + ast::StructMemberDecorationList deco; + members.push_back(create("vf", &f32, deco)); + } + { + ast::StructMemberDecorationList deco; + members.push_back(create( + Source{Source::Location{12, 34}}, "rt", &arr, deco)); + } + ast::StructDecorationList decos; + auto* st = create(decos, members); + ast::type::StructType struct_type("Foo", st); + + // mod()->AddConstructedType(&struct_type); + // EXPECT_TRUE(v()->ValidateConstructedTypes(mod()->constructed_types())); +} + +TEST_F(ValidatorTypeTest, DISABLED_RuntimeArrayIsNotLast_Fail) { + // struct Foo { + // rt: array; + // vf: f32; + // }; + + ast::type::F32Type f32; + ast::type::ArrayType arr(&f32); + ast::StructMemberList members; + { + ast::StructMemberDecorationList deco; + members.push_back(create( + Source{Source::Location{12, 34}}, "rt", &arr, deco)); + } + { + ast::StructMemberDecorationList deco; + members.push_back(create("vf", &f32, deco)); + } + ast::StructDecorationList decos; + auto* st = create(decos, members); + ast::type::StructType struct_type("Foo", st); + + mod()->AddConstructedType(&struct_type); + // EXPECT_FALSE(v()->ValidateConstructedTypes(mod()->constructed_types())); + // EXPECT_EQ(v()->error(), + // "12:34: v-0015: runtime arrays may only appear as the last member + // " "of a struct: 'rt'"); +} +} // namespace +} // namespace tint