[tint][val] Check RuntimeArrays in Vars

Fixes: 415274804
Change-Id: Iba9bfee820ea1dc6319fbef846601b04a75de605
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/241334
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
diff --git a/src/tint/lang/core/ir/validator.cc b/src/tint/lang/core/ir/validator.cc
index f8ee718..c3561e3 100644
--- a/src/tint/lang/core/ir/validator.cc
+++ b/src/tint/lang/core/ir/validator.cc
@@ -1878,6 +1878,16 @@
                 }
                 return true;
             },
+            [&](const core::type::Array* arr) {
+                if (arr->Count()->Is<core::type::RuntimeArrayCount>()) {
+                    auto* mv = root->As<core::type::MemoryView>();
+                    if (mv && mv->AddressSpace() != AddressSpace::kStorage) {
+                        diag() << "runtime arrays must be in the 'storage' address space";
+                        return false;
+                    }
+                }
+                return true;
+            },
             [&](const core::type::Vector* v) {
                 if (!v->Type()->IsScalar()) {
                     diag() << "vector elements, " << NameOf(type) << ", must be scalars";
diff --git a/src/tint/lang/core/ir/validator_value_test.cc b/src/tint/lang/core/ir/validator_value_test.cc
index 0ced475..4ee3573 100644
--- a/src/tint/lang/core/ir/validator_value_test.cc
+++ b/src/tint/lang/core/ir/validator_value_test.cc
@@ -475,7 +475,7 @@
 }
 
 TEST_F(IR_ValidatorTest, Var_Uniform_NotConstructible) {
-    auto* v = b.Var<uniform, array<vec4<f32>>>();
+    auto* v = b.Var<uniform, atomic<u32>>();
     v->SetBindingPoint(0, 0);
     mod.root_block->Append(v);
 
@@ -484,9 +484,9 @@
     EXPECT_THAT(
         res.Failure().reason,
         testing::HasSubstr(
-            R"(:2:45 error: var: vars in the 'uniform' address space must be host-shareable and constructible
-  %1:ptr<uniform, array<vec4<f32>>, read> = var undef @binding_point(0, 0)
-                                            ^^^
+            R"(:2:40 error: var: vars in the 'uniform' address space must be host-shareable and constructible
+  %1:ptr<uniform, atomic<u32>, read> = var undef @binding_point(0, 0)
+                                       ^^^
 )")) << res.Failure();
 }
 
@@ -674,6 +674,40 @@
 )")) << res.Failure();
 }
 
+TEST_F(IR_ValidatorTest, Var_RuntimeArray_NonStorage) {
+    auto* v = b.Var(ty.ptr(AddressSpace::kPrivate, ty.runtime_array(ty.f32()), read_write));
+    mod.root_block->Append(v);
+
+    auto res = ir::Validate(mod);
+    ASSERT_NE(res, Success);
+    EXPECT_THAT(res.Failure().reason,
+                testing::HasSubstr(
+                    R"(:2:3 error: var: runtime arrays must be in the 'storage' address space
+  %1:ptr<private, array<f32>, read_write> = var undef
+  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+)")) << res.Failure();
+}
+
+TEST_F(IR_ValidatorTest, Var_RuntimeArray_NonStorageInStruct) {
+    auto* str_ty = ty.Struct(mod.symbols.New("MyStruct"),
+                             {
+                                 {mod.symbols.New("a"), ty.runtime_array(ty.f32()), {}},
+                             });
+    auto* v = b.Var(ty.ptr(AddressSpace::kHandle, str_ty, read_write));
+    v->SetBindingPoint(0, 0);
+    v->SetInputAttachmentIndex(0);
+    mod.root_block->Append(v);
+
+    auto res = ir::Validate(mod);
+    ASSERT_NE(res, Success);
+    EXPECT_THAT(res.Failure().reason,
+                testing::HasSubstr(
+                    R"(:6:3 error: var: runtime arrays must be in the 'storage' address space
+  %1:ptr<handle, MyStruct, read_write> = var undef @binding_point(0, 0) @input_attachment_index(0)
+  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+)")) << res.Failure();
+}
+
 TEST_F(IR_ValidatorTest, Let_NullResult) {
     auto* v = mod.CreateInstruction<ir::Let>(nullptr, b.Constant(1_i));
 
diff --git a/src/tint/lang/msl/writer/type_test.cc b/src/tint/lang/msl/writer/type_test.cc
index 04571a0..670c11b 100644
--- a/src/tint/lang/msl/writer/type_test.cc
+++ b/src/tint/lang/msl/writer/type_test.cc
@@ -93,15 +93,16 @@
 
 TEST_F(MslWriterTest, EmitType_RuntimeArray) {
     auto* func = b.Function("foo", ty.void_());
+    auto* param =
+        b.FunctionParam("param", ty.ptr(core::AddressSpace::kStorage, ty.array<bool, 0>()));
+    func->SetParams({param});
     b.Append(func->Block(), [&] {
-        b.Var("a", ty.ptr(core::AddressSpace::kFunction, ty.array<bool, 0>()));
         b.Return(func);
     });
 
     ASSERT_TRUE(Generate()) << err_ << output_.msl;
     EXPECT_EQ(output_.msl, MetalHeader() + MetalArray() + R"(
-void foo() {
-  tint_array<bool, 1> a = {};
+void foo(device tint_array<bool, 1>* const param) {
 }
 )");
 }