This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
[patch, fortran] fixes for functions
- From: Daniel Franke <franke dot daniel at gmail dot com>
- To: fortran at gcc dot gnu dot org
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Mon, 9 Jul 2007 19:51:50 +0200
- Subject: [patch, fortran] fixes for functions
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:from:to:subject:date:user-agent:cc:mime-version:content-type:message-id; b=qIeRSqaQXdtsRSy/yh8ogvMkO7IUPVIPKUE5+sPiAgg84qPO8Zxie+6Di2kPt2AEuq5XSNFMv7FC3ZFY4TVPhn4TozIvtRUVL/vL2zf8ei+qNyOtrHsqy5x83mPzpD60YIwy37cYY/MzfFaOzH79afwy5AgQfpSa3eUQDlj0NW0=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:from:to:subject:date:user-agent:cc:mime-version:content-type:message-id; b=C982VZ4XgQ9YMB98E+um1wt/Rsc0ugonsr9Zzlk9DAvgI+1UHUgZ3PXW8MhTdBNAFTmxNofbfzbWgaTxJmBslOaYJ+QPWu/SCNq5A22Kd8Fqz7+IxDLERkwHcjS+wJCVoC/5mGn8x5SSuNKwxMcnUGw5SAiAdsBCH7GvsOkj4t8=
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 "" }