tint/resolver: Fix null-deref.

Check the result of Materialize() for an index accessor.

Bug: chromium:1347541
Change-Id: Iac1b030a1923c96d65d531a0ca5fd1f4bd4271b9
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97851
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Auto-Submit: Ben Clayton <bclayton@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/tint/resolver/materialize_test.cc b/src/tint/resolver/materialize_test.cc
index b1ec913..1fbc8f0 100644
--- a/src/tint/resolver/materialize_test.cc
+++ b/src/tint/resolver/materialize_test.cc
@@ -243,7 +243,7 @@
         case Method::kWorkgroupSize:
             return o << "workgroup-size";
         case Method::kRuntimeIndex:
-            return o << "dynamic-index";
+            return o << "runtime-index";
     }
     return o << "<unknown>";
 }
@@ -788,6 +788,9 @@
 
     // arr[abstract_expr]
     kIndex,
+
+    // abstract_expr[runtime-index]
+    kRuntimeIndex,
 };
 
 static std::ostream& operator<<(std::ostream& o, Method m) {
@@ -810,6 +813,8 @@
             return o << "workgroup-size";
         case Method::kIndex:
             return o << "index";
+        case Method::kRuntimeIndex:
+            return o << "runtime-index";
     }
     return o << "<unknown>";
 }
@@ -895,6 +900,10 @@
             GlobalVar("arr", ty.array<i32, 4>(), ast::StorageClass::kPrivate);
             WrapInFunction(IndexAccessor("arr", abstract_expr()));
             break;
+        case Method::kRuntimeIndex:
+            auto* runtime_index = Var("runtime_index", nullptr, Expr(1_i));
+            WrapInFunction(runtime_index, IndexAccessor(abstract_expr(), runtime_index));
+            break;
     }
 
     switch (expectation) {
@@ -952,10 +961,8 @@
 
 /// Methods that support vector materialization
 constexpr Method kVectorMethods[] = {
-    Method::kLet,
-    Method::kVar,
-    Method::kBuiltinArg,
-    Method::kBitcastVec3F32Arg,
+    Method::kLet,          Method::kVar, Method::kBuiltinArg, Method::kBitcastVec3F32Arg,
+    Method::kRuntimeIndex,
 };
 
 /// Methods that support matrix materialization
diff --git a/src/tint/resolver/resolver.cc b/src/tint/resolver/resolver.cc
index 3af15ab..097d591 100644
--- a/src/tint/resolver/resolver.cc
+++ b/src/tint/resolver/resolver.cc
@@ -1434,6 +1434,9 @@
         //     vec2(1, 2)[runtime-index]
         obj = Materialize(obj);
     }
+    if (!obj) {
+        return nullptr;
+    }
     auto* obj_raw_ty = obj->Type();
     auto* obj_ty = obj_raw_ty->UnwrapRef();
     auto* ty = Switch(