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.
Confirmed.
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)]
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)]
Patch proposed here: http://gcc.gnu.org/ml/fortran/2006-05/msg00108.html
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
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
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
Fixed on mainline and 4.1.