Fix all tests so TypeDetermination runs without errors

If TypeDetermination fails, then semantic info may be missing.
We don't want to have to guard against missing semantic nodes in each writer.

Bug: tint:390
Change-Id: I3124b514ce88f83bd3f75747c4ec6c960282f3c6
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40141
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/writer/hlsl/generator_impl_loop_test.cc b/src/writer/hlsl/generator_impl_loop_test.cc
index 33bbd2c..480aeef 100644
--- a/src/writer/hlsl/generator_impl_loop_test.cc
+++ b/src/writer/hlsl/generator_impl_loop_test.cc
@@ -37,7 +37,10 @@
   auto* body = create<ast::BlockStatement>(ast::StatementList{
       create<ast::DiscardStatement>(),
   });
-  auto* l = create<ast::LoopStatement>(body, nullptr);
+  auto* continuing = create<ast::BlockStatement>(ast::StatementList{});
+  auto* l = create<ast::LoopStatement>(body, continuing);
+
+  WrapInFunction(l);
 
   GeneratorImpl& gen = Build();
 
@@ -59,6 +62,8 @@
   });
   auto* l = create<ast::LoopStatement>(body, continuing);
 
+  WrapInFunction(l);
+
   GeneratorImpl& gen = Build();
 
   gen.increment_indent();
@@ -79,6 +84,9 @@
 }
 
 TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) {
+  Global("lhs", ast::StorageClass::kNone, ty.f32());
+  Global("rhs", ast::StorageClass::kNone, ty.f32());
+
   auto* body = create<ast::BlockStatement>(ast::StatementList{
       create<ast::DiscardStatement>(),
   });
@@ -99,6 +107,7 @@
   });
 
   auto* outer = create<ast::LoopStatement>(body, continuing);
+  WrapInFunction(outer);
 
   GeneratorImpl& gen = Build();
 
@@ -151,6 +160,8 @@
   //   }
   // }
 
+  Global("rhs", ast::StorageClass::kNone, ty.f32());
+
   auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f),
                   ast::VariableDecorationList{});
 
@@ -167,6 +178,7 @@
       create<ast::AssignmentStatement>(lhs, rhs),
   });
   auto* outer = create<ast::LoopStatement>(body, continuing);
+  WrapInFunction(outer);
 
   GeneratorImpl& gen = Build();
 
diff --git a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc
index d106a22..8e14553 100644
--- a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc
+++ b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc
@@ -33,7 +33,7 @@
 using HlslGeneratorImplTest_VariableDecl = TestHelper;
 
 TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) {
-  auto* var = Var("a", ast::StorageClass::kNone, ty.f32());
+  auto* var = Global("a", ast::StorageClass::kNone, ty.f32());
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
 
@@ -59,7 +59,7 @@
 }
 
 TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
-  auto* var = Var("a", ast::StorageClass::kNone, ty.array<f32, 5>());
+  auto* var = Global("a", ast::StorageClass::kNone, ty.array<f32, 5>());
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
 
@@ -73,7 +73,7 @@
 
 TEST_F(HlslGeneratorImplTest_VariableDecl,
        Emit_VariableDeclStatement_Function) {
-  auto* var = Var("a", ast::StorageClass::kFunction, ty.f32());
+  auto* var = Global("a", ast::StorageClass::kFunction, ty.f32());
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
 
@@ -86,7 +86,7 @@
 }
 
 TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
-  auto* var = Var("a", ast::StorageClass::kPrivate, ty.f32());
+  auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32());
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
 
@@ -100,8 +100,9 @@
 
 TEST_F(HlslGeneratorImplTest_VariableDecl,
        Emit_VariableDeclStatement_Initializer_Private) {
-  auto* var = Var("a", ast::StorageClass::kNone, ty.f32(), Expr("initializer"),
-                  ast::VariableDecorationList{});
+  Global("initializer", ast::StorageClass::kNone, ty.f32());
+  auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32(),
+                     Expr("initializer"), ast::VariableDecorationList{});
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
 
