PING: [Patch, fortran] PR fortran/96724 - Bogus warnings with the repeat intrinsic and the flag -Wconversion-extra

Message ID bbeeb639-9bc7-ea65-c8a8-0531d9dff591@gmail.com
State New
Headers show
Series
  • PING: [Patch, fortran] PR fortran/96724 - Bogus warnings with the repeat intrinsic and the flag -Wconversion-extra
Related show

Commit Message

Michael Meissner via Gcc-patches June 16, 2021, 3:13 p.m.
*PING*


-------- Forwarded Message --------
Subject: [Patch, fortran] PR fortran/96724 - Bogus warnings with the 
repeat intrinsic and the flag -Wconversion-extra
Date: Thu, 20 Aug 2020 16:52:10 +0000
From: José Rui Faustino de Sousa <jrfsousa@gmail.com>

To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org

Hi all!

Proposed patch to PR96724 - Bogus warnings with the repeat intrinsic and 
the flag -Wconversion-extra.

Patch tested only on x86_64-pc-linux-gnu.

Add code to force conversion to the default wider integer type before 
multiplication.

Thank you very much.

Best regards,
José Rui


2020-8-20  José Rui Faustino de Sousa  <jrfsousa@gmail.com>

   PR fortran/96724
   * iresolve.c (gfc_resolve_repeat): Force conversion to
   gfc_index_integer_kind before the call to gfc_multiply.

2020-8-20  José Rui Faustino de Sousa  <jrfsousa@gmail.com>

   PR fortran/96724
   * repeat_8.f90.f90: New test.

Patch

diff --git a/gcc/fortran/iresolve.c b/gcc/fortran/iresolve.c
index 7376961..74075a7 100644
--- a/gcc/fortran/iresolve.c
+++ b/gcc/fortran/iresolve.c
@@ -2332,7 +2332,22 @@  gfc_resolve_repeat (gfc_expr *f, gfc_expr *string,
     }
 
   if (tmp)
-    f->ts.u.cl->length = gfc_multiply (tmp, gfc_copy_expr (ncopies));
+    {
+      gfc_expr *e = gfc_copy_expr (ncopies);
+
+      /* Force-convert to index_kind so that we don't need
+	 so many runtime variations.  */
+      if (e->ts.kind != gfc_index_integer_kind)
+	{
+	  gfc_typespec ts = e->ts;
+
+	  ts.kind = gfc_index_integer_kind;
+	  gfc_convert_type_warn (e, &ts, 2, 0);
+	}
+      if (tmp->ts.kind != gfc_index_integer_kind)
+	gfc_convert_type_warn (tmp, &e->ts, 2, 0);
+      f->ts.u.cl->length = gfc_multiply (tmp, e);
+    }
 }
 
 
diff --git a/gcc/testsuite/gfortran.dg/repeat_8.f90 b/gcc/testsuite/gfortran.dg/repeat_8.f90
new file mode 100644
index 0000000..6876af9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/repeat_8.f90
@@ -0,0 +1,88 @@ 
+! { dg-do compile }
+! { dg-additional-options "-Wconversion-extra" }
+!
+! Test fix for PR96724
+!
+
+program repeat_p
+
+  use, intrinsic :: iso_fortran_env, only: &
+    int8, int16, int32, int64
+  
+  implicit none
+
+  integer, parameter :: n = 20
+
+  integer(kind=int8),  parameter :: p08 = int(n, kind=int8)
+  integer(kind=int16), parameter :: p16 = int(n, kind=int16)
+  integer(kind=int16), parameter :: p32 = int(n, kind=int32)
+  integer(kind=int16), parameter :: p64 = int(n, kind=int64)
+  
+  integer(kind=int8)  :: i08
+  integer(kind=int16) :: i16
+  integer(kind=int32) :: i32
+  integer(kind=int64) :: i64
+  
+  character(len=n) :: c
+
+  i08 = p08
+  c = repeat('X', 20_int8)
+  c = repeat('X', i08)
+  c = repeat('X', p08)
+  c = repeat('X', len08(c))
+  i16 = p16
+  c = repeat('X', 20_int16)
+  c = repeat('X', i16)
+  c = repeat('X', p16)
+  c = repeat('X', len16(c))
+  i32 = p32
+  c = repeat('X', 20_int32)
+  c = repeat('X', i32)
+  c = repeat('X', p32)
+  c = repeat('X', len32(c))
+  i64 = p64
+  c = repeat('X', 20_int64)
+  c = repeat('X', i64)
+  c = repeat('X', p64)
+  c = repeat('X', len64(c))
+  stop
+
+contains
+
+  function len08(x) result(l)
+    character(len=*), intent(in) :: x
+
+    integer(kind=int8) :: l
+
+    l = int(len(x), kind=int8)
+    return
+  end function len08
+  
+  function len16(x) result(l)
+    character(len=*), intent(in) :: x
+
+    integer(kind=int16) :: l
+
+    l = int(len(x), kind=int16)
+    return
+  end function len16
+  
+  function len32(x) result(l)
+    character(len=*), intent(in) :: x
+
+    integer(kind=int32) :: l
+
+    l = int(len(x), kind=int32)
+    return
+  end function len32
+  
+  function len64(x) result(l)
+    character(len=*), intent(in) :: x
+
+    integer(kind=int64) :: l
+
+    l = int(len(x), kind=int64)
+    return
+  end function len64
+  
+end program repeat_p