public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-9186] Fortran: do not evaluate polymorphic functions twice in assignment [PR114012]
@ 2024-02-26 17:50 Harald Anlauf
  0 siblings, 0 replies; only message in thread
From: Harald Anlauf @ 2024-02-26 17:50 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2f71e801ad0bb1f620334aadbd7c99cc4efe6309

commit r14-9186-g2f71e801ad0bb1f620334aadbd7c99cc4efe6309
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sun Feb 25 21:18:23 2024 +0100

    Fortran: do not evaluate polymorphic functions twice in assignment [PR114012]
    
            PR fortran/114012
    
    gcc/fortran/ChangeLog:
    
            * trans-expr.cc (gfc_conv_procedure_call): Evaluate non-trivial
            arguments just once before assigning to an unlimited polymorphic
            dummy variable.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr114012.f90: New test.

Diff:
---
 gcc/fortran/trans-expr.cc              |  4 ++
 gcc/testsuite/gfortran.dg/pr114012.f90 | 81 ++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 118dfd7c9b23..d63c304661a4 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -6691,6 +6691,10 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 			    {
 			      tree efield;
 
+			      /* Evaluate arguments just once.  */
+			      if (e->expr_type != EXPR_VARIABLE)
+				parmse.expr = save_expr (parmse.expr);
+
 			      /* Set the _data field.  */
 			      tmp = gfc_class_data_get (var);
 			      efield = fold_convert (TREE_TYPE (tmp),
diff --git a/gcc/testsuite/gfortran.dg/pr114012.f90 b/gcc/testsuite/gfortran.dg/pr114012.f90
new file mode 100644
index 000000000000..9dbb031c6643
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr114012.f90
@@ -0,0 +1,81 @@
+! { dg-do run }
+! PR fortran/114012
+!
+! Polymorphic functions were evaluated twice in assignment
+
+program test
+  implicit none
+
+  type :: custom_int
+     integer :: val = 2
+  end type
+
+  interface assignment(=)
+     procedure assign
+  end interface
+  interface operator(-)
+     procedure neg
+  end interface
+
+  type(custom_int) :: i
+  integer          :: count_assign, count_neg
+
+  count_assign = 0
+  count_neg    = 0
+
+  i = 1
+  if (count_assign /= 1 .or. count_neg /= 0) stop 1
+
+  i = -i
+  if (count_assign /= 2 .or. count_neg /= 1) stop 2
+  if (i% val /= -1) stop 3
+
+  i = neg(i)
+  if (count_assign /= 3 .or. count_neg /= 2) stop 4
+  if (i% val /=  1) stop 5
+
+  i = (neg(i))
+  if (count_assign /= 4 .or. count_neg /= 3) stop 6
+  if (i% val /= -1) stop 7
+
+  i = - neg(i)
+  if (count_assign /= 5 .or. count_neg /= 5) stop 8
+  if (i% val /= -1) stop 9
+
+contains
+
+  subroutine assign (field, val)
+    type(custom_int), intent(out) :: field
+    class(*), intent(in) :: val
+
+    count_assign = count_assign + 1
+
+    select type (val)
+    type is (integer)
+!      print *, " in assign(integer)", field%val, val
+       field%val = val
+    type is (custom_int)
+!      print *, " in assign(custom)", field%val, val%val
+       field%val = val%val
+    class default
+       error stop
+    end select
+
+  end subroutine assign
+
+  function neg (input_field) result(output_field)
+    type(custom_int), intent(in), target :: input_field
+    class(custom_int), allocatable :: output_field
+    allocate (custom_int :: output_field)
+
+    count_neg = count_neg + 1
+
+    select type (output_field)
+    type is (custom_int)
+!      print *, " in neg", output_field%val, input_field%val
+       output_field%val = -input_field%val
+    class default
+       error stop
+    end select
+  end function neg
+end program test

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-26 17:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-26 17:50 [gcc r14-9186] Fortran: do not evaluate polymorphic functions twice in assignment [PR114012] Harald Anlauf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).