diff mbox series

[v2] Fortran: restrictions on integer arguments to SYSTEM_CLOCK [PR112609]

Message ID 2898e351-eee8-45dd-a05d-0280378ba872@gmx.de
State New
Headers show
Series [v2] Fortran: restrictions on integer arguments to SYSTEM_CLOCK [PR112609] | expand

Commit Message

Harald Anlauf Nov. 19, 2023, 8:46 p.m. UTC
Hi Steve,

On 11/19/23 01:04, Steve Kargl wrote:
> On Sat, Nov 18, 2023 at 11:12:55PM +0100, Harald Anlauf wrote:
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
>
> Not in its current form.
>
>>   {
>> +  int first_int_kind = -1;
>> +  bool f2023 = ((gfc_option.allow_std & GFC_STD_F2023) != 0
>> +		&& (gfc_option.allow_std & GFC_STD_GNU) == 0);
>> +
>
> If you use the gfc_notify_std(), then you should not need the
> above check on GFC_STD_GNU as it should include GFC_STD_F2023.

this is actually the question (and problem).  For all new features,
-std=gnu shall include everything allowed by -std=f2023.

Here we have the problem that the testcase is valid F2018 and is
silently accepted by gfortran-13 for -std=gnu and -std=f2018.

I prefer to keep it that way also for gfortran-14, and apply the
new restrictions only for -std=f2023.  Do we agree on this?

Now that should happen for -std=gnu -pedantic (-w)?

I have thought some more and came up with the revised attached
patch, which still has the above condition.  It now marks the
diagnostics as GNU extensions beyond F2023 for -std=f2023.

The mask f2023 in the above form suppresses new warnings even
for -pedantic; one would normally use -w to suppress them.

Now if you remove the second part of the condition, we will
regress on testcases system_clock_1.f90 and system_clock_3.f90
because they would emit GNU extension warnings because the
testsuite runs with -pedantic.

The options I see:

- use patch-V1 (although diagnostics are better in V2),

- use patch-V2,

- use patch-V2, but enable -pedantic warnings for previously
   valid code, and adjust the failing testcases

- ???

> Elsewhere in the FE, gfortran uses gfc_notify_std() to enforce
> requirements of a Fortran standard.  The above would be
>
>        if (count->ts.kind < gfc_default_integer_kind
>            && gfc_notify_std (GFC_STD_F2023, "COUNT argument to SYSTEM_CLOCK "
>                               "at %L must have kind of at least default integer",
>                               &count->where))

I tried this first, and it did not do the job.

The logic in gfc_notify_std is:

   estd = std & ~gfc_option.allow_std;  /* Standard to error about.  */
   error = (estd != 0);
   if (error)
     msg = notify_std_msg (estd);
...

So for -std=f2023 we get estd=0, error=false, and *NO* error.
For -std=f2018 we get error=true and an error message.
This is the opposite of what is needed.

Can you please try yourself?

> Note, gfc_notify_std() should add the 'Fortran 2023: ' string,
> if not, that should be fixed.

This I did fix.

> Of course, I seldom provide patches if others don't have a comment
> then do as you like.

Thanks for your feedback!

Harald

Comments

Steve Kargl Nov. 20, 2023, 7:02 p.m. UTC | #1
Harald, 

Sorry about delayed response.  Got side-tracked by Family this weekend.

On Sun, Nov 19, 2023 at 09:46:46PM +0100, Harald Anlauf wrote:
> 
> On 11/19/23 01:04, Steve Kargl wrote:
> > On Sat, Nov 18, 2023 at 11:12:55PM +0100, Harald Anlauf wrote:
> > > Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> > > 
> > 
> > Not in its current form.
> > 
> > >   {
> > > +  int first_int_kind = -1;
> > > +  bool f2023 = ((gfc_option.allow_std & GFC_STD_F2023) != 0
> > > +		&& (gfc_option.allow_std & GFC_STD_GNU) == 0);
> > > +
> > 
> > If you use the gfc_notify_std(), then you should not need the
> > above check on GFC_STD_GNU as it should include GFC_STD_F2023.
> 
> this is actually the question (and problem).  For all new features,
> -std=gnu shall include everything allowed by -std=f2023.

Yes.

> Here we have the problem that the testcase is valid F2018 and is
> silently accepted by gfortran-13 for -std=gnu and -std=f2018.

F2023 is the Fortran standard and supercedes previous Fortran standards.
If there is a conflict between the standing standard and an old standard,
then the standing standard should take precedence unless one specifically
uses, for example, -std=f2018.

After 20+ years of contributing to gfortran, I've come to believe
that the default -std= should be the current standard, and -std=gnu
should be deprecated.  All GNU extensions should require an option
to active.  For example,

   write(*,*), 'hello'
   end

   gfortran12 -o z a.f90
   a.f90:1:10:

   1 | write(*,*), 'hello'
     |           1
   Warning: Legacy Extension: Comma before i/o item list at (1)

This should be an error unless the -fallow-write-stmt-comma is used.
The option would simply degrade the error to a warning.  Why, you ask?
To encourage people to write standard conforming code.  Unfortunately,
that horse has left the barn.

> I prefer to keep it that way also for gfortran-14, and apply the
> new restrictions only for -std=f2023.  Do we agree on this?

If gfortran wants to maintain the status quo for 14, then
it should probably remove the -std=f2023 patch and wait for
the branch to 15.  

> Now that should happen for -std=gnu -pedantic (-w)?

-pedantic is not a very effective option and should be ignored.
 
> I have thought some more and came up with the revised attached
> patch, which still has the above condition.  It now marks the
> diagnostics as GNU extensions beyond F2023 for -std=f2023.
> 
> The mask f2023 in the above form suppresses new warnings even
> for -pedantic; one would normally use -w to suppress them.
> 
> Now if you remove the second part of the condition, we will
> regress on testcases system_clock_1.f90 and system_clock_3.f90
> because they would emit GNU extension warnings because the
> testsuite runs with -pedantic.

It seems that the solution is to fix the code in the testsuite.
With -std=f2023 or -std=gnu, the code should error.  With
-std=f2018 (or older?), the code should compile.

It's been too long for my memory, doesn't the use of
'{ dg-options "-std=f023" }' supercede the set of predefined options
such as -pedantic?

> The options I see:
> 
> - use patch-V1 (although diagnostics are better in V2),
> 
> - use patch-V2,
> 
> - use patch-V2, but enable -pedantic warnings for previously
>   valid code, and adjust the failing testcases

I suppose that this last one is the best option.
> 
> - ???
> 
> > Elsewhere in the FE, gfortran uses gfc_notify_std() to enforce
> > requirements of a Fortran standard.  The above would be
> > 
> >        if (count->ts.kind < gfc_default_integer_kind
> >            && gfc_notify_std (GFC_STD_F2023, "COUNT argument to SYSTEM_CLOCK "
> >                               "at %L must have kind of at least default integer",
> >                               &count->where))
> 
> I tried this first, and it did not do the job.
> 
> The logic in gfc_notify_std is:
> 
>   estd = std & ~gfc_option.allow_std;  /* Standard to error about.  */
>   error = (estd != 0);
>   if (error)
>     msg = notify_std_msg (estd);
> ...
> 
> So for -std=f2023 we get estd=0, error=false, and *NO* error.
> For -std=f2018 we get error=true and an error message.
> This is the opposite of what is needed.
> 
> Can you please try yourself?
> 

I was afraid you were going to say this.  :-) :-) 

