From 8198d57ff235ddf57c9ab48f55a39dcb3037977e Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 19 Apr 2021 14:37:49 +0000 Subject: [PATCH] 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 Reviewed-by: James Price --- src/writer/wgsl/generator_impl.cc | 10 +++++++--- .../generator_impl_variable_decl_statement_test.cc | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 156b266d48..a93a5bf234 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -598,9 +598,13 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) { } } - out_ << " " << program_->Symbols().NameFor(var->symbol()) << " : "; - if (!EmitType(sem->DeclaredType())) { - return false; + out_ << " " << program_->Symbols().NameFor(var->symbol()); + + if (var->declared_type()) { + out_ << " : "; + if (!EmitType(var->declared_type())) { + return false; + } } if (var->constructor() != nullptr) { diff --git a/src/writer/wgsl/generator_impl_variable_decl_statement_test.cc b/src/writer/wgsl/generator_impl_variable_decl_statement_test.cc index e27397f2bd..27fa34f602 100644 --- a/src/writer/wgsl/generator_impl_variable_decl_statement_test.cc +++ b/src/writer/wgsl/generator_impl_variable_decl_statement_test.cc @@ -68,6 +68,20 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Private) { EXPECT_EQ(gen.result(), " var a : f32;\n"); } +TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_InferredType) { + auto* var = Var("a", nullptr, ast::StorageClass::kFunction, Expr(123)); + + auto* stmt = create(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