Bug 77391 - gfortran allows CHARACTER(LEN=:),PARAMETER:: STRING='constant' buts does not report it as an extension
Summary: gfortran allows CHARACTER(LEN=:),PARAMETER:: STRING='constant' buts does no...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 5.4.0
: P3 normal
Target Milestone: 7.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-26 16:28 UTC by urbanjost
Modified: 2016-10-01 04:58 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-08-26 00:00:00


Attachments
test script with code and output (651 bytes, text/plain)
2016-08-26 16:28 UTC, urbanjost
Details

Note You need to log in before you can comment on or make changes to this bug.
Description urbanjost 2016-08-26 16:28:26 UTC
Created attachment 39501 [details]
test script with code and output

gfortran allows length of ":" on character parameter values; which appears to be an extension of the standard. I would prefer that the extension either not be allowed or be warned about to support code portability.

DETAILS:

 It appears that gfortran has an extension that is not reported when
-std=2008 is used.  Other compilers fail and complain when a colon
is used for the length when declaring a character variable with the
parameter attribute.

For instance, the Intel ifort compiler (version 16.1) complains:

error #8238: A colon may be used as a type parameter value only in the
declaration of an entity or component that has the POINTER or ALLOCATABLE
attribute.

Which appears to be a quote from the f2008 standard, 4.2.6.

6 A type parameter value may be specified by a type specification (4.4, 4.5.9).

  R401       type-param-value             is scalar-int-expr
                                          or *
                                          or :

  C401       (R401) The type-param-value for a kind type parameter shall be an initialization expression.

  C402       (R401) A colon shall not be used as a type-param-value except in the declaration of an entity or component
             that has the POINTER or ALLOCATABLE attribute.

Example code:

program picky
character(len=*),parameter :: ident1="@(#)one(3f): with asterisk"
character(len=:),parameter :: ident2="@(#)two(3f): with colon, which appears to be an extension?"
write(*,*)ident1
write(*,*)ident2
end program picky
Comment 1 kargls 2016-08-26 18:28:55 UTC
Patches submitted to fortran@gcc.gnu.org mailing list.
Comment 2 Dominique d'Humieres 2016-08-26 19:44:51 UTC
Sorry, but I think the constraint C402 does not apply to named constants (AKA PARAMETER). The constraint appears in

> 4.2 Type parameters
> 1 A type might be parameterized. In this case, the set of values,
> the syntax for denoting the values, and the set of operations on the values
> of the type depend on the values of the parameters.

I think the relevant parts for CHARACTER PARAMETER are

> 1.3.39.2
> named constant
> named data object with the PARAMETER attribute (5.5.13)

> 1.3.149.12.4
> length type parameter
> type parameter whose value is permitted to be assumed, deferred,
> or given by a specification expression

> 5.5.13 PARAMETER attribute
> 1 The PARAMETER attribute specifies that an entity is a named constant.
> The entity has the value specified by its constant-expr , converted,
> if necessary, to the type, type parameters and shape of the entity.
> C550 An entity with the PARAMETER attribute shall not be a variable,
> a coarray, or a procedure.
> C551 An expression that specifies a length type parameter or array bound
> of a named constant shall be a constant expression.

Last point, I don't see how a PARAMETER can be ALLOCATABLE.
Comment 3 kargls 2016-08-26 20:07:55 UTC
(In reply to Dominique d'Humieres from comment #2)
> Sorry, but I think the constraint C402 does not apply to named constants
> (AKA PARAMETER).

It applies.  C402 is a number constraint.  A Fortran processor
must report violations of a constraint.

C402 (R401) A colon shall not be used as a type-param-value except
in the declaration of an entity or component that has the POINTER
or ALLOCATABLE attribute.

There is no "except for a named constant".


> Last point, I don't see how a PARAMETER can be ALLOCATABLE.

It takes all of 2 minutes to see that gfortran already reports
conflicts

troutmask:sgk[208] gfc7 -c a.f90
a.f90:3:34:

 character(len=:),parameter,pointer :: ident2="which appears to be an extension?"
                                  1
Error: PARAMETER attribute conflicts with POINTER attribute at (1)
troutmask:sgk[209] gfc7 -c a.f90
a.f90:3:26:

 character(len=:),parameter,allocatable :: ident2="which appears to be an extension?"
                          1
Error: PARAMETER attribute conflicts with ALLOCATABLE attribute at (1)
Comment 4 Dominique d'Humieres 2016-08-26 20:45:43 UTC
> C402 (R401) A colon shall not be used as a type-param-value except
> in the declaration of an entity or component that has the POINTER
> or ALLOCATABLE attribute.
>
> There is no "except for a named constant".

(1) As you have noticed this requirement does not make sense for PARAMETER.

(2) IMO this conflicts with

> type parameter whose value is permitted to be assumed, deferred,
                                                         ^^^^^^^^
Comment 5 Jerry DeLisle 2016-08-27 12:45:45 UTC
F2008 4.2

9 An asterisk as a type-param-value specifies that a length type parameter is an assumed type parameter. It is used for a dummy argument to assume the type parameter value from the effective argument, for an associate name in a SELECT TYPE construct to assume the type parameter value from the corresponding selector, and for a named constant of type character to assume the character length from the constant-expr .

I think the last fragment clarifies that the following is the right way to do it.

character(len=*),parameter :: ident1="@(#)one(3f): with asterisk"

and C402 excludes the use of the colon without exception

Repeating myself:

"named constant of type character to assume the character length from the constant-expr."

Is very explicit.
Comment 6 kargls 2016-09-04 20:01:20 UTC
Author: kargl
Date: Sun Sep  4 20:00:48 2016
New Revision: 239982

URL: https://gcc.gnu.org/viewcvs?rev=239982&root=gcc&view=rev
Log:
2016-09-04  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/77391
	* resolve.c (deferred_requirements): New function to check F2008:C402.
	(resolve_fl_variable,resolve_fl_parameter): Use it.
 
2016-09-04  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/77391
	* gfortran.dg/pr77391.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/pr77391.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 kargls 2016-09-04 20:20:55 UTC
Fixed on trunk.
Comment 8 urbanjost 2016-09-06 00:07:14 UTC
Thanks. The asterick in this case and when used to initialize allocatable character variables does not fit my old rule of thumb that "an asterick means the variable is allocated and has a fixed length and size defined in another scoping unit" (it is declared in the current unit); but it didn't fit my rule about : meaning it was allocatable either. What you can and cannot do with character variables has changed significantly; so I was not positive this was an issue or not. I appreciate the fix and clarifications.
Comment 9 kargls 2016-10-01 04:58:35 UTC
Author: kargl
Date: Sat Oct  1 04:58:02 2016
New Revision: 240693

URL: https://gcc.gnu.org/viewcvs?rev=240693&root=gcc&view=rev
Log:
2016-09-30  Steven G. Kargl  <kargl@gcc.gnu.org>

	Backport from trunk

	PR fortran/77391
	* resolve.c (deferred_requirements): New function to check F2008:C402.
	(resolve_fl_variable,resolve_fl_parameter): Use it.

	PR fortran/77391
	* gfortran.dg/pr77391.f90: New test.

Added:
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/pr77391.f90
Modified:
    branches/gcc-5-branch/gcc/fortran/ChangeLog
    branches/gcc-5-branch/gcc/fortran/resolve.c
    branches/gcc-5-branch/gcc/testsuite/ChangeLog