resolver: Call ValidateVariable() for globals

This was only called for function-scope variable declarations.
In calling this, there were inevitable tests failing, which have now been fixed.
Added a test for the single runtime-array-length validation rule that this function was checking.

Fixed: tint:345
Change-Id: Ic453c38158c1290a5e1ef6de56af0c621d97982a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/48381
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-04-19 20:23:12 +00:00 committed by Commit Bot service account
parent 85bfea6edd
commit cf28b5aafe
4 changed files with 42 additions and 8 deletions

View File

@ -25,6 +25,7 @@
#include "src/ast/loop_statement.h"
#include "src/ast/return_statement.h"
#include "src/ast/stage_decoration.h"
#include "src/ast/struct_block_decoration.h"
#include "src/ast/switch_statement.h"
#include "src/ast/unary_op_expression.h"
#include "src/ast/variable_decl_statement.h"
@ -758,8 +759,13 @@ INSTANTIATE_TEST_SUITE_P(
using ResolverIntrinsicDataTest = ResolverTest;
TEST_F(ResolverIntrinsicDataTest, ArrayLength_Vector) {
Global("arr", ty.array<int>(), ast::StorageClass::kInput);
auto* call = Call("arrayLength", "arr");
auto* ary = ty.array<i32>();
auto* str = Structure("S", {Member("x", ary)},
{create<ast::StructBlockDecoration>()});
auto* ac = ty.access(ast::AccessControl::kReadOnly, str);
Global("a", ac, ast::StorageClass::kStorage);
auto* call = Call("arrayLength", MemberAccessor("a", "x"));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();

View File

@ -353,7 +353,7 @@ bool Resolver::ValidateGlobalVariable(const VariableInfo* info) {
}
}
return true;
return ValidateVariable(info->declaration);
}
bool Resolver::ValidateVariable(const ast::Variable* var) {

View File

@ -371,9 +371,31 @@ TEST_F(ResolverTypeValidationTest, RuntimeArrayIsNotLast_Fail) {
WrapInFunction();
EXPECT_FALSE(r()->Resolve()) << r()->error();
EXPECT_EQ(r()->error(),
"12:34 error v-0015: runtime arrays may only appear as the last "
"member of a struct");
EXPECT_EQ(
r()->error(),
R"(12:34 error v-0015: runtime arrays may only appear as the last member of a struct)");
}
TEST_F(ResolverTypeValidationTest, RuntimeArrayAsGlobalVariable) {
Global(Source{{56, 78}}, "g", ty.array<i32>(), ast::StorageClass::kPrivate);
ASSERT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(56:78 error v-0015: runtime arrays may only appear as the last member of a struct)");
}
TEST_F(ResolverTypeValidationTest, RuntimeArrayAsLocalVariable) {
auto* v =
Var(Source{{56, 78}}, "g", ty.array<i32>(), ast::StorageClass::kFunction);
WrapInFunction(v);
ASSERT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(56:78 error v-0015: runtime arrays may only appear as the last member of a struct)");
}
TEST_F(ResolverTypeValidationTest, RuntimeArrayAsParameter_Fail) {

View File

@ -59,7 +59,10 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedAlias) {
TEST_F(BuilderTest_Type, GenerateRuntimeArray) {
auto* ary = ty.array(ty.i32(), 0);
Global("a", ary, ast::StorageClass::kInput);
auto* str = Structure("S", {Member("x", ary)},
{create<ast::StructBlockDecoration>()});
auto* ac = ty.access(ast::AccessControl::kReadOnly, str);
Global("a", ac, ast::StorageClass::kStorage);
spirv::Builder& b = Build();
@ -74,7 +77,10 @@ TEST_F(BuilderTest_Type, GenerateRuntimeArray) {
TEST_F(BuilderTest_Type, ReturnsGeneratedRuntimeArray) {
auto* ary = ty.array(ty.i32(), 0);
Global("a", ary, ast::StorageClass::kInput);
auto* str = Structure("S", {Member("x", ary)},
{create<ast::StructBlockDecoration>()});
auto* ac = ty.access(ast::AccessControl::kReadOnly, str);
Global("a", ac, ast::StorageClass::kStorage);
spirv::Builder& b = Build();