Require initializers on const variable declarations

Bug: skia:10837
Change-Id: I33da2eb1e723ed04ab62d65c21e54306dd362bed
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/372677
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/resources/sksl/errors/InvalidAssignment.sksl b/resources/sksl/errors/InvalidAssignment.sksl
index af58e63..efffed2 100644
--- a/resources/sksl/errors/InvalidAssignment.sksl
+++ b/resources/sksl/errors/InvalidAssignment.sksl
@@ -6,14 +6,14 @@
 
 void assign_to_literal()                      { 1 = 2; }
 void assign_to_uniform()                      { u = 0; }
-void assign_to_const()                        { const int x; x = 0; }
-void assign_to_const_array()                  { const int x[1]; x[0] = 0; }
-void assign_to_const_swizzle()                { const half4 x; x.w = 0; }
+void assign_to_const()                        { const int x = 1; x = 0; }
+
+void assign_to_const_swizzle()                { const half4 x = half4(1); x.w = 0; }
 void assign_to_repeated_swizzle()             { half4 x; x.yy = half2(0); }
-void assign_to_const_struct()                 { const S s; s.f = 0; }
-void assign_to_foldable_ternary_const_left()  { const float l; float r; (true ? l : r) = 0; }
-void assign_to_foldable_ternary_const_right() { float l; const float r; (false ? l : r) = 0; }
-void assign_to_foldable_ternary_const_both()  { const float l; const float r; (true ? l : r) = 0; }
+
+void assign_to_foldable_ternary_const_left()  { const float l = 1; float r; (true ? l : r) = 0; }
+void assign_to_foldable_ternary_const_right() { float l; const float r = 1; (false ? l : r) = 0; }
+void assign_to_foldable_ternary_const_both()  { const float l = 1; const float r = 1; (true ? l : r) = 0; }
 void assign_to_unfoldable_ternary()           { float l, r; (sqrt(1) > 0 ? l : r) = 0; }
 void assign_to_unary_minus()                  { float x; -x = 0; }
 void assign_to_unary_plus()                   { float x; +x = 0; }  // TODO(skbug.com/10766)
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index 9e88f93..0f0d9ed 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -454,6 +454,10 @@
 
 std::unique_ptr<Statement> IRGenerator::convertVarDeclaration(std::unique_ptr<Variable> var,
                                                               std::unique_ptr<Expression> value) {
+    if ((var->modifiers().fFlags & Modifiers::kConst_Flag) && !value) {
+        this->errorReporter().error(var->fOffset, "'const' variables must be initialized");
+        return nullptr;
+    }
     if (value) {
         if (var->type().isOpaque()) {
             this->errorReporter().error(
diff --git a/tests/sksl/errors/BadModifiers.glsl b/tests/sksl/errors/BadModifiers.glsl
index 05f5fe9..c0e8b58 100644
--- a/tests/sksl/errors/BadModifiers.glsl
+++ b/tests/sksl/errors/BadModifiers.glsl
@@ -17,4 +17,5 @@
 error: 5: 'varying' is only permitted in runtime effects
 error: 5: 'sk_has_side_effects' is not permitted here
 error: 5: 'inline' is not permitted here
-17 errors
+error: 5: 'const' variables must be initialized
+18 errors
diff --git a/tests/sksl/errors/InvalidAssignment.glsl b/tests/sksl/errors/InvalidAssignment.glsl
index 865e758..92a11e2 100644
--- a/tests/sksl/errors/InvalidAssignment.glsl
+++ b/tests/sksl/errors/InvalidAssignment.glsl
@@ -3,10 +3,8 @@
 error: 7: cannot assign to this expression
 error: 8: cannot modify immutable variable 'u'
 error: 9: cannot modify immutable variable 'x'
-error: 10: cannot modify immutable variable 'x'
 error: 11: cannot modify immutable variable 'x'
 error: 12: cannot write to the same swizzle field more than once
-error: 13: cannot modify immutable variable 's'
 error: 14: cannot modify immutable variable 'l'
 error: 15: cannot modify immutable variable 'r'
 error: 16: cannot modify immutable variable 'l'
@@ -15,4 +13,4 @@
 error: 21: cannot modify immutable variable 'x'
 error: 22: cannot modify immutable variable 'x'
 error: 23: cannot modify immutable variable 's'
-15 errors
+13 errors