@@ -114,10 +115,11 @@
 
 TEST_F(HlslGeneratorImplTest_VariableDecl,
        Emit_VariableDeclStatement_Initializer_ZeroVec) {
-  auto* var = Var("a", ast::StorageClass::kNone, ty.vec3<f32>(), vec3<f32>(),
-                  ast::VariableDecorationList{});
+  auto* var = Var("a", ast::StorageClass::kFunction, ty.vec3<f32>(),
+                  vec3<f32>(), ast::VariableDecorationList{});
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
+  WrapInFunction(stmt);
 
   GeneratorImpl& gen = Build();
 
@@ -128,10 +130,11 @@
 
 TEST_F(HlslGeneratorImplTest_VariableDecl,
        Emit_VariableDeclStatement_Initializer_ZeroMat) {
-  auto* var = Var("a", ast::StorageClass::kNone, ty.mat2x3<f32>(),
+  auto* var = Var("a", ast::StorageClass::kFunction, ty.mat2x3<f32>(),
                   mat2x3<f32>(), ast::VariableDecorationList{});
 
   auto* stmt = create<ast::VariableDeclStatement>(var);
+  WrapInFunction(stmt);
 
   GeneratorImpl& gen = Build();
 
diff --git a/src/writer/hlsl/test_helper.h b/src/writer/hlsl/test_helper.h
index 86ee08a..aee03b9 100644
--- a/src/writer/hlsl/test_helper.h
+++ b/src/writer/hlsl/test_helper.h
@@ -21,6 +21,7 @@
 #include <utility>
 
 #include "gtest/gtest.h"
+#include "src/diagnostic/formatter.h"
 #include "src/program_builder.h"
 #include "src/type_determiner.h"
 #include "src/writer/hlsl/generator_impl.h"
@@ -44,7 +45,15 @@
     if (gen_) {
       return *gen_;
     }
+    [&]() {
+      ASSERT_TRUE(IsValid()) << "Builder program is not valid\n"
+                             << diag::Formatter().format(Diagnostics());
+    }();
     program = std::make_unique<Program>(std::move(*this));
+    [&]() {
+      ASSERT_TRUE(program->IsValid())
+          << diag::Formatter().format(program->Diagnostics());
+    }();
     gen_ = std::make_unique<GeneratorImpl>(program.get());
     return *gen_;
   }
diff --git a/src/writer/msl/generator_impl_loop_test.cc b/src/writer/msl/generator_impl_loop_test.cc
index ebfbc13..9741d4f 100644
--- a/src/writer/msl/generator_impl_loop_test.cc
+++ b/src/writer/msl/generator_impl_loop_test.cc
@@ -39,7 +39,8 @@
   auto* body = create<ast::BlockStatement>(ast::StatementList{
       create<ast::DiscardStatement>(),
   });
-  auto* l = create<ast::LoopStatement>(body, nullptr);
+  auto* continuing = create<ast::BlockStatement>(ast::StatementList{});
+  auto* l = create<ast::LoopStatement>(body, continuing);
   WrapInFunction(l);
 
   GeneratorImpl& gen = Build();
@@ -83,6 +84,9 @@
 }
 
 TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
+  Global("lhs", ast::StorageClass::kNone, ty.f32());
+  Global("rhs", ast::StorageClass::kNone, ty.f32());
+
   auto* body = create<ast::BlockStatement>(ast::StatementList{
       create<ast::DiscardStatement>(),
   });
@@ -153,6 +157,8 @@
   //   }
   // }
 
+  Global("rhs", ast::StorageClass::kNone, ty.f32());
+
   auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f),
                   ast::VariableDecorationList{});
 
diff --git a/src/writer/msl/generator_impl_member_accessor_test.cc b/src/writer/msl/generator_impl_member_accessor_test.cc
index 892d2f9..6370bc4 100644
--- a/src/writer/msl/generator_impl_member_accessor_test.cc
+++ b/src/writer/msl/generator_impl_member_accessor_test.cc
@@ -29,6 +29,12 @@
 using MslGeneratorImplTest = TestHelper;
 
 TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) {
+  Global("str", ast::StorageClass::kPrivate,
+         ty.struct_("my_str", create<ast::Struct>(
+                                  ast::StructMemberList{
+                                      Member("mem", ty.f32()),
+                                  },
+                                  ast::StructDecorationList{})));
   auto* expr = MemberAccessor("str", "mem");
   WrapInFunction(expr);
 
diff --git a/src/writer/msl/generator_impl_variable_decl_statement_test.cc b/src/writer/msl/generator_impl_variable_decl_statement_test.cc
index 6e75ee3..33f230e 100644
--- a/src/writer/msl/generator_impl_variable_decl_statement_test.cc
+++ b/src/writer/msl/generator_impl_variable_decl_statement_test.cc
@@ -129,9 +129,8 @@
 }
 
 TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
-  auto* var = Var("a", ast::StorageClass::kPrivate, ty.f32());
+  auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32());
   auto* stmt = create<ast::VariableDeclStatement>(var);
-  WrapInFunction(stmt);
 
   GeneratorImpl& gen = Build();
 
@@ -142,10 +141,10 @@
 }
 
 TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
-  auto* var = Var("a", ast::StorageClass::kNone, ty.f32(), Expr("initializer"),
-                  ast::VariableDecorationList{});
+  Global("initializer", ast::StorageClass::kNone, ty.f32());
+  auto* var = Global("a", ast::StorageClass::kPrivate, ty.f32(),
+                     Expr("initializer"), ast::VariableDecorationList{});
   auto* stmt = create<ast::VariableDeclStatement>(var);
-  WrapInFunction(stmt);
 
   GeneratorImpl& gen = Build();
 
@@ -157,7 +156,7 @@
 TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
   auto* zero_vec = vec3<f32>();
 
-  auto* var = Var("a", ast::StorageClass::kNone, ty.vec3<f32>(), zero_vec,
+  auto* var = Var("a", ast::StorageClass::kFunction, ty.vec3<f32>(), zero_vec,
                   ast::VariableDecorationList{});
   auto* stmt = create<ast::VariableDeclStatement>(var);
   WrapInFunction(stmt);
diff --git a/src/writer/msl/test_helper.h b/src/writer/msl/test_helper.h
index d01fa2f..f8947c1 100644
--- a/src/writer/msl/test_helper.h
+++ b/src/writer/msl/test_helper.h
@@ -20,6 +20,7 @@
 
 #include "gtest/gtest.h"
 #include "src/ast/module.h"
+#include "src/diagnostic/formatter.h"
 #include "src/program_builder.h"
 #include "src/type_determiner.h"
 #include "src/writer/msl/generator_impl.h"
@@ -43,7 +44,15 @@
     if (gen_) {
       return *gen_;
     }
+    [&]() {
+      ASSERT_TRUE(IsValid()) << "Builder program is not valid\n"
+                             << diag::Formatter().format(Diagnostics());
+    }();
     program = std::make_unique<Program>(std::move(*this));
+    [&]() {
+      ASSERT_TRUE(program->IsValid())
+          << diag::Formatter().format(program->Diagnostics());
+    }();
     gen_ = std::make_unique<GeneratorImpl>(program.get());
     return *gen_;
   }