Issue 136522
Summary [Flang] Incorrect execution result of using polymorphic variable in select type construct
Labels flang
Assignees
Reporter ohno-fj
    ```
Version of flang : 21.0.0(ebba554a3211b0b98d3ae33ba70f9d6ceaab6ad4)/AArch64
```

In the attached program, `variable (p)` is the return value of `function (ufun)`. 
It is `a polymorphic variable` that can be of either `derived type (obj3)` or `integer type (obj)`, but it looks like the wrong behavior in `select type construct`.  
`type is (integer) ` in `select type construct` is not executed.  
The above program is sngg457k_22.f90.

The result is correct if the return value of `function (ufun)` is specified directly:
- Before modification
```
     p=ufun(n)
     select type(b=>p)
```
- After modification
```
     select type(b=>ufun(n))
```
The above program is sngg457k_23.f90.

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

sngg457k_22.f90:
```fortran
program main
  type  ty
     integer :: ii
  end type ty
  type ,extends(ty) :: ty1
     integer :: jj
  end type ty1

  type(ty1),target:: obj3
 integer,target:: obj
  class(*),allocatable::p

  do n=0,1
 print*,"n=",n
     p=ufun(n)
     select type(b=>p)
     type  is (ty1)
        write(6,*) "b%ii = ", b%ii, " b%jj = ", b%jj
     type is (integer)
        write(6,*) "obj = ", obj
     end select
  end do
 print*,"pass"
contains
  function ufun(n)
    class(*),pointer :: ufun
    obj3%ii=1
    obj3%jj=10
    obj=100
    select case(n)
 case(0)
       ufun=>obj3
    case(1)
       ufun=>obj
    end select
 end function ufun
end program main
```

```
$ flang sngg457k_22.f90; ./a.out
 n= 0
 b%ii =  1  b%jj =  10
 n= 1
 b%ii =  100  b%jj =  0
 pass
$
```

```
$ gfortran sngg457k_22.f90; ./a.out
 n=           0
 b%ii =            1  b%jj =           10
 n=           1
 obj = 100
 pass
$
```

```
$ ifx sngg457k_22.f90; ./a.out
 n=           0
 b%ii =            1  b%jj =           10
 n=           1
 obj = 100
 pass
$
```

sngg457k_23.f90:
```fortran
program main
  type ty
     integer :: ii
  end type ty
  type ,extends(ty) :: ty1
 integer :: jj
  end type ty1

  type(ty1),target:: obj3
 integer,target:: obj
  class(*),allocatable::p

  do n=0,1
 print*,"n=",n
!     p=ufun(n)
!     select type(b=>p)
     select type(b=>ufun(n))
     type  is (ty1)
        write(6,*) "b%ii = ", b%ii, " b%jj = ", b%jj
     type  is (integer)
        write(6,*) "obj = ", obj
     end select
  end do
  print*,"pass"
contains
  function ufun(n)
    class(*),pointer :: ufun
    obj3%ii=1
    obj3%jj=10
 obj=100
    select case(n)
    case(0)
       ufun=>obj3
    case(1)
 ufun=>obj
    end select
  end function ufun
end program main
```

```
$ flang sngg457k_23.f90; ./a.out
 n= 0
 b%ii =  1  b%jj = 10
 n= 1
 obj =  100
 pass
$
```

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

Reply via email to