https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107874
--- Comment #4 from anlauf at gcc dot gnu.org --- The following patch fixes comment#3: diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index 9c2fea8c5f2..2f69c4369ab 100644 --- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -4913,6 +4914,11 @@ gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask) if (mask->expr_type == EXPR_CONSTANT) { + /* The standard requires evaluation of all function arguments. + Simplify only when TSOURCE, FSOURCE are constant expressions. */ + if (!gfc_is_constant_expr (tsource) || !gfc_is_constant_expr (fsource)) + return NULL; + result = gfc_copy_expr (mask->value.logical ? tsource : fsource); /* Parenthesis is needed to get lower bounds of 1. */ result = gfc_get_parentheses (result); This leads to a "regression" for gfortran.dg/merge_init_expr_2.f90, which is due to the pattern matching the old, faulty simplification result. That's trivial to fix, though.