[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>
This commit is contained in:
Sarah Mashayekhi 2020-11-19 14:05:01 +00:00 committed by Commit Bot service account
parent d8ea65bb2c
commit 8bea9671d5
2 changed files with 37 additions and 0 deletions

View File

@ -240,6 +240,15 @@ bool ValidatorImpl::ValidateDeclStatement(
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;
}

View File

@ -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 @@ TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsLast_Pass) {
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