operator overloads and the Sun Fortran compiler 6.2

From: Richard L. Naff (rlnaff_at_usgs.gov)
Date: 09/30/04


Date: 30 Sep 2004 11:44:59 -0700

We are having a problem with Fortran operator overloads while
compiling with Sun WorkShop 6 update 2 Fortran 95 6.2 Patch 111690-10
2003/08/28; the operating system is Solaris. A sample program, called
"vec_over_test.f90," follows to illustrate the problem:

module new_types
  ! ...
  implicit none
  type vector_3
     real::x,y,z
  end type vector_3
  ! ...
end module new_types

module vector_operators
  ! ...
  ! ... Define vector operations
  ! ...
  use new_types
  ! ...
  implicit none
  ! ...
  private

  interface operator(.z.)
  ! xxx interface operator(.c.)
     module procedure v_cp
  end interface

  public :: operator(.z.)
  ! xxx public :: operator(.c.)

contains

  function v_cp(V_1,V_2)
    ! ... Form cross product of V_1 and V_2, place result in v_cp.
    type(vector_3), intent(in) :: V_1,V_2
    type(vector_3) :: v_cp
    ! ...
    v_cp%x=V_1%y*V_2%z-V_1%z*V_2%y
    v_cp%y=V_1%z*V_2%x-V_1%x*V_2%z
    v_cp%z=V_1%x*V_2%y-V_1%y*V_2%x
  end function v_cp

end module vector_operators

program main
  use new_types
  use vector_operators
  type(vector_3):: v_1, v_2, result
  v_1%x=1
  v_1%y=1
  v_1%z=1
  v_2%x=3
  v_2%y=4
  v_2%z=5
  result = v_1 .z. v_2
  ! xxx result = v_1 .c. v_2
  print*, "result=",result
  ! ...
end program main

....

The operator overload, ".z.", is a cross product of two three
dimensional vectors. This program will not compile under Sun's
Fortran 95 6.2 and emits the following error:

(ksh) sunarcolkr.cr.usgs.gov% f95 -g vec_over_test.f90 -o
vec_over_test.ex

  result = v_1 .z. v_2
                 ^
"vec_over_test.f90", Line = 52, Column = 18: ERROR: Unexpected syntax:
"operator or EOS" was expected but found ".".

....

However, if the symbol for the operator overload is chaged to ".c.",
then the program not only compiles but runs:

(ksh) sunarcolkr.cr.usgs.gov% ./vec_over_test.ex
 result= 1.0 -2.0 1.0

....
(Lines in the program starting with "! xxx" contain this alternative.)

The program will not compile if either ".x.", ".y.", or ".z." is used
as the overload symbol, but use of other letters or combinations of
letters does allow compilation and execution. This behavior and the
error message leads us to deduce that it is likely that the Sun
fortran compiler is confusing the operator overload, in the cases of
".x.", ".y.", and ".z.", with the derived type, vector_3. The derived
type vector_3 uses x,y,z as objects of the structure. Thus it appears
that the Sun compiler confuses "v_1 .z." for "v_1 %z." and complains
about the additional period.

To my mind, this is a compiler problem and should not be happening.
However, we would appreciate confirmation, or other comments, from
those of you who engage in written standards for Fortran.

Thank you, Richard L. Naff