http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54082

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |burnus at gcc dot gnu.org

--- Comment #1 from Tobias Burnus <burnus at gcc dot gnu.org> 2012-07-24 
15:43:11 UTC ---
(In reply to comment #0)
> program abs
>   print *, abs(-1)
> end program

That program is invalid.


The compiler knows in the first line only that "abs" is the name of the PROGRAM
- if you then use it as function, it rightly complains.

Note that "abs" is no reserved name, you could also have the following (which
is valid):

program test
  call abs ()
contains
  subroutine abs ()
  end subroutine abs
end program test

In that case, "abs" is a subroutine - and not the intrinsic function "abs".


gfortran gives a better error message if you tell it explicitly that you want
to use "abs" as intrinsic procedure:

program abs
  intrinsic abs
  ...

Then it prints:
  intrinsic abs
               1
Error: PROGRAM attribute conflicts with INTRINSIC attribute at (1)

Otherwise, it has to guess that from the usage - as long the name is not
explicitly used elsewhere. Like in my valid "subroutine abs" example or your
invalid "program abs".


Back to your example, and quoting from the Fortran standard (cf.
http://gcc.gnu.org/wiki/GFortranStandards; F2008, Section 16.2):

"Program units, common blocks, external procedures, entities with binding
labels, external input/output units, pending data transfer operations, and
images are global entities of a program. [...]"

"The global identifier of an entity shall not be the same as the global
identifier of any other entity."

Here, "program abs" is a "program unit" and the function "abs" of "abs(-1)" is
an "external procedure".


(For completeness: As used in my example, the "abs" subroutine is a local
identifier, which per 16.3.1 "shall not be the same as a global identifier
used in that scope unless [...]" - thus, I used a different name, i.e "test".)


> Compiling above program fails with this confusing error:
>   print *, abs(-1)
>               1
> Error: Symbol at (1) is not appropriate for an expression
> 
> I'm not sure if the program is standard conform or not.
> If it isn't, then the error message could be improved.


Well, there ways to write invalid programs are legion; the compiler should try
to provide a good error message, but that's not always simple. (And the
development currently focuses on correctly implementing the missing features of
Fortran 2003 and 2008.) Nonetheless, providing good diagnostics is a goal.

ifort has: "This global name is invalid in this context.   [ABS]"
NAG has: "Invalid recursive self-reference to ABS"
pathf95 has: "This reference to main program ABS is illegal."

I am not sure that those are better.

Reply via email to