public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: rank checking with explicit-/assumed-size arrays and CLASS [PR58331]
@ 2023-03-14 19:38 Harald Anlauf
  2023-03-15  9:10 ` Tobias Burnus
  0 siblings, 1 reply; 3+ messages in thread
From: Harald Anlauf @ 2023-03-14 19:38 UTC (permalink / raw)
  To: fortran, gcc-patches; +Cc: tobias

[-- Attachment #1: Type: text/plain, Size: 479 bytes --]

Dear all,

the attached patch, which is based on a draft by Tobias, fixes
an old rejects-valid issue with rank checking for CLASS arrays
by using the proper array spec of CLASS variables.

The testcase covers only non-coarray cases, as playing with
coarray variants hit pre-exisiting issues in gfortran that
are very likely unrelated to the interface checks.  I consider
this rather as post 13-release stuff.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr58331.diff --]
[-- Type: text/x-patch, Size: 5447 bytes --]

From 4453686ae4e70c14a0898c6687db912fa84ece9f Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 14 Mar 2023 20:23:06 +0100
Subject: [PATCH] Fortran: rank checking with explicit-/assumed-size arrays and
 CLASS [PR58331]

gcc/fortran/ChangeLog:

	PR fortran/58331
	* interface.cc (compare_parameter): Adjust check of array dummy
	arguments to handle the case of CLASS variables.

gcc/testsuite/ChangeLog:

	PR fortran/58331
	* gfortran.dg/class_dummy_10.f90: New test.

Co-authored-by: Tobias Burnus <tobias@codesourcery.com>
---
 gcc/fortran/interface.cc                     | 27 +++++++---
 gcc/testsuite/gfortran.dg/class_dummy_10.f90 | 56 ++++++++++++++++++++
 2 files changed, 76 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/class_dummy_10.f90

diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index dafe41753b7..1d0f8bb5915 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -2349,6 +2349,7 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
   char err[200];
   gfc_component *ppc;
   bool codimension = false;
+  gfc_array_spec *formal_as;

   /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
      procs c_f_pointer or c_f_procpointer, and we need to accept most
@@ -2540,6 +2541,9 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
       return false;
     }

+  formal_as = formal->ts.type == BT_CLASS ? CLASS_DATA (formal)->as
+					  : formal->as;
+
   if (codimension && formal->attr.allocatable)
     {
       gfc_ref *last = NULL;
@@ -2650,10 +2654,10 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
   if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
     return true;

-  rank_check = where != NULL && !is_elemental && formal->as
-	       && (formal->as->type == AS_ASSUMED_SHAPE
-		   || formal->as->type == AS_DEFERRED)
-	       && actual->expr_type != EXPR_NULL;
+  rank_check = where != NULL && !is_elemental && formal_as
+    && (formal_as->type == AS_ASSUMED_SHAPE
+	|| formal_as->type == AS_DEFERRED)
+    && actual->expr_type != EXPR_NULL;

   /* Skip rank checks for NO_ARG_CHECK.  */
   if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
@@ -2662,14 +2666,20 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
   /* Scalar & coindexed, see: F2008, Section 12.5.2.4.  */
   if (rank_check || ranks_must_agree
       || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
-      || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
+      || (actual->rank != 0
+	  && !(is_elemental || formal->attr.dimension
+	       || (formal->ts.type == BT_CLASS
+		   && CLASS_DATA (formal)->attr.dimension)))
       || (actual->rank == 0
 	  && ((formal->ts.type == BT_CLASS
 	       && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
 	      || (formal->ts.type != BT_CLASS
 		   && formal->as->type == AS_ASSUMED_SHAPE))
 	  && actual->expr_type != EXPR_NULL)
-      || (actual->rank == 0 && formal->attr.dimension
+      || (actual->rank == 0
+	  && (formal->attr.dimension
+	      || (formal->ts.type == BT_CLASS
+		  && CLASS_DATA (formal)->attr.dimension))
 	  && gfc_is_coindexed (actual))
       /* Assumed-rank actual argument; F2018 C838.  */
       || actual->rank == -1)
@@ -2690,7 +2700,10 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
 	}
       return false;
     }
-  else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
+  else if (actual->rank != 0
+	   && (is_elemental || formal->attr.dimension
+	       || (formal->ts.type == BT_CLASS
+		   && CLASS_DATA (formal)->attr.dimension)))
     return true;

   /* At this point, we are considering a scalar passed to an array.   This
diff --git a/gcc/testsuite/gfortran.dg/class_dummy_10.f90 b/gcc/testsuite/gfortran.dg/class_dummy_10.f90
new file mode 100644
index 00000000000..cee5d4d82b2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/class_dummy_10.f90
@@ -0,0 +1,56 @@
+! { dg-do compile }
+! PR fortran/58331 - rank checking for CLASS dummy arguments
+
+module mymod
+  implicit none
+contains
+  subroutine mysub(a, n)
+    integer,  intent(in) :: n
+    class(*), intent(in) :: a(n)
+
+    select type(a)
+    type is(integer)
+       print *,'a is integer'
+       print *, "n=", n, '  a=', a
+    class default
+       print *,'a is unsupported type'
+       stop 1
+    end select
+  end
+
+  ! Assumed rank
+  subroutine sub_ar (a)
+    class(*), intent(in) :: a(..)
+    print *, rank (a), size (a), ":", shape (a)
+  end
+
+  ! Assumed shape
+  subroutine sub_as1 (a)
+    class(*), intent(in) :: a(:)
+    print *, rank (a), size (a), ":", shape (a)
+  end
+  subroutine sub_as2 (b)
+    class(*), intent(in) :: b(:,:)
+    print *, rank (b), size (b), ":", shape (b)
+  end
+end
+
+program p
+  use mymod
+  implicit none
+  integer :: a(3)   = [11, 12, 13]
+  integer :: b(2,2) = reshape([21, 22, 23, 24], [2,2])
+  integer :: c      = 1
+
+  call mysub(a,3)
+  call mysub(b,4)
+  call sub_ar(a)
+  call sub_ar(b)
+  call sub_ar(c)
+  call sub_as1(a)
+  call sub_as2(b)
+  !
+  call mysub(c,1) ! { dg-error "rank-1 and scalar" }
+  call sub_as1(b) ! { dg-error "rank-1 and rank-2" }
+  call sub_as2(a) ! { dg-error "rank-2 and rank-1" }
+end
--
2.35.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fortran: rank checking with explicit-/assumed-size arrays and CLASS [PR58331]
  2023-03-14 19:38 [PATCH] Fortran: rank checking with explicit-/assumed-size arrays and CLASS [PR58331] Harald Anlauf
@ 2023-03-15  9:10 ` Tobias Burnus
  2023-03-15 19:36   ` Harald Anlauf
  0 siblings, 1 reply; 3+ messages in thread
From: Tobias Burnus @ 2023-03-15  9:10 UTC (permalink / raw)
  To: Harald Anlauf, fortran; +Cc: gcc-patches

Hi Harald,

On 14.03.23 20:38, Harald Anlauf wrote:
> The testcase covers only non-coarray cases, as playing with
> coarray variants hit pre-exisiting issues in gfortran that
> are very likely unrelated to the interface checks.
I concur (but would not rule out additional interface issues).
> I consider this rather as post 13-release stuff.
In any case, the coarray issue can be fixed separately. And I think
post-GCC-13 makes sense.
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
Thanks – LGTM!
> +  formal_as = formal->ts.type == BT_CLASS ? CLASS_DATA (formal)->as
> +                                       : formal->as;
> +

(Jakub remarks for such code that some editor (emacs?), he does not use,
mis-<tab>-auto-indent such a code - and proposes to add a parentheses
around the right-hand side of the assignment.)

* * *

I also wonder whether we need some run-time testcase. The interface
check now works and I also tend to write dg-do-compile testcases, but
given what can go wrong with all the array descriptor, class etc
handling, we may want to ensure it works at run time. – Thoughts?

(That's independent of the patch it and could be done as follow up, if
it deemed reasonable. The included testcase is surely compile-only as it
has dg-error checks.)

Tobias

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fortran: rank checking with explicit-/assumed-size arrays and CLASS [PR58331]
  2023-03-15  9:10 ` Tobias Burnus
