Fix ESSL3 indexing corner cases

Indexing interface blocks or fragment outputs with a non-constant
expression is not valid even if ANGLE has been able to constant fold
the expression.

BUG=angleproject:1210
TEST=angle_unittests

Change-Id: I2ccb67871b682976a31b8de306053b9b28c06437
Reviewed-on: https://chromium-review.googlesource.com/312044
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tested-by: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/compiler/translator/ParseContext.cpp b/src/compiler/translator/ParseContext.cpp
index 5cae37c..fd9d2c0 100644
--- a/src/compiler/translator/ParseContext.cpp
+++ b/src/compiler/translator/ParseContext.cpp
@@ -2698,6 +2698,27 @@
 
     TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
 
+    // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
+    // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
+    // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
+    // index is a constant expression.
+    if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
+    {
+        if (baseExpression->isInterfaceBlock())
+        {
+            error(
+                location, "", "[",
+                "array indexes for interface blocks arrays must be constant integral expressions");
+            recover();
+        }
+        else if (baseExpression->getQualifier() == EvqFragmentOut)
+        {
+            error(location, "", "[",
+                  "array indexes for fragment outputs must be constant integral expressions");
+            recover();
+        }
+    }
+
     if (indexConstantUnion)
     {
         // If the index is not qualified as constant, the behavior in the spec is undefined. This
@@ -2790,20 +2811,6 @@
     }
     else
     {
-        if (baseExpression->isInterfaceBlock())
-        {
-            error(
-                location, "", "[",
-                "array indexes for interface blocks arrays must be constant integral expressions");
-            recover();
-        }
-        else if (baseExpression->getQualifier() == EvqFragmentOut)
-        {
-            error(location, "", "[",
-                  "array indexes for fragment outputs must be constant integral expressions");
-            recover();
-        }
-
         indexedExpression =
             intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
     }