Unexpected Fortran compiler behavior on variable declarations.
There are several problems concerning Sun Fortran compiler for Linux, concerning variable declarations:
(1) A variable can be declared two or more times within the same
program scope. Surprisingly, the compiler accepts this, without any error or
warning message. This is clearly a bug.
(2) There is no warning message if a variable is used but never set, even if the -w4 flag is used. This is a serious bug.
(3) There is no warning message if a variable is declared but never
used.
To illustrate the bugs, compile the following simple program:
! -
module fooModule
implicit none
public :: add
contains
function add(x,y)
real, intent(in) :: x,y
real :: unused ! This variable is never used and never set; no warning message.
real :: add
add=x+y
end function add
end module fooModule
program TryProg
use fooModule
implicit none
real :: x
integer :: i
integer :: i ! Duplicated declaration. The compiler accepts this!
i=3
print*,i
print*,add(x,2.0) ! Variable x is used but not set; no warning message.
end program TryProg
! -
EXPECTED BEHAVIOR:
(1) Something like "ERROR: Duplicate declaration of 'i'" or "ERROR:
Symbol 'i' already has basic type of INTEGER."
(2) WARNING: Variable 'x' is used but not set.
(3) WARNING: Variable 'unused' is never used and never set.
ACTUAL BEHAVIOR:
No error or warning messages; expected one error and two warnings.
The bugs appear in all Fortran compilers for Linux, including latest release, SSX2.
# 1
> There are several problems concerning Sun Fortran
> compiler for Linux, concerning variable
> declarations:
>
> (1) A variable can be declared two or more times
> within the same
> program scope. Surprisingly, the compiler accepts
> this, without any error or
> warning message. This is clearly a bug.
It is not always correct. In your example the variable is declared twice with [b]same[/b] declaration. There is not a standard's constraint on this situation and it is not leads the ambiguity so such code is accepted by our compiler. In other hand, If you use [b]different[/b] declarations and write, for example,
...
integer i
integer(1) i
...
you will get the error message, as you want.
> (2) There is no warning message if a variable is used
> but never set, even if the -w4 flag is used. This is
> a serious bug.
> (3) There is no warning message if a variable is
> declared but never
> used.
In Sun Studio on Solaris we have special system GPC (Global Program Checker) that discovers such situations and have many other functions. GPC is not ported on Linux.
Have you use this system on Solaris (flags family -Xlist)? Or maybe you have a posibility to try it now (on Solaris)? Would you like to have such functionality on Linux?
Alex
# 2
> There are several problems concerning Sun Fortran
> compiler for Linux, concerning variable
> declarations:
>
> (1) A variable can be declared two or more times
> within the same
> program scope. Surprisingly, the compiler accepts
> this, without any error or
> warning message. This is clearly a bug.
> The bugs appear in all Fortran compilers for Linux,
> including latest release, SSX2.
You have cited a number of constraint violations. The
Fortran standard states
A processor conforms to this standard if
. . .
(3) It contains the capability to detect and report the
use within a sumitted program unit of an
additional form or relationship that is not
permitted by the numbered syntax rules or
constraints, including the deleted features
described in Annex B;
In English, this statement means that a conforming
implementation must be able to detect and report
syntax errors and constraint violations. It does not
mean that it must detect and report them by default.
Sun f95, like most other Fortran compilers, does not
give errors or warnings for the use of common
extensions by default. The option -ansi enables
the compilation mode that reports such uses.
For example, when I compile the program
PROGRAM MAIN
REAL X
REAL X
END
with the option -ansi, I get
REAL X
^
"t.f", Line = 2, Column = 1: ANSI: Use of the tab character is an extension of the Fortran standard.
REAL X
^
"t.f", Line = 3, Column = 7: ANSI: "X" has been given the REAL attribute more than once. This is nonstandard.
f90comp: 4 SOURCE LINES
f90comp: 0 ERRORS, 0 WARNINGS, 0 OTHER MESSAGES, 2 ANSI
The caret lines up with the identifer X in the output. The column
number is given as column 7, because the letter X is the seventh
character in the line.
# 3
> It is not always correct. In your example the
> variable is declared twice with [b]same[/b]
> declaration. There is not a standard's constraint on
> this situation and it is not leads the ambiguity so
> such code is accepted by our compiler. In other hand,
> If you use [b]different[/b] declarations and write,
> for example,
> ...
> integer i
> integer(1) i
> ...
> you will get the error message, as you want.
I know that, but I think that the compiler should report a warning, even if a variable is declared twice with same declaration. This is what most Linux compilers do in this case.
> In Sun Studio on Solaris we have special system GPC
> (Global Program Checker) that discovers such
> situations and have many other functions. GPC is not
> ported on Linux.
> Have you use this system on Solaris (flags family
> -Xlist)? Or maybe you have a posibility to try it now
> (on Solaris)?
I tried Solaris several months ago; it seems to be very interesting, but I think that it's too early for me to migrate to Solaris right now (one of the problems I have encountered is that I prefer light window managers, such as WindowMaker or FluxBox, which are not included in Solaris distribution).
> Would you like to have such functionality on Linux?
Of course I want similar functionality on Linux (and I don't think that I am the only one). Using a variable before assigning a value to it is a serious bug, and should be detected by any compiler. Sun95 seems to be very powerful, especially in optimization: some experiments I have made using large codes reveal that it is able to produce an AMD-32 executable which is considerably faster than, e.g., gfortran, g95, or Intel Fortran. I would like to use Sun95 as my default Fortran compiler. Unfortunately, bugs like those I have reported don't let me to do so... yet.
# 4
> Sun f95, like most other Fortran compilers, does not
> give errors or warnings for the use of common
> extensions by default. The option -ansi enables
> the compilation mode that reports such uses.
Indeed, the option -ansi reports duplicate declarations. However, your statement, "most Fortran compilers do not give errors or warnings for the use of common extensions by default", is not correct. I have compiled the example program using gfortran, g95, F, and Intel Fortran: by default, all these compilers give warnings concerning duplicate declarations.