public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: improve checking of assumed size array spec [PR102180]
@ 2022-12-11 22:33 Harald Anlauf
  2022-12-11 23:52 ` Steve Kargl
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2022-12-11 22:33 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

the attached patch improves checking of array specs in two ways:
- bad assumed size array spec
- a bad first array element spec may already trigger an error,
  leading to a more consistent behavior

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

Thanks,
Harald


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

From 06c1d0a96544640c7b1485fe977337ef1572ac91 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sun, 11 Dec 2022 23:24:03 +0100
Subject: [PATCH] Fortran: improve checking of assumed size array spec
 [PR102180]

gcc/fortran/ChangeLog:

	PR fortran/102180
	* array.cc (match_array_element_spec): Add check for bad
	assumed-implied-spec.
	(gfc_match_array_spec): Reorder logic so that a first bad array
	element spec may trigger an error.

gcc/testsuite/ChangeLog:

	PR fortran/102180
	* gfortran.dg/pr102180.f90: New test.
---
 gcc/fortran/array.cc                   | 12 ++++++++++--
 gcc/testsuite/gfortran.dg/pr102180.f90 | 19 +++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr102180.f90

diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index bbdb5b392fc..9c1d55fa8cc 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -488,6 +488,14 @@ match_array_element_spec (gfc_array_spec *as)
       return AS_ASSUMED_SIZE;
     }

+  /* F2018:R821: "assumed-implied-spec  is  [ lower-bound : ] *".  */
+  if (gfc_match (" : * ") == MATCH_YES)
+    {
+      gfc_error ("A lower bound must precede colon in "
+		 "assumed size array specification at %C");
+      return AS_UNKNOWN;
+    }
+
   if (gfc_match_char (':') == MATCH_YES)
     return AS_DEFERRED;

@@ -591,6 +599,8 @@ gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
     {
       as->rank++;
       current_type = match_array_element_spec (as);
+      if (current_type == AS_UNKNOWN)
+	goto cleanup;

       /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
 	 and implied-shape specifications.  If the rank is at least 2, we can
@@ -600,8 +610,6 @@ gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)

       if (as->rank == 1)
 	{
-	  if (current_type == AS_UNKNOWN)
-	    goto cleanup;
 	  as->type = current_type;
 	}
       else
diff --git a/gcc/testsuite/gfortran.dg/pr102180.f90 b/gcc/testsuite/gfortran.dg/pr102180.f90
new file mode 100644
index 00000000000..cbf3e7299e7
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr102180.f90
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=lib" }
+! PR fortran/102180 - Improve checking of assumed size array spec
+
+subroutine s(x,y)
+  real :: x(0:*) ! legal
+  real :: y[0:*] ! legal
+end
+
+subroutine t(x,y)
+  real :: x(:*) ! { dg-error "A lower bound must precede colon" }
+  real :: y[:*] ! { dg-error "A lower bound must precede colon" }
+end
+
+subroutine u(x,y,z)
+  real :: x(2,*)
+  real :: y(2,2:*)
+  real :: z(2,:*) ! { dg-error "A lower bound must precede colon" }
+end
--
2.35.3


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

* Re: [PATCH] Fortran: improve checking of assumed size array spec [PR102180]
  2022-12-11 22:33 [PATCH] Fortran: improve checking of assumed size array spec [PR102180] Harald Anlauf
@ 2022-12-11 23:52 ` Steve Kargl
  2022-12-12 19:49   ` Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Steve Kargl @ 2022-12-11 23:52 UTC (permalink / raw)
  To: Harald Anlauf via Fortran; +Cc: gcc-patches

On Sun, Dec 11, 2022 at 11:33:43PM +0100, Harald Anlauf via Fortran wrote:
> Dear all,
> 
> the attached patch improves checking of array specs in two ways:
> - bad assumed size array spec
> - a bad first array element spec may already trigger an error,
>   leading to a more consistent behavior
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 

OK with minor nit.

> +  /* F2018:R821: "assumed-implied-spec  is  [ lower-bound : ] *".  */
> +  if (gfc_match (" : * ") == MATCH_YES)
> +    {
> +      gfc_error ("A lower bound must precede colon in "
> +		 "assumed size array specification at %C");

"assumed size" should likely be "assumed-size"

> +      return AS_UNKNOWN;
> +    }
> +


-- 
Steve

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

* Re: [PATCH] Fortran: improve checking of assumed size array spec [PR102180]
  2022-12-11 23:52 ` Steve Kargl
@ 2022-12-12 19:49   ` Harald Anlauf
  2022-12-12 20:07     ` Steve Kargl
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2022-12-12 19:49 UTC (permalink / raw)
  To: sgk, Harald Anlauf via Fortran; +Cc: gcc-patches

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

Hi Steve,

Am 12.12.22 um 00:52 schrieb Steve Kargl via Gcc-patches:
> On Sun, Dec 11, 2022 at 11:33:43PM +0100, Harald Anlauf via Fortran wrote:
>> Dear all,
>>
>> the attached patch improves checking of array specs in two ways:
>> - bad assumed size array spec
>> - a bad first array element spec may already trigger an error,
>>    leading to a more consistent behavior
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
>
> OK with minor nit.
>
>> +  /* F2018:R821: "assumed-implied-spec  is  [ lower-bound : ] *".  */
>> +  if (gfc_match (" : * ") == MATCH_YES)
>> +    {
>> +      gfc_error ("A lower bound must precede colon in "
>> +		 "assumed size array specification at %C");
>
> "assumed size" should likely be "assumed-size"

good point, I've fixed that.

I was a bit unhappy with the previously submitted patch,
as it tried to match ':' twice and gave an unfortunate locus
in the error message.  I now chose to combine the adjacent
matches and to remember a more suitable locus for use with
the emitted error, see attached updated patch.

Committed as r13-4623-gcf5327b89ab610.

Thanks,
Harald

>> +      return AS_UNKNOWN;
>> +    }
>> +
>
>

