<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/136522>136522</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Incorrect execution result of using polymorphic variable in select type construct
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ohno-fj
      </td>
    </tr>
</table>

<pre>
    ```
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
$
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcV02P2zgP_jXKhZhApux8HHJwJ29e9F7sdSHZsq3UkQxZTpt_v5BkzyTOdDOzi-5XUKAjiXxIPqRIi_e9qrWUO5J9Itl-wQfXGLszjTZP1XEhTHnZkRUd_9H8F2l7ZTSYCqqW6xoIywGTJV1SghspBM-ylDNMEkHFdlMyLhkTfE2rbbkqJOdixcuU4JbgIc9t0axSQvNrC4TmnzW4RgJ3jheNLKGzprb8RPAZyIqeuVVctBIIbjqPtKKg-qBhpRushjNvB-ldJCtaDbpw3mOCm6EadFRYgjfjvB5ZUQ6daS8nY7tGFTDhe1zXcAcF1yACnlSukdarlNKqsyzBXbrgiBFHNvpigoDSTtbSXgvEcx-FGBwoB60xX3to1VcZvP9mja5ByIaflbGgtMfpZSsLF2EKo3tnh8KFCCASF058HLgZbRLcQiDldwG8jjYO5HdZDE6WEfCL512Ys5xI92K9rus0W3_9FXFZbekyZulL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