This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Constraint violation


On Sat, Nov 25, 2017 at 02:51:21PM +0100, Thomas Koenig wrote:
> Hi Steve,
> 
> > It seems that gfortran has been violating a constraint, which has
> > been in the Fortran standard since at least F95.
> 
> 
> > ! Constraint: The optional comma in a length-selector is permitted only if
> > !             no double colon separator appears in the type-declaration-stmt.
> 
> > On the otherhand, gfortran seem to have never gotten the second
> > constraint correct.
> > 
> > program foo
> >     character*2, parameter :: c(2) = ['ab', 'cd']
> >     print *, c
> > end program foo
> 
> I'm afraid you're going to have to help me out on this one.
> What is the length-selector here, and what is the optinal
> comma?
> 

>From F2003, 

R425 length-selector  is ( [ LEN = ] type-param-value )
                      or * char-length [ , ]

So the length-selector in the below code is '*2,'. 

program foo
   character*2, parameter :: c(2) = ['ab', 'cd']
   print *, c
end program foo

The comma is optional except that it cannot appear if '::' is present.
This is a constraint and gfortran does not report it.  To fix this,
one dives done a rabbit hole.

% gfortran6 -static -o z c.f90 && ./z
 abcd

If one removes the the attribute, one gets

program foo
   character*2, :: c(2) = ['ab', 'cd']
   print *, c
end program foo

% gfortran6 -static c.f90
c.f90:2:17:

    character*2, :: c(2) = ['ab', 'cd']
                 1
Error: Invalid character in name at (1)

A better error message that reports the constraint (to some extent) would be

% gfc -c c.f90
c.f90:2:18:

    character*2, :: c(2) = ['ab', 'cd']
                  1
Error: Double colon at (1) cannot appear after the comma


The constraint seems to resolves an ambiguity.  Again, from F2003 (where
I have shorten the word declaration to decl),

R501
type-decl-stmt  is decl-type-spec [[ , attr-spec ] ... ::] entity-decl-list

The comma belongs to the attr-spec.  As the comma is optional for
the length-selector one might argument that there isn't a problem.

   character*2   , parameter   :: c(2) = ['ab', 'cd']

except now I should be able to do (note extra comma)

   character*2,   , parameter   :: c(2) = ['ab', 'cd']

% gfortran6 -static -o z c.f90 && ./z
c.f90:2:16:

    character*2,, parameter :: c(2) = ['ab', 'cd']
                1
Error: Invalid character in name at (1)

PS: I check the F90 standard, the constraints as given in F95 are
present in F90.

-- 
Steve


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]