[validation] v-0015: runtime arrays cannot be declared in a function body.
Bug: tint:345
Change-Id: I547e8981c2b541979add0a40d7f685b7c7f948aa
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33320
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/validator/validator_impl.cc b/src/validator/validator_impl.cc
index 998277f..69f89f7 100644
--- a/src/validator/validator_impl.cc
+++ b/src/validator/validator_impl.cc
@@ -240,6 +240,15 @@
return false;
}
variable_stack_.set(name, decl->variable());
+ if (decl->variable()->type()->UnwrapAll()->IsArray()) {
+ if (decl->variable()->type()->UnwrapAll()->AsArray()->IsRuntimeArray()) {
+ set_error(decl->source(),
+ "v-0015: runtime arrays may only appear as the last "
+ "member of a struct: '" +
+ decl->variable()->name() + "'");
+ return false;
+ }
+ }
return true;
}
diff --git a/src/validator/validator_type_test.cc b/src/validator/validator_type_test.cc
index db6f7f6..95b33eb 100644
--- a/src/validator/validator_type_test.cc
+++ b/src/validator/validator_type_test.cc
@@ -20,11 +20,15 @@
#include "src/ast/type/alias_type.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/f32_type.h"
+#include "src/ast/type/i32_type.h"
#include "src/ast/type/struct_type.h"
#include "src/ast/type_constructor_expression.h"
+#include "src/ast/variable_decl_statement.h"
#include "src/validator/validator_impl.h"
#include "src/validator/validator_test_helper.h"
+#include "src/ast/pipeline_stage.h"
+#include "src/ast/stage_decoration.h"
namespace tint {
namespace {
@@ -145,5 +149,29 @@
EXPECT_TRUE(v()->ValidateConstructedTypes(mod()->constructed_types()));
}
+TEST_F(ValidatorTypeTest, RuntimeArrayInFunction_Fail) {
+ /// [[stage(vertex)]]
+ // fn func -> void { var a : array<i32>; }
+ ast::type::I32Type i32;
+ ast::type::ArrayType array(&i32);
+
+ auto* var = create<ast::Variable>("a", ast::StorageClass::kNone, &array);
+ ast::VariableList params;
+ ast::type::VoidType void_type;
+ auto* body = create<ast::BlockStatement>();
+ body->append(create<ast::VariableDeclStatement>(
+ Source{Source::Location{12, 34}}, var));
+ auto* func = create<ast::Function>("func", params, &void_type, body);
+ func->add_decoration(
+ create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
+ mod()->AddFunction(func);
+
+ EXPECT_TRUE(td()->Determine()) << td()->error();
+ EXPECT_FALSE(v()->Validate(mod()));
+ EXPECT_EQ(v()->error(),
+ "12:34: v-0015: runtime arrays may only appear as the last member "
+ "of a struct: 'a'");
+}
+
} // namespace
} // namespace tint