https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87142
Bug ID: 87142 Summary: Aliasing issue with overloaded assignment and allocatable components Product: gcc Version: 7.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: mscfd at gmx dot net Target Milestone: --- The following code does not work, as the left hand side in "str = str%cs(1:10)" is deallocated, before the right hand side, where str also appears, is evaluated. With gfortran-7.3.0, the output looks right, but I see invalid memory accesses in valgrind. In other circumstances, depending on the length of the string and with 8.1.0, I have also seen wrong results, but then depending on the optimisation level. As far as I know, this is a violation of the standard, which requires that the right hand side is evaluated before the left hand side is modified (i.e. in this case deallocated). module mod implicit none private type, public :: string character(len=:), allocatable :: cs contains procedure, public :: assign generic, public :: assignment(=) => assign end type string contains elemental subroutine assign(self, cs) class(string), intent(inout) :: self character(len=*), intent(in) :: cs ! this is not working if cs and self%cs are aliasing self%cs = cs end subroutine assign end module mod program assign_alias use mod implicit none type(string) :: str str%cs = repeat('0123456789', 100) str = str%cs(1:10) end program assign_alias