From 18dd272dfd6b077c005f4e1a0a6cb4ffa906bed4 Mon Sep 17 00:00:00 2001
From: Paul Thomas <pault@gcc.gnu.org>
Date: Sun, 14 Dec 2008 16:00:25 +0000
Subject: [PATCH] re PR fortran/35937 (Wrong type for charlength of function)

2008-12-14  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/35937
	* trans-expr.c (gfc_finish_interface_mapping): Fold convert the
	character length to gfc_charlen_type_node.

2008-12-14  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/35937
	* gfortran.dg/char_length_14.f90: New test.

From-SVN: r142750
---
 gcc/fortran/ChangeLog                        |  6 +++++
 gcc/fortran/trans-expr.c                     |  2 +-
 gcc/testsuite/ChangeLog                      |  5 +++++
 gcc/testsuite/gfortran.dg/char_length_14.f90 | 23 ++++++++++++++++++++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/char_length_14.f90

diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog
index db1fe4209866..ad494810dccb 100644
--- a/gcc/fortran/ChangeLog
+++ b/gcc/fortran/ChangeLog
@@ -1,3 +1,9 @@
+2008-12-14  Paul Thomas  <pault@gcc.gnu.org>
+
+	PR fortran/35937
+	* trans-expr.c (gfc_finish_interface_mapping): Fold convert the
+	character length to gfc_charlen_type_node.
+
 2008-12-12  Daniel Franke  <franke.daniel@gmail.com>
 
 	PR fortran/36355
diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 4ecfa0839d0d..4a84234eb79f 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -1830,7 +1830,7 @@ gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
 	gfc_apply_interface_mapping_to_expr (mapping, expr);
 	gfc_init_se (&se, NULL);
 	gfc_conv_expr (&se, expr);
-
+	se.expr = fold_convert (gfc_charlen_type_node, se.expr);
 	se.expr = gfc_evaluate_now (se.expr, &se.pre);
 	gfc_add_block_to_block (pre, &se.pre);
 	gfc_add_block_to_block (post, &se.post);
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4cd90f02b0c0..0ff2a04222b8 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2008-12-14  Paul Thomas  <pault@gcc.gnu.org>
+
+	PR fortran/35937
+	* gfortran.dg/char_length_14.f90: New test.
+
 2008-12-13  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
 
 	PR libfortran/38504
diff --git a/gcc/testsuite/gfortran.dg/char_length_14.f90 b/gcc/testsuite/gfortran.dg/char_length_14.f90
new file mode 100644
index 000000000000..5827dd95eeeb
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/char_length_14.f90
@@ -0,0 +1,23 @@
+! { dg-do run }
+! PR35937, in which letting the length of 'c' to kind = 8 would
+! screw up the interface and would cause an ICE. Note that this is
+! actually the example of comment #4.
+!
+! Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>
+!
+program main
+  implicit none
+  if (f5 ('1') .ne. "a") call abort
+  if (len (f5 ('1')) .ne. 1) call abort
+  if (f5 ('4') .ne. "abcd") call abort
+  if (len (f5 ('4')) .ne. 4) call abort
+contains
+  function f5 (c)
+    character(len=1_8) :: c
+    character(len=scan('123456789', c)) :: f5
+    integer :: i
+    do i = 1, len (f5)
+       f5(i:i) = char (i+96)
+    end do
+  end function f5
+end program main
-- 
GitLab