public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: sgk@troutmask.apl.washington.edu
Cc: fortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH, v2] Fortran: restrictions on integer arguments to SYSTEM_CLOCK [PR112609]
Date: Sun, 19 Nov 2023 21:46:46 +0100	[thread overview]
Message-ID: <2898e351-eee8-45dd-a05d-0280378ba872@gmx.de> (raw)
In-Reply-To: <ZVlQ-Yzmr4KFTeTM@troutmask.apl.washington.edu>

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

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


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

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


  reply	other threads:[~2023-11-19 20:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-18 22:12 [PATCH] " Harald Anlauf
2023-11-19  0:04 ` Steve Kargl
2023-11-19 20:46   ` Harald Anlauf [this message]
2023-11-20 19:02     ` [PATCH, v2] " Steve Kargl
2023-11-21 11:33       ` Mikael Morin
2023-11-21 21:54         ` [PATCH, v3] " Harald Anlauf
2023-11-21 22:09           ` Harald Anlauf
2023-11-22  9:36             ` Mikael Morin
2023-11-22 18:03               ` Steve Kargl
2023-11-22 20:40                 ` Harald Anlauf
2023-11-22 20:40                   ` Harald Anlauf
2023-11-22 20:36               ` [PATCH, v4] " Harald Anlauf
2023-11-23  9:07                 ` Mikael Morin

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=2898e351-eee8-45dd-a05d-0280378ba872@gmx.de \
    --to=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=sgk@troutmask.apl.washington.edu \
    /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).