https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120743
--- Comment #5 from anlauf at gcc dot gnu.org ---
(In reply to Mikael Morin from comment #4)
> (In reply to Jerry DeLisle from comment #3)
> >
> > Here is a smaller reproducer.
> >
> ...
> >
> > Delete the if statement inside the do while and the problem diasappears.
>
> For me the problem remains as long as there are substrings of attrlist.
>
> Probable culprit: r16-1096-gafa2de8093a0cd
>
> In gfc_conv_substring:
>
> > gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
> -> start.expr has type gfc_charlen_type_node
>
> > diff = fold_build2 (MINUS_EXPR, size_type_node, start.expr,
> > build_one_cst (size_type_node));
> -> start.expr is used with type size_type node.
>
> Oops.
Untested fix:
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index c8a207609e4..74688a48027 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -2800,8 +2799,9 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
{
tree diff;
- diff = fold_build2 (MINUS_EXPR, size_type_node, start.expr,
- build_one_cst (size_type_node));
+ diff = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, start.expr,
+ build_one_cst (gfc_charlen_type_node));
+ diff = fold_convert (size_type_node, diff);
se->expr
= fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
}