Bug 31764 - NEW_LINE with array argument
Summary: NEW_LINE with array argument
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
Keywords: ice-on-valid-code, patch
Depends on:
Blocks: 20585
  Show dependency treegraph
Reported: 2007-04-30 14:56 UTC by Daniel Franke
Modified: 2007-05-06 23:57 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail: 4.3.0
Last reconfirmed: 2007-05-06 15:22:57


Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Franke 2007-04-30 14:56:03 UTC
F2003, 13.7.85, NEW_LINE, Argument:
A shall be of type character. It may be a scalar or an array.

$> cat newline.f90
WRITE(*,*) NEW_LINE((/'a', 'b', 'c'/))

$> gfortran-svn -g -Wall newline.f90 && ./a.out
newline.f90: In function 'MAIN__':
newline.f90:1: internal compiler error: in gfc_conv_intrinsic_function, at fortran/trans-intrinsic.c:3567

$> gfortran-svn -v
gcc version 4.3.0 20070428 (experimental)
Comment 1 Tobias Burnus 2007-04-30 15:43:12 UTC
> F2003, 13.7.85, NEW_LINE, Argument:
> A shall be of type character. It may be a scalar or an array.

Additional information:

Class. Inquiry function.

Result Characteristics. Character scalar of length one with the same kind type parameter as A.

I.e. no array should be returned! This is different to the elemental functions achar, iachar etc.
Comment 2 Francois-Xavier Coudert 2007-05-04 12:52:17 UTC
(Side note: NEW_LINE is not an elemental function. It can be used with an array argument, but it's different from being elemental.)

As for the bug itself, there's no reason that the argument to NEW_LINE should be constant: the only requirement is that it's of character kind (all that is used in NEW_LINE is the character kind, which is always known at compile-time). So, the following patch fixes it, and I believe it does the right thing:

Index: simplify.c
--- simplify.c  (revision 124415)
+++ simplify.c  (working copy)
@@ -2641,13 +2641,8 @@ gfc_simplify_new_line (gfc_expr *e)
   gfc_expr *result;
-  if (e->expr_type != EXPR_CONSTANT)
-    return NULL;
   result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
   result->value.character.string = gfc_getmem (2);
   result->value.character.length = 1;
   result->value.character.string[0] = '\n';
   result->value.character.string[1] = '\0';     /* For debugger */

This patch is donated to whomever has time to confirm that it does what it should, that it conforms to the standard and that it regtests. (I'll take care of it next week if noone has time before then.) The following testcase now passes:

$ cat a.f90 
  character(len=10) :: a1
  character(len=10) :: a2(2)
  character(len=10), parameter :: a3 = "1234567890"
  character(len=10), parameter :: a4(2) = "1234567890"
  character(len=10), parameter :: a5(2) = repeat("1234567890",2)

  print *, ichar(new_line(a1))
  print *, ichar(new_line(a2))
  print *, ichar(new_line(a3))
  print *, ichar(new_line(a4))
  print *, ichar(new_line(a5))
$ gfortran a.f90 && ./a.out
Comment 3 Francois-Xavier Coudert 2007-05-06 23:57:03 UTC
Subject: Bug 31764

Author: fxcoudert
Date: Sun May  6 22:56:52 2007
New Revision: 124482

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=124482
	PR fortran/31764

	* simplify.c (gfc_simplify_new_line): NEW_LINE can be simplified
	even for non constant arguments.

	* gfortran.dg/new_line.f90: Add new checks.


Comment 4 Francois-Xavier Coudert 2007-05-06 23:57:23 UTC
Fixed on mainline.