Index: frontend-passes.c
===================================================================
--- frontend-passes.c	(Revision 179709)
+++ frontend-passes.c	(Arbeitskopie)
@@ -62,6 +62,10 @@ static gfc_code *inserted_block, **changed_stateme
 
 gfc_namespace *current_ns;
 
+/* If we are within any forall loop.  */
+
+static int forall_level;
+
 /* Entry point - run all passes for a namespace.  So far, only an
    optimization pass is run.  */
 
@@ -165,6 +169,12 @@ cfe_register_funcs (gfc_expr **e, int *walk_subtre
 	  || (*e)->ts.u.cl->length->expr_type != EXPR_CONSTANT))
     return 0;
 
+  /* We don't do function elimination within FORALL statements, it can
+     lead to wrong-code in certain circumstances.  */
+
+  if (forall_level > 0)
+    return 0;
+
   /* If we don't know the shape at compile time, we create an allocatable
      temporary variable to hold the intermediate result, but only if
      allocation on assignment is active.  */
@@ -493,6 +503,7 @@ optimize_namespace (gfc_namespace *ns)
 {
 
   current_ns = ns;
+  forall_level = 0;
 
   gfc_code_walker (&ns->code, convert_do_while, dummy_expr_callback, NULL);
   gfc_code_walker (&ns->code, cfe_code, cfe_expr_0, NULL);
@@ -1193,6 +1204,7 @@ gfc_code_walker (gfc_code **c, walk_code_fn_t code
 		    WALK_SUBEXPR (fa->end);
 		    WALK_SUBEXPR (fa->stride);
 		  }
+		forall_level ++;
 		break;
 	      }
 
@@ -1335,6 +1347,10 @@ gfc_code_walker (gfc_code **c, walk_code_fn_t code
 	      WALK_SUBEXPR (b->expr2);
 	      WALK_SUBCODE (b->next);
 	    }
+
+	  if (co->op == EXEC_FORALL || co->op == EXEC_DO_CONCURRENT)
+	    forall_level --;
+
 	}
     }
   return 0;
