[Bug fortran/35937] Wrong type for charlength of function

fxcoudert at gcc dot gnu dot org gcc-bugzilla@gcc.gnu.org
Mon May 12 17:44:00 GMT 2008



------- Comment #4 from fxcoudert at gcc dot gnu dot org  2008-05-12 17:44 -------
It's not an issue limited to SCAN, it can happen to all functions (intrinsics
or not). Reduced testcase, showing the failure even without
-fdefault-integer-8:

program main
  implicit none

  print *, len(f5('1'))
contains

  function f5 (c)
    character(len=1_8) :: c
    character(len=scan('123456789', c)) :: f5
  end function f5

end program main


The problem is subtle: when the length of f5 is evaluated, it's done outside of
the body of f5 itself. And there, the string length variable is somehow set to
a integer(kind=8), as is specified by its original type. This is a failure in
the complicated mapping mechanism, and a nightmare to track, because we start
with the right type but something somewhere changes it under our feet. After a
long chase, I found it's gfc_finish_interface_mapping() that is not doing its
job properly, and the following patch fixes it:

Index: trans-expr.c
===================================================================
--- trans-expr.c        (revision 135088)
+++ trans-expr.c        (working copy)
@@ -1671,7 +1671,19 @@ gfc_finish_interface_mapping (gfc_interf
        gfc_init_se (&se, NULL);
        gfc_conv_expr (&se, expr);

-       se.expr = gfc_evaluate_now (se.expr, &se.pre);
+       /* Give the string length the right type.  If we deal with a
+          simple constant, which is the most common case, we don't need
+          to create a temporary variable.  */
+       if (CONSTANT_CLASS_P (se.expr))
+         se.expr = fold_convert (gfc_charlen_type_node, se.expr);
+       else
+         {
+           tree tmp = gfc_create_var (gfc_charlen_type_node, "slength");
+           gfc_add_modify_expr (&se.pre, tmp,
+                                fold_convert (gfc_charlen_type_node,
se.expr));
+           se.expr = tmp;
+         }
+
        gfc_add_block_to_block (pre, &se.pre);
        gfc_add_block_to_block (post, &se.post);


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |patch
   Last reconfirmed|0000-00-00 00:00:00         |2008-05-12 17:44:16
               date|                            |
            Summary|char_result_5.f90 /         |Wrong type for charlength of
                   |char_result_6.f90 fail with |function
                   |-fdefault-integer-8         |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35937



More information about the Gcc-bugs mailing list