https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128399
--- Comment #1 from Mikael Morin <mikael at gcc dot gnu.org> ---
The problem is an incorrect array indexing in the copy assignment:
> D.5078 = source.30.dim[0].stride;
> S.34 = D.5071;
> while (1)
> {
> if (S.34 > D.5072) goto L.8;
> {
> struct __vtype_prog_T1 * {ref-all} D.5079;
>
> a._vptr = ctmp.28._vptr;
> D.5079 = a._vptr;
> a._vptr->_copy ((struct t1 *) D.5069 + (sizetype) ((S.34 *
> D.5078 + D.5070) * 8), (struct t1 *) a._data.data + (sizetype) (((S.34 +
> D.5076) + D.5073) * a._vptr->_size));
> }
> S.34 = S.34 + 1;
> }
> L.8:;
The indexing of `a' is correctly using the size from the vptr, but the source
is using an incorrect value of 8 as span.
In gfc_trans_allocate, we create a fake symbol to hold the source value, and
use that as assignment rhs to initialize the allocated arrays.
7143 newsym->n.sym = gfc_new_symbol (newsym->name, NULL);
7144 /* The backend_decl is known. It is expr3, which is inserted
7145 here. */
7146 newsym->n.sym->backend_decl = expr3;
7147 e3rhs = gfc_get_expr ();
7148 e3rhs->rank = code->expr3->rank;
7149 e3rhs->corank = code->expr3->corank;
7150 e3rhs->symtree = newsym;
7151 /* Mark the symbol referenced or gfc_trans_assignment will
bug. */
7152 newsym->n.sym->attr.referenced = 1;
7153 e3rhs->expr_type = EXPR_VARIABLE;
and the type is a non-polymorphic value chosen here:
7163 else if (code->expr3->ts.type == BT_CLASS
7164 && !GFC_CLASS_TYPE_P (TREE_TYPE (expr3)))
7165 e3rhs->ts = CLASS_DATA (code->expr3)->ts;
7166 else
7167 e3rhs->ts = code->expr3->ts;
7168 newsym->n.sym->ts = e3rhs->ts;
As expr3 is an array descriptor, it isn't a class container and the type of the
fake symbol is the declared type (so non-polymorphic) of the source.
Then, when creating an array reference of that fake symbol,
build_class_array_ref is unable to guess a class container, and we fall back to
non-polymorphic array indexing. As the descriptor hasn't the flags set for
span indexing, we fall back to regular derived type indexing.