<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141835>141835</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[flang] Incorrect implementation sequence when the defined assignment is elemental for allocatable component.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
flang:frontend
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
DanielCChen
</td>
</tr>
</table>
<pre>
Consider the following code:
```
module m
type base
integer :: i
contains
procedure :: bassgn
generic :: assignment(=) => bassgn
end type
type, extends(base) :: child
integer :: j
end type
type container
class(base), allocatable :: b1
end type
contains
impure elemental subroutine bassgn( a, b )
class(base), intent(out) :: a
class(base), intent(in) :: b
print*, "in bassgn"
a%i = b%i + 1
select type ( a )
type is ( child )
print*, "a is child"
select type ( b )
type is ( child )
print*, "b is child"
a%j = b%j + 1
end select
end select
end subroutine
end module
program genericAssignmentDtIntrinAssgn027
use m
type(container) :: c1, c2, c3
pointer :: c2
allocatable :: c3
allocate ( c2, c3 )
allocate ( c2%b1, source = child(1,2) )
c3 = c2
end program
```
Expected output:
```
> a.out
in bassgn
a is child
b is child
```
Flang produces:
```
in bassgn
```
The standard says the following about the intrinsic assignment of `c3 = c2` [24-007: 10.2.1.3, paragraph 15, page 178]:
```
For a noncoarray allocatable component the following sequence of operations is applied.
(1) If the component of the variable is allocated, it is deallocated.
(2) If the component of the value of expr is allocated, the corresponding component of the variable is allocated with the same dynamic type and type parameters as the component of the value of expr. If it is an array, it is allocated with the same bounds. The value of the component of the value of expr is then assigned to the corresponding component of the variable using defined assignment if the declared type of the component has a type-bound defined assignment consistent with the component, and intrinsic assignment for the dynamic type of that component otherwise.
```
So `c3%b1` should be allocated with the dynamic type of `child` first before calling the defined assignment `bassgn`. Then both `select type` constructs inside the subroutine should be executed.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVt1yo7AOfhpyoykDTkjDRS7YZj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