[validation] add implementation and unit test for v-0031

v-0031: A struct containing a runtime array must be in the 'storage' storage class

Change-Id: I3f7f8bd70cb24514815d7fc19858f64fd40860ac
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33361
Commit-Queue: Sarah Mashayekhi <sarahmashay@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Sarah Mashayekhi 2020-11-20 16:57:54 +00:00 committed by Commit Bot service account
parent 7f075c2a88
commit ca03041ad7
2 changed files with 54 additions and 7 deletions

View File

@ -73,12 +73,21 @@ bool ValidatorImpl::ValidateConstructedTypes(
for (auto* member : st->impl()->members()) {
if (member->type()->UnwrapAll()->IsArray()) {
auto* r = member->type()->UnwrapAll()->AsArray();
if (r->IsRuntimeArray() && member != st->impl()->members().back()) {
set_error(member->source(),
"v-0015: runtime arrays may only appear as the last "
"member of a struct: '" +
member->name() + "'");
return false;
if (r->IsRuntimeArray()) {
if (member != st->impl()->members().back()) {
set_error(member->source(),
"v-0015: runtime arrays may only appear as the last "
"member of a struct: '" +
member->name() + "'");
return false;
}
if (!st->IsBlockDecorated()) {
set_error(member->source(),
"v-0031: a struct containing a runtime-sized array "
"must be in the 'storage' storage class: '" +
st->name() + "'");
return false;
}
}
}
}

View File

@ -15,6 +15,7 @@
#include "gtest/gtest.h"
#include "src/ast/array_accessor_expression.h"
#include "src/ast/struct.h"
#include "src/ast/struct_block_decoration.h"
#include "src/ast/struct_member.h"
#include "src/ast/struct_member_decoration.h"
#include "src/ast/type/alias_type.h"
@ -35,6 +36,34 @@ namespace {
class ValidatorTypeTest : public ValidatorTestHelper, public testing::Test {};
TEST_F(ValidatorTypeTest, RuntimeArrayIsLast_Pass) {
// [[Block]]
// struct Foo {
// vf: f32;
// rt: array<f32>;
// };
ast::type::F32Type f32;
ast::type::ArrayType arr(&f32);
ast::StructMemberList members;
{
ast::StructMemberDecorationList deco;
members.push_back(create<ast::StructMember>("vf", &f32, deco));
}
{
ast::StructMemberDecorationList deco;
members.push_back(create<ast::StructMember>(
Source{Source::Location{12, 34}}, "rt", &arr, deco));
}
ast::StructDecorationList decos;
decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto* st = create<ast::Struct>(decos, members);
ast::type::StructType struct_type("Foo", st);
mod()->AddConstructedType(&struct_type);
EXPECT_TRUE(v()->ValidateConstructedTypes(mod()->constructed_types()));
}
TEST_F(ValidatorTypeTest, RuntimeArrayIsLastNoBlock_Fail) {
// struct Foo {
// vf: f32;
// rt: array<f32>;
@ -57,10 +86,14 @@ TEST_F(ValidatorTypeTest, RuntimeArrayIsLast_Pass) {
ast::type::StructType struct_type("Foo", st);
mod()->AddConstructedType(&struct_type);
EXPECT_TRUE(v()->ValidateConstructedTypes(mod()->constructed_types()));
EXPECT_FALSE(v()->ValidateConstructedTypes(mod()->constructed_types()));
EXPECT_EQ(v()->error(),
"12:34: v-0031: a struct containing a runtime-sized array must be "
"in the 'storage' storage class: 'Foo'");
}
TEST_F(ValidatorTypeTest, RuntimeArrayIsNotLast_Fail) {
// [[Block]]
// struct Foo {
// rt: array<f32>;
// vf: f32;
@ -79,6 +112,7 @@ TEST_F(ValidatorTypeTest, RuntimeArrayIsNotLast_Fail) {
members.push_back(create<ast::StructMember>("vf", &f32, deco));
}
ast::StructDecorationList decos;
decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto* st = create<ast::Struct>(decos, members);
ast::type::StructType struct_type("Foo", st);
@ -90,6 +124,7 @@ TEST_F(ValidatorTypeTest, RuntimeArrayIsNotLast_Fail) {
}
TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsNotLast_Fail) {
// [[Block]]
// type RTArr = array<u32>;
// struct s {
// b: RTArr;
@ -112,6 +147,7 @@ TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsNotLast_Fail) {
}
ast::StructDecorationList decos;
decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto* st = create<ast::Struct>(decos, members);
ast::type::StructType struct_type("s", st);
mod()->AddConstructedType(&struct_type);
@ -122,6 +158,7 @@ TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsNotLast_Fail) {
}
TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsLast_Pass) {
// [[Block]]
// type RTArr = array<u32>;
// struct s {
// a: u32;
@ -143,6 +180,7 @@ TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsLast_Pass) {
Source{Source::Location{12, 34}}, "b", &alias, deco));
}
ast::StructDecorationList decos;
decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto* st = create<ast::Struct>(decos, members);
ast::type::StructType struct_type("s", st);
mod()->AddConstructedType(&struct_type);