[Bug fortran/67430] reallocate lhs with overloaded assignment operators causes memory error and wrong size

andrew at boldfriend dot org gcc-bugzilla@gcc.gnu.org
Wed Sep 2 20:27:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67430

--- Comment #2 from Andrew Baldwin <andrew at boldfriend dot org> ---
In 7.2.1.4 of my copy of the standard it states:

"A subroutine defines the defined assignment x1 = x2 if"

...

"(5) either
    (a) the ranks of x1 and x2 match those of d1 and d2 or
    (b) the subroutine is elemental, x1 and x2 are conformable, and there is no
other subroutine that defines the assignment."

Since x1 and x2 are not conformable in this instance the defined assignment
(overloaded assignment) my reading is that it should not occur.

However, assigning two elements to an allocatable array allocated to four
elements is not invalid for intrinsic assignment.  Note 7.38 in my copy of the
standard states:

"If C is an allocatable array of rank 1, then

C = PACK(ARRAY,ARRAY>0)

will cause C to contain all the positive elements of ARRAY in array element
order; if C is not allocated or is allocated with the wrong size, it will be
re-allocated to be of the correct size to hold the result of PACK."

This behavior is observed when the overloaded assignment operator is not
provided.  The output in that case is:

 SIZE(A)           2 A:           7           7

It is also observed when the defined assignment does not meet the requirement
(5a).  I.e. replacing the defined assignment with

        SUBROUTINE ASGN_A (A, B)
          CLASS (A_TYPE), INTENT (OUT) :: A(:)
          CLASS (A_TYPE), INTENT (IN) :: B
!
          A(:)%INT = 45
        END SUBROUTINE

and changing the generic binding to a generic interface.  The output in that
case is also:

 SIZE(A)           2 A:           7           7



More information about the Gcc-bugs mailing list