===================================================================
@@ -4873,7 +4873,7 @@ resolve_array_ref (gfc_array_ref *ar)
static bool
-resolve_substring (gfc_ref *ref)
+resolve_substring (gfc_ref *ref, bool *equal_length)
{
int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
@@ -4944,6 +4944,13 @@ static bool
&ref->u.ss.end->where);
return false;
}
+ /* If the substring has the same length as the original
+ variable, the reference itself can be deleted. */
+
+ if (ref->u.ss.length != NULL
+ && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
+ && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
+ *equal_length = true;
}
return true;
@@ -5037,7 +5044,8 @@ static bool
resolve_ref (gfc_expr *expr)
{
int current_part_dimension, n_components, seen_part_dimension;
- gfc_ref *ref;
+ gfc_ref *ref, **prev;
+ bool equal_length;
for (ref = expr->ref; ref; ref = ref->next)
if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
@@ -5046,7 +5054,8 @@ resolve_ref (gfc_expr *expr)
break;
}
- for (ref = expr->ref; ref; ref = ref->next)
+
+ for (ref = expr->ref, prev = &expr->ref; ref; prev = &ref->next, ref = ref->next)
switch (ref->type)
{
case REF_ARRAY:
@@ -5059,8 +5068,19 @@ resolve_ref (gfc_expr *expr)
break;
case REF_SUBSTRING:
- if (!resolve_substring (ref))
+ equal_length = false;
+ if (!resolve_substring (ref, &equal_length))
return false;
+
+ if (expr->expr_type != EXPR_SUBSTRING && equal_length)
+ {
+ /* Remove the reference and move the charlen, if any. */
+ *prev = ref->next;
+ ref->next = NULL;
+ expr->ts.u.cl = ref->u.ss.length;
+ ref->u.ss.length = NULL;
+ gfc_free_ref_list (ref);
+ }
break;
}