public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Peter Hill <peter.hill@york.ac.uk>
To: Harald Anlauf <anlauf@gmx.de>
Cc: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org
Subject: Re: [PATCH] Fortran: fix passing array component to polymorphic argument [PR105658]
Date: Mon, 19 Feb 2024 15:19:01 +0000	[thread overview]
Message-ID: <CAFmps25O9KUejAcp2R5adbynfq-KhGg8VSQmqzFWsSt8dBTvDg@mail.gmail.com> (raw)
In-Reply-To: <cdf24a94-0edd-46c3-8ef6-c3289a992419@gmx.de>

Hi Harald,

Thanks for your help, please see the updated and signed-off patch below.

> (I am not entirely sure whether we need to exclude pointer and
> allocatable attributes here explicitly, given the constraints
> in F2023:15.5.2.6, but other may have an opinion, too.
> The above should be safe anyway.)

I've included them in the patch here, but it does seem to work fine
without checking those attributes here -- and invalid code is still
caught with that change.

It also occurred to me that array temporaries aren't _required_ here
(for arrays of derived type components), but in the general case with
a type with differently sized components, the stride wouldn't be a
multiple of the component's type's size. Is it possible in principle
to have an arbitrary stride?

Cheers,
Peter

From 907a104facfc7f35f48ebcfa9ef5f8f5430d4d3c Mon Sep 17 00:00:00 2001
From: Peter Hill <peter.hill@york.ac.uk>
Date: Thu, 15 Feb 2024 16:58:33 +0000
Subject: [PATCH] Fortran: fix passing array component ref to polymorphic
 procedures

         PR fortran/105658

gcc/fortran/ChangeLog

        * trans-expr.cc (gfc_conv_intrinsic_to_class): When passing an
        array component reference of intrinsic type to a procedure
        with an unlimited polymorphic dummy argument, a temporary
        should be created.

gcc/testsuite/ChangeLog

        * gfortran.dg/PR105658.f90: New test.

Signed-off-by: Peter Hill <peter.hill@york.ac.uk>
---
 gcc/fortran/trans-expr.cc              |  9 +++++
 gcc/testsuite/gfortran.dg/PR105658.f90 | 50 ++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/PR105658.f90

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index a0593b76f18..004081aa6c3 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -1019,6 +1019,14 @@ gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
   tmp = gfc_typenode_for_spec (&class_ts);
   var = gfc_create_var (tmp, "class");

+  /* Force a temporary for component or substring references */
+  if (unlimited_poly
+      && class_ts.u.derived->components->attr.dimension
+      && !class_ts.u.derived->components->attr.allocatable
+      && !class_ts.u.derived->components->attr.class_pointer
+      && is_subref_array (e))
+    parmse->force_tmp = 1;
+
   /* Set the vptr.  */
   ctree = gfc_class_vptr_get (var);

@@ -6439,6 +6447,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
       CLASS object for the unlimited polymorphic formal.  */
    gfc_find_vtab (&e->ts);
    gfc_init_se (&parmse, se);
+
    gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);

  }
diff --git a/gcc/testsuite/gfortran.dg/PR105658.f90
b/gcc/testsuite/gfortran.dg/PR105658.f90
new file mode 100644
index 00000000000..8aacecf806e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/PR105658.f90
@@ -0,0 +1,50 @@
+! { dg-do compile }
+! { dg-options "-Warray-temporaries" }
+! Test fix for incorrectly passing array component to unlimited
polymorphic procedure
+
+module test_PR105658_mod
+   implicit none
+   type :: foo
+     integer :: member1
+     integer :: member2
+   end type foo
+contains
+   subroutine print_poly(array)
+     class(*), dimension(:), intent(in) :: array
+     select type(array)
+     type is (integer)
+       print*, array
+     type is (character(*))
+       print *, array
+     end select
+   end subroutine print_poly
+
+   subroutine do_print(thing)
+     type(foo), dimension(3), intent(in) :: thing
+     type(foo), parameter :: y(3) = [foo(1,2),foo(3,4),foo(5,6)]
+     integer :: i, j, uu(5,6)
+
+     call print_poly(thing%member1)   ! { dg-warning "array temporary" }
+     call print_poly(y%member2)       ! { dg-warning "array temporary" }
+     call print_poly(y(1::2)%member2) ! { dg-warning "array temporary" }
+
+     ! The following array sections work without temporaries
+     uu = reshape([(((10*i+j),i=1,5),j=1,6)],[5,6])
+     print *, uu(2,2::2)
+     call print_poly (uu(2,2::2))     ! no temp needed!
+     print *, uu(1::2,6)
+     call print_poly (uu(1::2,6))     ! no temp needed!
+   end subroutine do_print
+
+   subroutine do_print2(thing2)
+     class(foo), dimension(:), intent(in) :: thing2
+     call print_poly (thing2% member2) ! { dg-warning "array temporary" }
+   end subroutine do_print2
+
+   subroutine do_print3 ()
+     character(3) :: c(3) = ["abc","def","ghi"]
+     call print_poly (c(1::2))      ! no temp needed!
+     call print_poly (c(1::2)(2:3)) ! { dg-warning "array temporary" }
+   end subroutine do_print3
+
+end module test_PR105658_mod
-- 
2.43.0

  parent reply	other threads:[~2024-02-19 15:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-15 17:50 Peter Hill
2024-02-16 21:19 ` Harald Anlauf
2024-02-16 21:19   ` Harald Anlauf
2024-02-19 15:19   ` Peter Hill [this message]
2024-02-19 19:52     ` Harald Anlauf
2024-02-19 19:52       ` Harald Anlauf
2024-02-20 19:53     ` Harald Anlauf
2024-02-20 19:53       ` Harald Anlauf
2024-02-20 20:09       ` Steve Kargl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFmps25O9KUejAcp2R5adbynfq-KhGg8VSQmqzFWsSt8dBTvDg@mail.gmail.com \
    --to=peter.hill@york.ac.uk \
    --cc=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).