validation: Reject decorations on local variables

These are never valid. The WGSL parser cannot produce them, but the
SPIR-V reader can since these are not always caught by spirv-val.

Fixed: chromium:1239557
Change-Id: Ie19e4534ffb73b61beaa42046b18b2b8a3f7f65b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/62020
Auto-Submit: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Sarah Mashayekhi <sarahmashay@google.com>
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
James Price 2021-08-17 16:09:00 +00:00 committed by Tint LUCI CQ
parent 91181b906f
commit d47a7ef0cf
3 changed files with 19 additions and 11 deletions

View File

@ -778,6 +778,19 @@ TEST_F(VariableDecorationTest, DuplicateDecoration) {
12:34 note: first decoration declared here)");
}
TEST_F(VariableDecorationTest, LocalVariable) {
auto* v = Var("a", ty.f32(),
ast::DecorationList{
create<ast::BindingDecoration>(Source{{12, 34}}, 2),
});
WrapInFunction(v);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: decorations are not valid on local variables");
}
using ConstantDecorationTest = TestWithParams;
TEST_P(ConstantDecorationTest, IsValid) {
auto& params = GetParam();

View File

@ -3366,8 +3366,11 @@ bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) {
}
for (auto* deco : var->decorations()) {
// TODO(bclayton): Validate decorations
Mark(deco);
if (!deco->Is<ast::InternalDecoration>()) {
AddError("decorations are not valid on local variables", deco->source());
return false;
}
}
variable_stack_.set(var->symbol(), info);

View File

@ -864,11 +864,7 @@ TEST_F(ResolverTest, Expr_Identifier_FunctionVariable) {
auto* my_var_b = Expr("my_var");
auto* assign = Assign(my_var_a, my_var_b);
auto* var = Var("my_var", ty.f32(),
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(0),
});
auto* var = Var("my_var", ty.f32());
Func("my_func", ast::VariableList{}, ty.void_(),
{
@ -1937,11 +1933,7 @@ INSTANTIATE_TEST_SUITE_P(ResolverTest,
ast::UnaryOp::kNot));
TEST_F(ResolverTest, StorageClass_SetsIfMissing) {
auto* var = Var("var", ty.i32(),
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(0),
});
auto* var = Var("var", ty.i32());
auto* stmt = Decl(var);
Func("func", ast::VariableList{}, ty.void_(), {stmt}, ast::DecorationList{});