Issue 157379
Summary [Flang] Incorrect execution result of _expression_ in structure constructor
Labels flang
Assignees
Reporter ohno-fj
    ```
Version of flang : 22.0.0(eef79c8b49aa45458bbaf895603385a7819cc182)/AArch64
```

In the following statement of the attached program, the processing of `_expression_ (+ia)` in `structure constructor` appears to be incorrect.
```
 str=ty1(+ia)
```
As a result, the value of `derived-type variable (str)` used in `internal subprogram (sub)` appears to be incorrect.

The preceding example assigns `struct constructor (ty1(+ia))` to `derived-type variable (str)`.
`_expression_ (+ia)` in `structure constructor` does not produce correct results.
The above program is `snfmm391_22321.f90`.

Here are the expected results:
```
 1 : str%ia( 1 ) = 11
 1 : str%ia( 2 ) = 12
 1 : str%ia( 3 ) = 13
 1 : str%ia( 4 ) = 14
 1 : str%ia( 5 ) = 15
```

When it is outputted element by element using a DO loop, the result is incorrect.
However, when `str%ia` is outputted at once, the result is correct.

As a test, I check `_expression_ (-ia)` in `struct constructor` and get the correct result.
The above program is `snfmm391_22322.f90`.

The following are the test program, Flang, Gfortran and ifx compilation/execution results.

snfmm391_22321.f90:
```fortran
program main
  type ty1
 integer,allocatable::ia(:)
  end type ty1
  type(ty1) :: str
 integer,allocatable::ia(:)
  allocate(ia(11:15))
  ia=(/(i,i=11,15)/)

 str=ty1(+ia)
  call sub((/(i,i=11,15)/))

contains
  subroutine sub(a2)
 integer ::a2(5)
    write(6,*) "0 : str%ia = ", str%ia
    do i=1,5
 write(6,*) "1 : str%ia(",i,") = ", str%ia(i)
    end do
  end subroutine sub
end program main
```

```
$ flang snfmm391_22321.f90; ./a.out
 0 : str%ia =  11 12 13 14 15
 1 : str%ia( 1 ) =  33
 1 : str%ia( 2 ) =  0
 1 : str%ia( 3 ) =  11
 1 : str%ia( 4 ) =  12
 1 : str%ia( 5 ) = 13
$
```

```
$ gfortran snfmm391_22321.f90; ./a.out
 0 : str%ia = 11          12          13          14          15
 1 : str%ia( 1 ) =           11
 1 : str%ia(           2 ) =           12
 1 : str%ia( 3 ) =           13
 1 : str%ia(           4 ) =           14
 1 : str%ia(           5 ) =           15
$
```

```
$ ifx snfmm391_22321.f90; ./a.out
 0 : str%ia =           11          12 13          14          15
 1 : str%ia(           1 ) =           11
 1 : str%ia(           2 ) =           12
 1 : str%ia(           3 ) = 13
 1 : str%ia(           4 ) =           14
 1 : str%ia(           5 ) = 15
$
```

snfmm391_22322.f90:
```fortran
program main
  type ty1
     integer,allocatable::ia(:)
  end type ty1
  type(ty1) :: str
 integer,allocatable::ia(:)
  allocate(ia(11:15))
  ia=(/(i,i=11,15)/)

 str=ty1(-ia)
  call sub((/(i,i=11,15)/))

contains
  subroutine sub(a2)
 integer ::a2(5)
    write(6,*) "0 : str%ia = ", str%ia
    do i=1,5
 write(6,*) "1 : str%ia(",i,") = ", str%ia(i)
    end do
  end subroutine sub
end program main
```

```
$ flang snfmm391_22322.f90; ./a.out
 0 : str%ia =  -11 -12 -13 -14 -15
 1 : str%ia( 1 ) =  -11
 1 : str%ia( 2 ) =  -12
 1 : str%ia( 3 ) =  -13
 1 : str%ia( 4 ) =  -14
 1 : str%ia( 5 ) =  -15
$
```

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to