This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch, fortran] fixes for functions


According to the standard, functions shall not have an initial value, i.e. 
code as the following is illegal. Please find some code snippets below that 
currently are not handled very gracefully:

$> cat f1.f90
  function f()
    integer :: f = 42
  end function

$> gfortran-svn -g -Wall f1.f90
f1.f90: In function 'f1':
f1.f90:1: warning: Function return value not set
f1.f90:1: warning: control reaches end of non-void function


$> cat f2.f90
  function f2() RESULT (r)
    integer :: r = 42
  end function

$> gfortran-svn -g -Wall f2.f90
[...]
The error given is correct - but is printed twice. 


$> cat f3.f90
  function f3() RESULT (f3)
    integer :: f3 = 42
  end function
$> gfortran-svn -g -Wall f3.f90
[...]
Gives a very general error message (Error in function suffix at (1)), this is 
a regression from the bind-c changes. The more helpful error is overwritten 
by the more general one.


The fixes for these issues are simple and the patch should thus be 
self-explanatory, there is no PR regarding this that I am aware of. 

Full testsuite coverage is provided. Please note that 
the "dg-excess-error"-directive adds an XFAIL to the results.


:ADDPATCH fortran:

gcc/fortran:
2007-07-09  Daniel Franke  <franke.daniel@gmail.com>

	* decl.c (gfc_match_suffix): Removed surplus general error that hides 
	a more specific message.
	* resolve.c (resolve_fl_variable): Reject illegal initializiers only if 
	not already done.
	(resolve_fl_procedure): Added check for initializers of functions.

gcc/testsuite:
2007-07-09  Daniel Franke  <franke.daniel@gmail.com>

	* gfortran.dg/func_decl_4.f90: New test.


Regression tested on i686-pc-linux-gnu. Ok for trunk?

Regards
	Daniel
Index: fortran/decl.c
===================================================================
--- fortran/decl.c	(revision 126485)
+++ fortran/decl.c	(working copy)
@@ -3561,12 +3561,6 @@
       break;
     }
 
-  if (is_result == MATCH_ERROR || is_bind_c == MATCH_ERROR)
-    {
-      gfc_error ("Error in function suffix at %C");
-      return MATCH_ERROR;
-    }
-
   if (is_bind_c == MATCH_YES)
     if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
         == FAILURE)
Index: fortran/resolve.c
===================================================================
--- fortran/resolve.c	(revision 126485)
+++ fortran/resolve.c	(working copy)
@@ -6530,7 +6530,7 @@
   }
 
   /* Reject illegal initializers.  */
-  if (sym->value && flag)
+  if (!sym->mark && sym->value && flag)
     {
       if (sym->attr.allocatable)
 	gfc_error ("Allocatable '%s' at %L cannot have an initializer",
@@ -6728,6 +6728,14 @@
 	}
     }
 
+  /* A function shall not have an initializer.  */
+  if (sym->attr.function && sym->value)
+    {
+      gfc_error ("Function '%s' at %L cannot have an inital value",
+		 sym->name, &sym->declared_at);
+      return FAILURE;
+    }
+
   /* An external symbol may not have an initializer because it is taken to be
      a procedure.  */
   if (sym->attr.external && sym->value)
Index: testsuite/gfortran.dg/func_decl_4.f90
===================================================================
--- testsuite/gfortran.dg/func_decl_4.f90	(revision 0)
+++ testsuite/gfortran.dg/func_decl_4.f90	(revision 0)
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! { dg-options "-c" }
+!
+! Functions shall not have an initializer.
+!
+
+function f1()                      ! { dg-error "cannot have an inital value" }
+  integer :: f1 = 42
+end function
+
+function f2() RESULT (r)           ! { dg-error "cannot have an initializer" }
+  integer :: r = 42
+end function
+
+function f3() RESULT (f3)          ! { dg-error "must be different than function name" }
+  integer :: f3 = 42
+end function                       ! { dg-excess-errors "" }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]