PR 50892 Latent bug in char pointer assignment

Message ID 1515235490-10192-1-git-send-email-blomqvist.janne@gmail.com
State New
Headers show
Series
  • PR 50892 Latent bug in char pointer assignment
Related show

Commit Message

Janne Blomqvist Jan. 6, 2018, 10:44 a.m.
Due to r256284 (PR 78534) there was a latent bug that reared it's head
due to different character length types in the pointer
assignment. Fixed by this patch, which also adds a reduced testcase.

Regtested on x86_64-pc-linux-gnu, committed to trunk as obvious.

gcc/fortran/ChangeLog:

2018-01-06  Janne Blomqvist  <jb@gcc.gnu.org>

	PR fortran/50892
	* trans-expr.c (gfc_trans_pointer_assignment): fold_convert rhs to
	lhs type.

gcc/testsuite/ChangeLog:

2018-01-06  Janne Blomqvist  <jb@gcc.gnu.org>

	PR fortran/50892
	* gfortran.dg/char_pointer_assign_icb_1.f90: New test.
---
 gcc/fortran/trans-expr.c                                |  4 +++-
 gcc/testsuite/gfortran.dg/char_pointer_assign_icb_1.f90 | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/char_pointer_assign_icb_1.f90

-- 
2.7.4

Patch

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 533435a..82fe424 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -8392,7 +8392,9 @@  gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
       if (expr1->ts.deferred)
 	{
 	  if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
-	    gfc_add_modify (&block, lse.string_length, rse.string_length);
+	    gfc_add_modify (&block, lse.string_length,
+			    fold_convert (TREE_TYPE (lse.string_length),
+					  rse.string_length));
 	  else if (lse.string_length != NULL)
 	    gfc_add_modify (&block, lse.string_length,
 			    build_zero_cst (TREE_TYPE (lse.string_length)));
diff --git a/gcc/testsuite/gfortran.dg/char_pointer_assign_icb_1.f90 b/gcc/testsuite/gfortran.dg/char_pointer_assign_icb_1.f90
new file mode 100644
index 0000000..966757d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/char_pointer_assign_icb_1.f90
@@ -0,0 +1,13 @@ 
+! { dg-do compile }
+! Reduced testcase from PR 50892, regressed due to r256284 (PR 78534)
+subroutine test
+  use, intrinsic :: ISO_C_Binding, only: c_ptr
+  type(c_ptr) :: text
+  character(len=:), pointer :: ftext
+  ftext => FortranChar(text)
+contains
+  function FortranChar ( C )
+    type(c_ptr), intent(in), value :: C
+    character(len=10), pointer :: FortranChar
+  end function FortranChar
+end subroutine test