| 1 |
! { dg-do run } |
| 2 |
! PR fortran/114012 |
| 3 |
! |
| 4 |
! Polymorphic functions were evaluated twice in assignment |
| 5 |
|
| 6 |
program test |
| 7 |
implicit none |
| 8 |
|
| 9 |
type :: custom_int |
| 10 |
integer :: val = 2 |
| 11 |
end type |
| 12 |
|
| 13 |
interface assignment(=) |
| 14 |
procedure assign |
| 15 |
end interface |
| 16 |
interface operator(-) |
| 17 |
procedure neg |
| 18 |
end interface |
| 19 |
|
| 20 |
type(custom_int) :: i |
| 21 |
integer :: count_assign, count_neg |
| 22 |
|
| 23 |
count_assign = 0 |
| 24 |
count_neg = 0 |
| 25 |
|
| 26 |
i = 1 |
| 27 |
if (count_assign /= 1 .or. count_neg /= 0) stop 1 |
| 28 |
|
| 29 |
i = -i |
| 30 |
if (count_assign /= 2 .or. count_neg /= 1) stop 2 |
| 31 |
if (i% val /= -1) stop 3 |
| 32 |
|
| 33 |
i = neg(i) |
| 34 |
if (count_assign /= 3 .or. count_neg /= 2) stop 4 |
| 35 |
if (i% val /= 1) stop 5 |
| 36 |
|
| 37 |
i = (neg(i)) |
| 38 |
if (count_assign /= 4 .or. count_neg /= 3) stop 6 |
| 39 |
if (i% val /= -1) stop 7 |
| 40 |
|
| 41 |
i = - neg(i) |
| 42 |
if (count_assign /= 5 .or. count_neg /= 5) stop 8 |
| 43 |
if (i% val /= -1) stop 9 |
| 44 |
|
| 45 |
contains |
| 46 |
|
| 47 |
subroutine assign (field, val) |
| 48 |
type(custom_int), intent(out) :: field |
| 49 |
class(*), intent(in) :: val |
| 50 |
|
| 51 |
count_assign = count_assign + 1 |
| 52 |
|
| 53 |
select type (val) |
| 54 |
type is (integer) |
| 55 |
! print *, " in assign(integer)", field%val, val |
| 56 |
field%val = val |
| 57 |
type is (custom_int) |
| 58 |
! print *, " in assign(custom)", field%val, val%val |
| 59 |
field%val = val%val |
| 60 |
class default |
| 61 |
error stop |
| 62 |
end select |
| 63 |
|
| 64 |
end subroutine assign |
| 65 |
|
| 66 |
function neg (input_field) result(output_field) |
| 67 |
type(custom_int), intent(in), target :: input_field |
| 68 |
class(custom_int), allocatable :: output_field |
| 69 |
allocate (custom_int :: output_field) |
| 70 |
|
| 71 |
count_neg = count_neg + 1 |
| 72 |
|
| 73 |
select type (output_field) |
| 74 |
type is (custom_int) |
| 75 |
! print *, " in neg", output_field%val, input_field%val |
| 76 |
output_field%val = -input_field%val |
| 77 |
class default |
| 78 |
error stop |
| 79 |
end select |
| 80 |
end function neg |
| 81 |
end program test |