In the program below, the optional argument arg is used as part of the specification expression for the length of a string. This is forbidden by section 7.1.6.2; constraint (2) of the F95 standard (and wouldn't make much sense anyway - what happens when the function is called without the optional argument?) gfortran accepts it without complaint, though. parabrisas:~/test% cat testpresent.f90 function testpresent(arg) integer, intent(in), optional :: arg character(len=arg) :: s logical :: testpresent testpresent=.true. end function testpresent parabrisas:~/test% gfortran -c testpresent.f90 parabrisas:~/test% gfortran -v Using built-in specs. Configured with: ../gcc/configure --prefix=/home/tow/root/gcc-4.0 --enable-languages=c,f95 : (reconfigured) ../gcc/configure --prefix=/home/tow/root/gcc-4.0 --with-gcc-version-trigger=/home/tow/dl/gcc/gcc/gcc/version.c --enable-languages=c,f95 --no-create --no-recursion : (reconfigured) ../gcc/configure --prefix=/home/tow/root/gcc-4.0 --with-gcc-version-trigger=/home/tow/dl/gcc/gcc/gcc/version.c --enable-languages=c,f95 --no-create --no-recursion Thread model: posix gcc version 4.0.0 20050127 (experimental)
The complaint is a segfault at runtime when you actually want to do anything with the string whose length depends on a missing optional argument. This isn't too bad (the same thing happens if you access a missing optional argument). Thomas
(In reply to comment #1) > The complaint is a segfault at runtime when > you actually want to do anything with the > string whose length depends on a missing > optional argument. This isn't too bad (the > same thing happens if you access a missing > optional argument). Indeed - but even if the optional argument were present, you're still not allowed to access it in a specification expression, and this is a constraint that the compiler is required to detect at compile time. (It's a constraint that I'd quite like to see partially lifted - it would be very useful to be able to use present(arg) as part of a specification expression - but it is forbidden according to the current standard.)
Patch here: http://gcc.gnu.org/ml/fortran/2005-03/msg00220.html
Subject: Bug 20323 CVSROOT: /cvs/gcc Module name: gcc Changes by: tobi@gcc.gnu.org 2005-03-13 18:37:17 Modified files: gcc/fortran : ChangeLog resolve.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gfortran.dg: spec_expr_1.f90 Log message: fortran/ PR fortran/20323 * resolve.c (gfc_resolve): Check if character lengths are specification expressions. testsuite/ PR fortran/20323 * gfortran.dg/spec_expr_1.f90: New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&r1=1.349&r2=1.350 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/resolve.c.diff?cvsroot=gcc&r1=1.35&r2=1.36 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5154&r2=1.5155 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/spec_expr_1.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
Subject: Bug 20323 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-4_0-branch Changes by: tobi@gcc.gnu.org 2005-03-13 18:40:29 Modified files: gcc/fortran : ChangeLog resolve.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gfortran.dg: spec_expr_1.f90 Log message: fortran/ > PR fortran/20323 > * resolve.c (gfc_resolve): Check if character lengths are > specification expressions. > > testsuite/ > PR fortran/20323 > * gfortran.dg/spec_expr_1.f90: New test. > Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.335.2.10&r2=1.335.2.11 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/resolve.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.34.2.1&r2=1.34.2.2 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5084.2.38&r2=1.5084.2.39 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/spec_expr_1.f90.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
Fixed.