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>
This commit is contained in:
Brian Osman 2021-02-19 16:26:13 -05:00 committed by Skia Commit-Bot
parent 4d68c244a7
commit 4a015c5092
4 changed files with 14 additions and 11 deletions

View File

@ -6,14 +6,14 @@ uniform int u;
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)

View File

@ -454,6 +454,10 @@ std::unique_ptr<Variable> IRGenerator::convertVar(int offset, const Modifiers& m
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(

View File

@ -17,4 +17,5 @@ error: 5: 'in uniform' variables only permitted within fragment processors
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

View File

@ -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: 18: cannot assign to this expression
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