https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91661

--- Comment #4 from anlauf at gcc dot gnu.org ---
The following variant of the patch in comment#3 regtests cleanly and handles
more complex cases, like

  integer, parameter :: b(a(1)+a(2)) = 3

Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c  (Revision 278972)
+++ gcc/fortran/decl.c  (Arbeitskopie)
@@ -29,6 +29,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "parse.h"
 #include "constructor.h"
 #include "target.h"
+#include "arith.h"

 /* Macros to access allocate memory for gfc_data_variable,
    gfc_data_value and gfc_data.  */
@@ -2587,8 +2588,20 @@ variable_decl (int elem)
              if (e->expr_type != EXPR_CONSTANT)
                {
                  n = gfc_copy_expr (e);
+                 switch (n->expr_type)
+                   {
+                   case EXPR_VARIABLE:
+                   case EXPR_FUNCTION:
+                   case EXPR_OP:
+                     gfc_resolve_expr (n);
+                     break;
+
+                   default:
+                     break;
+                   }
                  gfc_simplify_expr (n, 1);
-                 if (n->expr_type == EXPR_CONSTANT)
+                 if (n->expr_type == EXPR_CONSTANT
+                     && gfc_range_check (n) == ARITH_OK)
                    gfc_replace_expr (e, n);
                  else
                    gfc_free_expr (n);
@@ -2597,8 +2610,20 @@ variable_decl (int elem)
              if (e->expr_type != EXPR_CONSTANT)
                {
                  n = gfc_copy_expr (e);
+                 switch (n->expr_type)
+                   {
+                   case EXPR_VARIABLE:
+                   case EXPR_FUNCTION:
+                   case EXPR_OP:
+                     gfc_resolve_expr (n);
+                     break;
+
+                   default:
+                     break;
+                   }
                  gfc_simplify_expr (n, 1);
-                 if (n->expr_type == EXPR_CONSTANT)
+                 if (n->expr_type == EXPR_CONSTANT
+                     && gfc_range_check (n) == ARITH_OK)
                    gfc_replace_expr (e, n);
                  else
                    gfc_free_expr (n);

Given the clumsiness to circumvent the issue with type-bound procedures,
I feel that the simplifications involved should be done at an earlier stage.
Anybody knows where that might be?

Reply via email to