The holidays draw near.  I can probably find the time to
poke at gfortran.

> > Note, gfc_notify_std() should add the 'Fortran 2023: ' string,
> > if not, that should be fixed.
> 
> This I did fix.

Thanks.

> > Of course, I seldom provide patches if others don't have a comment
> > then do as you like.
> 
> Thanks for your feedback!

No, thank you for continuing to peck away at gfortran issue.
Mikael Morin Nov. 21, 2023, 11:33 a.m. UTC | #2
Hello,

Le 20/11/2023 à 20:02, Steve Kargl a écrit :
> Harald,
> 
> Sorry about delayed response.  Got side-tracked by Family this weekend.
> 
> On Sun, Nov 19, 2023 at 09:46:46PM +0100, Harald Anlauf wrote:
>>
>> On 11/19/23 01:04, Steve Kargl wrote:
>>> On Sat, Nov 18, 2023 at 11:12:55PM +0100, Harald Anlauf wrote:
>>>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>>>
>>>
>>> Not in its current form.
>>>
>>>>    {
>>>> +  int first_int_kind = -1;
>>>> +  bool f2023 = ((gfc_option.allow_std & GFC_STD_F2023) != 0
>>>> +		&& (gfc_option.allow_std & GFC_STD_GNU) == 0);
>>>> +
>>>
>>> If you use the gfc_notify_std(), then you should not need the
>>> above check on GFC_STD_GNU as it should include GFC_STD_F2023.
>>
>> this is actually the question (and problem).  For all new features,
>> -std=gnu shall include everything allowed by -std=f2023.
> 
> Yes.
> 
Harald, you mentioned the lack of GFC_STD_F2023_DEL feature group in 
your first message, but I don't quite understand why you didn't add one. 
  It seems to me the most natural way to do this.

>> Here we have the problem that the testcase is valid F2018 and is
>> silently accepted by gfortran-13 for -std=gnu and -std=f2018.
> 
> F2023 is the Fortran standard and supercedes previous Fortran standards.
> If there is a conflict between the standing standard and an old standard,
> then the standing standard should take precedence unless one specifically
> uses, for example, -std=f2018.
> 
> After 20+ years of contributing to gfortran, I've come to believe
> that the default -std= should be the current standard, and -std=gnu
> should be deprecated.  All GNU extensions should require an option
> to active.  For example,
> 
>     write(*,*), 'hello'
>     end
> 
>     gfortran12 -o z a.f90
>     a.f90:1:10:
> 
>     1 | write(*,*), 'hello'
>       |           1
>     Warning: Legacy Extension: Comma before i/o item list at (1)
> 
> This should be an error unless the -fallow-write-stmt-comma is used.
> The option would simply degrade the error to a warning.  Why, you ask?
> To encourage people to write standard conforming code.  Unfortunately,
> that horse has left the barn.
> 
>> I prefer to keep it that way also for gfortran-14, and apply the
>> new restrictions only for -std=f2023.  Do we agree on this?
> 
I suggest we emit a warning by default, error with -std=f2023 (I agree 
with Steve that we should push towards strict f2023 conformance), and no 
diagnostic with -std=gnu or -std=f2018 or lower.

> If gfortran wants to maintain the status quo for 14, then
> it should probably remove the -std=f2023 patch and wait for
> the branch to 15.
> 
>> Now that should happen for -std=gnu -pedantic (-w)?
> 
> -pedantic is not a very effective option and should be ignored.
>  >> I have thought some more and came up with the revised attached
>> patch, which still has the above condition.  It now marks the
>> diagnostics as GNU extensions beyond F2023 for -std=f2023.
>>
>> The mask f2023 in the above form suppresses new warnings even
>> for -pedantic; one would normally use -w to suppress them.
>>
>> Now if you remove the second part of the condition, we will
>> regress on testcases system_clock_1.f90 and system_clock_3.f90
>> because they would emit GNU extension warnings because the
>> testsuite runs with -pedantic.
> 
> It seems that the solution is to fix the code in the testsuite.

Agreed, these seem to explicitly test mismatching kinds, so add an 
option to prevent error.

Mikael
diff mbox series

Patch

From 2a85dc469696c85524459380ce11faa20e558680 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sun, 19 Nov 2023 21:14:37 +0100
Subject: [PATCH] Fortran: restrictions on integer arguments to SYSTEM_CLOCK
 [PR112609]

Fortran 2023 added restrictions on integer arguments to SYSTEM_CLOCK to
have a decimal exponent range at least as large as a default integer,
and that all integer arguments have the same kind type parameter.

gcc/fortran/ChangeLog:

	PR fortran/112609
	* check.cc (gfc_check_system_clock): Add checks on integer arguments
	to SYSTEM_CLOCK specific to F2023.
	* error.cc (notify_std_msg): Adjust to handle new features added
	in F2023.

gcc/testsuite/ChangeLog:

	PR fortran/112609
	* gfortran.dg/system_clock_4.f90: New test.
---
 gcc/fortran/check.cc                         | 52 ++++++++++++++++++++
 gcc/fortran/error.cc                         |  4 +-
 gcc/testsuite/gfortran.dg/system_clock_4.f90 | 24 +++++++++
 3 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/system_clock_4.f90

diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc
index 6c45e6542f0..faaea853bc4 100644
--- a/gcc/fortran/check.cc
+++ b/gcc/fortran/check.cc
@@ -6774,6 +6774,10 @@  bool
 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 			gfc_expr *count_max)
 {
+  int first_int_kind = -1;
+  bool f2023 = ((gfc_option.allow_std & GFC_STD_F2023) != 0
+		&& (gfc_option.allow_std & GFC_STD_GNU) == 0);
+
   if (count != NULL)
     {
       if (!scalar_check (count, 0))
@@ -6788,8 +6792,17 @@  gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 			      &count->where))
 	return false;
 