@ 2023-03-15 19:36   ` Harald Anlauf
  0 siblings, 0 replies; 3+ messages in thread
From: Harald Anlauf @ 2023-03-15 19:36 UTC (permalink / raw)
  To: Tobias Burnus, fortran; +Cc: gcc-patches

Hi Tobias,

Am 15.03.23 um 10:10 schrieb Tobias Burnus:
> Hi Harald,
>
> On 14.03.23 20:38, Harald Anlauf wrote:
>> The testcase covers only non-coarray cases, as playing with
>> coarray variants hit pre-exisiting issues in gfortran that
>> are very likely unrelated to the interface checks.
> I concur (but would not rule out additional interface issues).

More testing seems to mostly uncover issues later on in trans*.cc,
e.g. when passing type to class.  I'll open a PR on this as a followup.

>> I consider this rather as post 13-release stuff.
> In any case, the coarray issue can be fixed separately. And I think
> post-GCC-13 makes sense.

Good.

>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> Thanks – LGTM!
>> +  formal_as = formal->ts.type == BT_CLASS ? CLASS_DATA (formal)->as
>> +                                       : formal->as;
>> +
>
> (Jakub remarks for such code that some editor (emacs?), he does not use,
> mis-<tab>-auto-indent such a code - and proposes to add a parentheses
> around the right-hand side of the assignment.)

Ah, adding parentheses helps!  I've reformatted this block accordingly.
Pushed as:

https://gcc.gnu.org/g:901edd99b44976b3c2b13a7d525d9e315540186a

> * * *
>
> I also wonder whether we need some run-time testcase. The interface
> check now works and I also tend to write dg-do-compile testcases, but
> given what can go wrong with all the array descriptor, class etc
> handling, we may want to ensure it works at run time. – Thoughts?

If you comment out the lines with dg-error, the code compiles
and seems to run fine here.  I've even found cases where passing
array sections works correctly here and with current Intel it
does not ;-)

I'd prefer to postpone more elaborate run-time tests until we have
more non-ICEing related code.

Thanks,
Harald

> (That's independent of the patch it and could be done as follow up, if
> it deemed reasonable. The included testcase is surely compile-only as it
> has dg-error checks.)
>
> Tobias
>
> -----------------
> Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201,
> 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer:
> Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München;
> Registergericht München, HRB 106955
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-15 19:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 19:38 [PATCH] Fortran: rank checking with explicit-/assumed-size arrays and CLASS [PR58331] Harald Anlauf
2023-03-15  9:10 ` Tobias Burnus
2023-03-15 19:36   ` 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).