[-- Attachment #2: pr102180-v2.diff --]
[-- Type: text/x-patch, Size: 2835 bytes --]

From cf5327b89ab610649c5faab78ea7907bb74b103c Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sun, 11 Dec 2022 23:24:03 +0100
Subject: [PATCH] Fortran: improve checking of assumed-size array spec
 [PR102180]

gcc/fortran/ChangeLog:

	PR fortran/102180
	* array.cc (match_array_element_spec): Add check for bad
	assumed-implied-spec.
	(gfc_match_array_spec): Reorder logic so that the first bad array
	element spec may trigger an error.

gcc/testsuite/ChangeLog:

	PR fortran/102180
	* gfortran.dg/pr102180.f90: New test.
---
 gcc/fortran/array.cc                   | 19 ++++++++++++++++---
 gcc/testsuite/gfortran.dg/pr102180.f90 | 19 +++++++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr102180.f90

diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index bbdb5b392fc..10d9e0c5354 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -489,7 +489,20 @@ match_array_element_spec (gfc_array_spec *as)
     }
 
   if (gfc_match_char (':') == MATCH_YES)
-    return AS_DEFERRED;
+    {
+      locus old_loc = gfc_current_locus;
+      if (gfc_match_char ('*') == MATCH_YES)
+	{
+	  /* F2018:R821: "assumed-implied-spec  is  [ lower-bound : ] *".  */
+	  gfc_error ("A lower bound must precede colon in "
+		     "assumed-size array specification at %L", &old_loc);
+	  return AS_UNKNOWN;
+	}
+      else
+	{
+	  return AS_DEFERRED;
+	}
+    }
 
   m = gfc_match_expr (upper);
   if (m == MATCH_NO)
@@ -591,6 +604,8 @@ gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
     {
       as->rank++;
       current_type = match_array_element_spec (as);
+      if (current_type == AS_UNKNOWN)
+	goto cleanup;
 
       /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
 	 and implied-shape specifications.  If the rank is at least 2, we can
@@ -600,8 +615,6 @@ gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
 
       if (as->rank == 1)
 	{
-	  if (current_type == AS_UNKNOWN)
-	    goto cleanup;
 	  as->type = current_type;
 	}
       else
diff --git a/gcc/testsuite/gfortran.dg/pr102180.f90 b/gcc/testsuite/gfortran.dg/pr102180.f90
new file mode 100644
index 00000000000..cbf3e7299e7
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr102180.f90
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=lib" }
+! PR fortran/102180 - Improve checking of assumed size array spec
+
+subroutine s(x,y)
+  real :: x(0:*) ! legal
+  real :: y[0:*] ! legal
+end
+
+subroutine t(x,y)
+  real :: x(:*) ! { dg-error "A lower bound must precede colon" }
+  real :: y[:*] ! { dg-error "A lower bound must precede colon" }
+end
+
+subroutine u(x,y,z)
+  real :: x(2,*)
+  real :: y(2,2:*)
+  real :: z(2,:*) ! { dg-error "A lower bound must precede colon" }
+end
-- 
2.35.3


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

* Re: [PATCH] Fortran: improve checking of assumed size array spec [PR102180]
  2022-12-12 19:49   ` Harald Anlauf
@ 2022-12-12 20:07     ` Steve Kargl
  0 siblings, 0 replies; 4+ messages in thread
From: Steve Kargl @ 2022-12-12 20:07 UTC (permalink / raw)
  To: Harald Anlauf via Fortran; +Cc: gcc-patches

On Mon, Dec 12, 2022 at 08:49:50PM +0100, Harald Anlauf via Fortran wrote:
> 
> Committed as r13-4623-gcf5327b89ab610.
> 

To be clear, I have no problems with this commit.

But, just a FYI, there is gfc_peek_ascii_char(),
which allows you to look at the next character
without having to keep track of the locus.

> +    {
> +      locus old_loc = gfc_current_locus;
> +      if (gfc_match_char ('*') == MATCH_YES)

         gfc_gobble_whitespace (); /* Can't remember if matching up to this
                                      eats whitespace.  */ 
         if (gfc_peek_ascii_char () == '*')

> +	{
> +	  /* F2018:R821: "assumed-implied-spec  is  [ lower-bound : ] *".  */
> +	  gfc_error ("A lower bound must precede colon in "
> +		     "assumed-size array specification at %L", &old_loc);
> +	  return AS_UNKNOWN;
> +	}
> +      else
> +	{
          gfc_current_locus = old_loc; /* Is this needed? */
> +	  return AS_DEFERRED;
> +	}
> +    }


-- 
Steve

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

end of thread, other threads:[~2022-12-12 20:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-11 22:33 [PATCH] Fortran: improve checking of assumed size array spec [PR102180] Harald Anlauf
2022-12-11 23:52 ` Steve Kargl
2022-12-12 19:49   ` Harald Anlauf
2022-12-12 20:07     ` Steve Kargl

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).