[validation] add const variables shouldn't have a storage class
This CL adds a rule to separate global variables and global consts in regards to storage class:
v-0022: Global variables must have a storage class
v-global01: const variables shouldn't have a storage class
Bug: tint: 225
Change-Id: I53d84afd771c78d91eaf47568e954986abf38e58
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/28141
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: David Neto <dneto@google.com>
diff --git a/src/validator_impl.cc b/src/validator_impl.cc
index d504a93..e9e5ac1 100644
--- a/src/validator_impl.cc
+++ b/src/validator_impl.cc
@@ -62,11 +62,17 @@
                 "v-0011: redeclared global identifier '" + var->name() + "'");
       return false;
     }
-    if (var->storage_class() == ast::StorageClass::kNone) {
+    if (!var->is_const() && var->storage_class() == ast::StorageClass::kNone) {
       set_error(var->source(),
                 "v-0022: global variables must have a storage class");
       return false;
     }
+    if (var->is_const() &&
+        !(var->storage_class() == ast::StorageClass::kNone)) {
+      set_error(var->source(),
+                "v-global01: global constants shouldn't have a storage class");
+      return false;
+    }
     variable_stack_.set_global(var->name(), var.get());
   }
   return true;
diff --git a/src/validator_test.cc b/src/validator_test.cc
index 32bf883..2683377 100644
--- a/src/validator_test.cc
+++ b/src/validator_test.cc
@@ -280,6 +280,32 @@
   EXPECT_EQ(v()->error(),
             "12:34: v-0022: global variables must have a storage class");
 }
+TEST_F(ValidatorTest, GlobalConstantWithStorageClass_Fail) {
+  // const<in> gloabl_var: f32;
+  ast::type::F32Type f32;
+  auto global_var = std::make_unique<ast::Variable>(
+      Source{12, 34}, "global_var", ast::StorageClass::kInput, &f32);
+  global_var->set_is_const(true);
+
+  mod()->AddGlobalVariable(std::move(global_var));
+  EXPECT_TRUE(td()->Determine()) << td()->error();
+  EXPECT_FALSE(v()->Validate(mod()));
+  EXPECT_EQ(
+      v()->error(),
+      "12:34: v-global01: global constants shouldn't have a storage class");
+}
+
+TEST_F(ValidatorTest, GlobalConstNoStorageClass_Pass) {
+  // const gloabl_var: f32;
+  ast::type::F32Type f32;
+  auto global_var = std::make_unique<ast::Variable>(
+      Source{12, 34}, "global_var", ast::StorageClass::kNone, &f32);
+  global_var->set_is_const(true);
+
+  mod()->AddGlobalVariable(std::move(global_var));
+  EXPECT_TRUE(td()->Determine()) << td()->error();
+  EXPECT_FALSE(v()->Validate(mod())) << v()->error();
+}
 
 TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
   // var global_var: f32 = 2.1;