[fortran,9/10,Regression] PR 93236 -fno-automatic and RECURSIVE

Message ID 8663a201-5172-ea8a-e900-1f0cd9ddbb94@codethink.co.uk
State New
Headers show
Series
  • Untitled series #21708
Related show

Commit Message

Mark Eggleston Jan. 16, 2020, 2:47 p.m.
Please find attached patch to fix this regression.

OK for master and 9 branch?

Change logs:

gcc/fortran

     Mark Eggleston  <mark.eggleston@codethink.com>

     PR fortran/93236
     * resolve.c (resolve_types): Declare boolean recursive and set with the
     value of the recursive attribute of namespace proc_name symbol
     structure if it exists.  Call gfc_save_all if both flag_automatic and
     recursive are false or ns->save_all is true.

gcc/testsuite

     Mark Eggleston  <mark.eggleston@codethink.com>
     Tobias Burnus  <burnus@gcc.gnu.org>

     * gfortran.dg/pr93263_1.f90: New test.
     * gfortran.dg/pr93263_2.f90: New test.

-- 
https://www.codethink.co.uk/privacy.html

Comments

Tobias Burnus Jan. 16, 2020, 4:29 p.m. | #1
On 1/16/20 3:47 PM, Mark Eggleston wrote:
> Please find attached patch to fix this regression.

> OK for master and 9 branch?


OK for both – thanks!

As note to others:

* Up to Fortran 2008, 'recursive' attribute is required if a proc is 
recursively used; since Fortran 2018 recursive is the default (and the 
non_recursive attribute exists as well). For this patch (and the near 
future), we mostly ignore this bit of F2018. See also PR91413.

* The PR is about a vendor extension – cf. test case pr93263_1.f90. The 
second test case should be standard conforming and ensures that neither 
this patch (nor a future patch) breaks it. [Namely, static memory is 
indeed used as requested.]

Thanks,

Tobias

> Change logs:

> gcc/fortran

>

>     Mark Eggleston  <mark.eggleston@codethink.com>

>

>     PR fortran/93236

>     * resolve.c (resolve_types): Declare boolean recursive and set 

> with the

>     value of the recursive attribute of namespace proc_name symbol

>     structure if it exists.  Call gfc_save_all if both flag_automatic and

>     recursive are false or ns->save_all is true.

>

> gcc/testsuite

>

>     Mark Eggleston  <mark.eggleston@codethink.com>

>     Tobias Burnus  <burnus@gcc.gnu.org>

>

>     * gfortran.dg/pr93263_1.f90: New test.

>     * gfortran.dg/pr93263_2.f90: New test.

>

Patch

From aefc0be25bc7ea1a216ac1950a856d5b654d5493 Mon Sep 17 00:00:00 2001
From: Mark Eggleston <markeggleston@gcc.gnu.org>
Date: Thu, 16 Jan 2020 14:08:11 +0000
Subject: [PATCH] Fortran: PR93263 -fno-automatic and RECURSIVE

The use of -fno-automatic should not affect the save attribute of a
recursive procedure. The first test case checks unsaved variables
and the second checks saved variables.
---
 gcc/fortran/resolve.c                   |  3 ++-
 gcc/testsuite/gfortran.dg/pr93263_1.f90 | 29 +++++++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr93263_2.f90 | 24 ++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr93263_1.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr93263_2.f90

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 6f2a4c4d65a..bddab39d023 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -17079,6 +17079,7 @@  resolve_types (gfc_namespace *ns)
   gfc_data *d;
   gfc_equiv *eq;
   gfc_namespace* old_ns = gfc_current_ns;
+  bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
 
   if (ns->types_resolved)
     return;
@@ -17132,7 +17133,7 @@  resolve_types (gfc_namespace *ns)
 
   gfc_traverse_ns (ns, resolve_values);
 
-  if (ns->save_all || !flag_automatic)
+  if (ns->save_all || (!flag_automatic && !recursive))
     gfc_save_all (ns);
 
   iter_stack = NULL;
diff --git a/gcc/testsuite/gfortran.dg/pr93263_1.f90 b/gcc/testsuite/gfortran.dg/pr93263_1.f90
new file mode 100644
index 00000000000..f96b3589411
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr93263_1.f90
@@ -0,0 +1,29 @@ 
+! { dg-do compile }
+! { dg-options "-fno-automatic -fdump-tree-original" }
+!
+! Test contributed by Mark Eggleston  <mark.eggleston@codethink.com>
+
+program main
+  implicit none
+  call check(2)
+end 
+
+recursive subroutine check(n)
+  implicit none
+  integer n, a
+  a = 10
+  print*,"n=",n
+  if (n==1) then
+    a=a-1
+    print*,"assigning a=",a
+  else
+    a=a-2
+    print*,"assigning a=",a
+    call check(n-1)
+  endif
+  print*,"a=",a
+end 
+
+! { dg-final { scan-tree-dump-not "static integer\\(kind=4\\) a" "original" } }
+! { dg-final { scan-tree-dump-not "integer\\(kind=4\\) a" "original" } }
+
diff --git a/gcc/testsuite/gfortran.dg/pr93263_2.f90 b/gcc/testsuite/gfortran.dg/pr93263_2.f90
new file mode 100644
index 00000000000..fd353c6b548
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr93263_2.f90
@@ -0,0 +1,24 @@ 
+! { dg-do run }
+!
+! Test contributed by Tobias Burnus  <burnus@gcc.gnu.org>
+
+  integer :: cnt
+  cnt = 0
+  call sub()
+  if (cnt /= 5) stop 1
+contains
+  recursive subroutine sub()
+    save
+    logical :: first = .true.
+    integer :: i
+    cnt = cnt + 1
+    if (first) then
+      first = .false.
+      i = 1
+    end if
+    print *, "Hello", i
+    i = i + 1
+    if (i <= 5) call sub()
+  end subroutine
+end
+
-- 
2.11.0