User account creation filtered due to spam.

Bug 33957 - gfortran rejects valid initialization expression
Summary: gfortran rejects valid initialization expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.3.0
Assignee: Francois-Xavier Coudert
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch, rejects-valid
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2007-10-31 12:06 UTC by Toby White
Modified: 2007-11-16 22:40 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-11-09 01:25:46


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Toby White 2007-10-31 12:06:18 UTC
gfortran rejects the following:

function bug(i) result(c)
  integer, pointer :: i
  character(len=merge(1,2, associated(p)) :: c
end function bug

Error: Inquiry function 'associated' at (1) is not permitted in an
initialization expression

However according to 7.1.6.2 of ISO/IEC1539-1 there is no such prohibition:

R734: specification-expr is scalar-int-expr
Constraint: scalar-int-expr shall be a restricted expression:

A restricted expression is [composed of]

(2) a variable that is a dummy argument that has neither then OPTIONAL nor the INTENT(OUT) attribut [...]

[ie referencing a POINTER argument is ok]

[...]

(8) a reference to any other intrinsic function defined in this standard where each argument is a restricted expression.

[...]

(and indeed no other compiler I have access to has complained about this.)
Comment 1 Harald Anlauf 2007-10-31 12:59:25 UTC
(In reply to comment #0)

> [...]
> 
> (and indeed no other compiler I have access to has complained about this.)

As a side note: xlf 9.1 says this is a F2003 feature and not a F95 feature.
Comment 2 Dominique d'Humieres 2007-10-31 13:17:19 UTC
The test case is bogus (missing closing parenthesis and p is not a pointer). I hope the following is valid:

! { dg-do compile }
function bug(i) result(c)
  integer, pointer :: i
  character(len=merge(1,2, associated(i))) :: c
  c = ""
end function bug

it is accepted by xlf and g95 (with or without -std=f95).

If my understanding of the proc 'check_inquiry' in expr.c is correct, 'associated' is not in the set inquiry_func_f2003.
Comment 3 Dominique d'Humieres 2007-10-31 13:33:43 UTC
(7) A specification inquiry where each designator or function argument is
 (a) a restricted expression or
 (b) a variable whose properties inquired about are not
  (i) dependent on the upper bound of the last dimension of an assumed-size array,
  (ii) deferred, or
  (iii) defined by an expression that is not a restricted expression,
 (8) A reference to any other standard intrinsic function where each argument is a restricted expression,

A specification inquiry is a reference to
(1) an array inquiry function (13.5.7),
(2) the bit inquiry function BIT_SIZE,
(3) the character inquiry function LEN,
(4) the kind inquiry function KIND,
(5) the character inquiry function NEW_LINE,
(6) a numeric inquiry function (13.5.6),
(7) a type parameter inquiry (6.1.3), or
(8) an IEEE inquiry function (14.9.1),

ASSOCIATED appears in 13.5.8, but is not listed above, so it is probably not a valid "specification inquiry" however this restriction seems lifted by (8) (it is probably the appropriate time to have a "simplified" F2003 draft!-).

13.5.8 Other inquiry functions

ALLOCATED (ARRAY) or ALLOCATED (SCALAR) Allocation status
ASSOCIATED (POINTER [, TARGET]) Association status inquiry or comparison
BIT_SIZE (I) Number of bits of the model
EXTENDS TYPE OF (A, MOLD) Same dynamic type or an extension
LEN (STRING [, KIND]) Length of a character entity
NEW_LINE (A) Newline character
PRESENT (A) Argument presence
SAME_TYPE_AS (A, B) Same dynamic type
Comment 4 Toby White 2007-10-31 13:58:16 UTC
(Sorry for mis-typed example, I can't cut & paste from the VM I'm working in into my web-browser. Your corrected version is what I meant to type)

Well I'm going from the F95 standard (which is the only one I have to hand; and in any case I am trying to write F95-compliant code here, so I'm not overly concerned about F2003 personally). F95 has no concept of a "specification inquiry" (that I can see). Section (7) of the definition of a "restricted expression" seems to cover mostly the same ground, and reads in full:

(7) A reference to an intrinsic function that is
  (a) an array inquiry function (13.11.15) other than ALLOCATED,
  (b) the bit inquiry function BIT_SIZE
  (c) the character enquiry function LEN
  (d) the kind inquire function KIND, or
  (e) a numeric inquiry function (13.11.8)
and where each argument of the function is
  (a) a restricted expression
  (b) a variable whose properties inquired about are not 
    (i) dependent on the upper bound of the last dimension of an assumed-size array,
    (ii) defined by an expression that is not a restricted expression, or
    (iii) definable by an ALLOCATE or pointer assignment expression

That is - the expression is not allowed under (7) - but it is under (8).
Comment 5 Francois-Xavier Coudert 2007-11-09 01:25:46 UTC
The patch below should do it.

Index: expr.c
===================================================================
--- expr.c      (revision 129869)
+++ expr.c      (working copy)
@@ -1981,11 +1981,7 @@ check_inquiry (gfc_expr *e, int not_rest
       break;
 
   if (functions[i] == NULL)
-    {
-      gfc_error ("Inquiry function '%s' at %L is not permitted "
-                "in an initialization expression", name, &e->where);
-      return MATCH_ERROR;
-    }
+    return MATCH_ERROR;
 
   /* At this point we have an inquiry function with a variable argument.  The
      type of the variable might be undefined, but we need it now, because the
Comment 6 Francois-Xavier Coudert 2007-11-16 22:38:40 UTC
Subject: Bug 33957

Author: fxcoudert
Date: Fri Nov 16 22:38:21 2007
New Revision: 130246

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130246
Log:
	PR fortran/33957

	* gfortran.dg/initialization_15.f90 : New test.

	* expr.c (check_inquiry): Don't call gfc_error now.

Added:
    trunk/gcc/testsuite/gfortran.dg/initialization_15.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Francois-Xavier Coudert 2007-11-16 22:40:03 UTC
Fixed.