writer/wgsl: Handle inferred-type variable declarations

Fixes a null-pointer deference crash.

Bug: tint:672
Change-Id: Icfcb1c8754950ea0e074a0b392d0b34748a13827
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/48225
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-04-19 14:37:49 +00:00 committed by Commit Bot service account
parent 238de88266
commit 8198d57ff2
2 changed files with 21 additions and 3 deletions

View File

@ -598,10 +598,14 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) {
}
}
out_ << " " << program_->Symbols().NameFor(var->symbol()) << " : ";
if (!EmitType(sem->DeclaredType())) {
out_ << " " << program_->Symbols().NameFor(var->symbol());
if (var->declared_type()) {
out_ << " : ";
if (!EmitType(var->declared_type())) {
return false;
}
}
if (var->constructor() != nullptr) {
out_ << " = ";

View File

@ -68,6 +68,20 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
EXPECT_EQ(gen.result(), " var<private> a : f32;\n");
}
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_InferredType) {
auto* var = Var("a", nullptr, ast::StorageClass::kFunction, Expr(123));
auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " var a = 123;\n");
}
} // namespace
} // namespace wgsl
} // namespace writer