+      if (f2023 && count->ts.kind < gfc_default_integer_kind
+	  && !gfc_notify_std (GFC_STD_GNU, "Fortran 2023 requires "
+			      "COUNT argument to SYSTEM_CLOCK at %L "
+			      "to have a kind of at least default integer",
+			      &count->where))
+	return false;
+
       if (!variable_check (count, 0, false))
 	return false;
+
+      first_int_kind = count->ts.kind;
     }
 
   if (count_rate != NULL)
@@ -6816,6 +6829,16 @@  gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 				  "SYSTEM_CLOCK at %L has non-default kind",
 				  &count_rate->where))
 	    return false;
+
+	  if (f2023 && count_rate->ts.kind < gfc_default_integer_kind
+	      && !gfc_notify_std (GFC_STD_GNU, "Fortran 2023 requires "
+				  "COUNT_RATE argument to SYSTEM_CLOCK at %L "
+				  "to have a kind of at least default integer",
+				  &count_rate->where))
+	    return false;
+
+	  if (first_int_kind < 0)
+	    first_int_kind = count_rate->ts.kind;
 	}
 
     }
@@ -6836,6 +6859,35 @@  gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
 
       if (!variable_check (count_max, 2, false))
 	return false;
+
+      if (f2023 && count_max->ts.kind < gfc_default_integer_kind
+	  && !gfc_notify_std (GFC_STD_GNU, "Fortran 2023 requires "
+			      "COUNT_MAX argument to SYSTEM_CLOCK at %L "
+			      "to have a kind of at least default integer",
+			      &count_max->where))
+	  return false;
+
+      if (first_int_kind < 0)
+	first_int_kind = count_max->ts.kind;
+    }
+
+  if (f2023 && first_int_kind > 0)
+    {
+      if (count_rate
+	  && count_rate->ts.type == BT_INTEGER
+	  && count_rate->ts.kind != first_int_kind
+	  && !gfc_notify_std (GFC_STD_GNU, "Fortran 2023 requires "
+			      "all integer arguments to SYSTEM_CLOCK "
+			      "at %L to have the same kind",
+			      &count_rate->where))
+	return false;
+
+      if (count_max && count_max->ts.kind != first_int_kind
+	  && !gfc_notify_std (GFC_STD_GNU, "Fortran 2023 requires "
+			      "all integer arguments to SYSTEM_CLOCK "
+			      "at %L to have the same kind",
+			      &count_max->where))
+	return false;
     }
 
   return true;
diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc
index 2ac51e95e4d..b8b36c0cd7c 100644
--- a/gcc/fortran/error.cc
+++ b/gcc/fortran/error.cc
@@ -980,7 +980,9 @@  char const*
 notify_std_msg(int std)
 {
 
-  if (std & GFC_STD_F2018_DEL)
+  if (std & GFC_STD_F2023)
+    return _("Fortran 2023:");
+  else if (std & GFC_STD_F2018_DEL)
     return _("Fortran 2018 deleted feature:");
   else if (std & GFC_STD_F2018_OBS)
     return _("Fortran 2018 obsolescent feature:");
diff --git a/gcc/testsuite/gfortran.dg/system_clock_4.f90 b/gcc/testsuite/gfortran.dg/system_clock_4.f90
new file mode 100644
index 00000000000..d4496efbe2a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/system_clock_4.f90
@@ -0,0 +1,24 @@ 
+! { dg-do compile }
+! { dg-options "-std=f2023" }
+! PR fortran/112609 - F2023 restrictions on integer arguments to SYSTEM_CLOCK
+
+program p
+  implicit none
+  integer    :: i,  j,  k
+  integer(2) :: i2, j2, k2
+  integer(8) :: i8, j8, k8
+  real       :: x
+
+  call system_clock(count=i2)      ! { dg-error "at least default integer" }
+  call system_clock(count_rate=j2) ! { dg-error "at least default integer" }
+  call system_clock(count_max=k2)  ! { dg-error "at least default integer" }
+
+  call system_clock(count=i8,count_rate=x,count_max=k8)
+  call system_clock(count=i, count_rate=j8)     ! { dg-error "have the same kind" }
+  call system_clock(count=i8,count_rate=j)      ! { dg-error "have the same kind" }
+  call system_clock(count=i, count_max=k8)      ! { dg-error "have the same kind" }
+  call system_clock(count=i8,count_max=k)       ! { dg-error "have the same kind" }
+  call system_clock(count_rate=j, count_max=k8) ! { dg-error "have the same kind" }
+  call system_clock(count_rate=j8,count_max=k)  ! { dg-error "have the same kind" }
+  call system_clock(i,x,k8)                     ! { dg-error "have the same kind" }
+end
-- 
2.35.3