Bug 26551 - gfortran compiles recursive subroutines declared without the RECURSIVE attribute
gfortran compiles recursive subroutines declared without the RECURSIVE attribute
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
unknown
: P3 normal
: 4.1.2
Assigned To: Francois-Xavier Coudert
http://gcc.gnu.org/ml/fortran/2006-05...
: accepts-invalid, patch
Depends on:
Blocks: 19292
  Show dependency treegraph
 
Reported: 2006-03-03 19:50 UTC by Brian Taylor
Modified: 2006-05-27 09:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.2.0 4.1.2
Known to fail:
Last reconfirmed: 2006-05-07 11:08:22


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brian Taylor 2006-03-03 19:50:28 UTC
A subroutine or function that calls itself recursively must be declared with the RECURSIVE attribute.  gfortran should refuse to compile the subroutine below and return an error indicating that the RECURSIVE attribute must be specified.

>$ cat sub.f90 
SUBROUTINE SUB()
CALL SUB()
END SUBROUTINE
>$ gfortran -std=f95 -pedantic sub.f90 -c
>$ gfortran -c -v
Using built-in specs.
Target: powerpc-apple-darwin8.1.0
Configured with: ../gcc-4.1-20050611/configure --enable-threads=posix --enable-languages=c++,f95
Thread model: posix
gcc version 4.1.0 20050611 (experimental)

For comparison, here is output from g95 and xlf90:
>$ g95 -std=f95 -pedantic sub.f90 -c
In file sub.f90:2

CALL SUB()
         1
Error: SUBROUTINE 'sub' at (1) must be RECURSIVE in order to call itself
>$ xlf90 -qsuffix=cpp=f90 -f95 sub.f90 -c
"sub.f90", line 2.6: 1513-126 (S) Recursive calls are only permitted when the RECUR option is specified or the RECURSIVE keyword is specified.
** sub   === End of Compilation 1 ===
1501-511  Compilation failed for file sub.f90.


On a related note, the limitations on RECURSIVE subroutines and functions should also be extended to any ENTRY statements within the subprogram.  For example, the following code is also illegal but gfortran currently accepts it:

>$ cat sub2.f90 
SUBROUTINE SUB2()
ENTRY ENT2()
CALL ENT2()
END SUBROUTINE
>$ gfortran -std=f95 -pedantic sub2.f90 -c

For comparison, xlf has the following output:
>$ xlf90 -qsuffix=cpp=f90 -f95 sub2.f90 -c
"sub2.f90", line 3.6: 1513-126 (S) Recursive calls are only permitted when the RECUR option is specified or the RECURSIVE keyword is specified.
** sub2   === End of Compilation 1 ===
1501-511  Compilation failed for file sub2.f90.
Comment 1 Andrew Pinski 2006-03-03 20:00:33 UTC
Confirmed.
Comment 2 Andrew Pinski 2006-03-03 20:02:44 UTC
g77 errored out:
[dandelion:~] pinskia% /Volumes/home/pinskia/gcc-3.3/bin/g77 t.f -ffree-form
t.f: In subroutine `sub':
t.f:1: 
   SUBROUTINE SUB()
              1
t.f:2: (continued):
   CALL SUB()
        2
Invalid declaration of or reference to symbol `sub' at (2) [initially seen at (1)]
Comment 3 Brian Taylor 2006-03-03 21:05:51 UTC
The second example (involving a recursive call to an ENTRY statement) also errors out on g77 (v3.4.4):

>$ g77 -ffree-form sub2.f -c
sub2.f: In subroutine `sub2':
sub2.f:2: 
   ENTRY ENT2()
         1
sub2.f:3: (continued):
   CALL ENT2()
        2
Invalid declaration of or reference to symbol `ent2' at (2) [initially seen at (1)]
Comment 4 Francois-Xavier Coudert 2006-05-07 11:08:22 UTC
Patch proposed here: http://gcc.gnu.org/ml/fortran/2006-05/msg00108.html
Comment 5 Francois-Xavier Coudert 2006-05-17 14:11:52 UTC
Subject: Bug 26551

Author: fxcoudert
Date: Wed May 17 14:11:40 2006
New Revision: 113860

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113860
Log:
	PR fortran/26551

	* resolve.c (resolve_call, resolve_function): Issue an error
	if a function or subroutine call is recursive but the function or
	subroutine wasn't declared as such.

	* gfortran.dg/recursive_check_1.f: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/recursive_check_1.f
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog

Comment 6 Francois-Xavier Coudert 2006-05-17 14:15:07 UTC
Subject: Bug 26551

Author: fxcoudert
Date: Wed May 17 14:14:56 2006
New Revision: 113861

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113861
Log:
	Testcase forgotten in the previous commit.

	PR fortran/26551
	* gfortran.dg/recursive_check_2.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/recursive_check_2.f90
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 7 Francois-Xavier Coudert 2006-05-27 09:06:19 UTC
Subject: Bug 26551

Author: fxcoudert
Date: Sat May 27 09:06:01 2006
New Revision: 114152

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=114152
Log:
	Backports from mainline

	PR fortran/27552
	* dump-parse-tree.c (gfc_show_expr): Deal with Hollerith constants.
	* data.c (create_character_intializer): Set from_H flag if character is
	initialized by Hollerith constant.

	PR fortran/27320
	* dump-parse-tree.c (gfc_show_code_node): Try harder to find the
	called procedure name.

	PR fortran/26551
	* resolve.c (resolve_call, resolve_function): Issue an error
	if a function or subroutine call is recursive but the function or
	subroutine wasn't declared as such.
	* gfortran.dg/recursive_check_1.f: New test.
	* gfortran.dg/recursive_check_2.f90: New test.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/recursive_check_1.f
      - copied unchanged from r113861, trunk/gcc/testsuite/gfortran.dg/recursive_check_1.f
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/recursive_check_2.f90
      - copied unchanged from r113861, trunk/gcc/testsuite/gfortran.dg/recursive_check_2.f90
Modified:
    branches/gcc-4_1-branch/gcc/fortran/ChangeLog
    branches/gcc-4_1-branch/gcc/fortran/data.c
    branches/gcc-4_1-branch/gcc/fortran/dump-parse-tree.c
    branches/gcc-4_1-branch/gcc/fortran/resolve.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 8 Francois-Xavier Coudert 2006-05-27 09:07:18 UTC
Fixed on mainline and 4.1.