From 18f212aaca8a13fbd2f40cc7636b1a20185cc01e Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Fri, 5 Jan 2024 22:24:25 +0100
Subject: [PATCH] Fortran: bogus warnings with REPEAT intrinsic and
 -Wconversion-extra [PR96724]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

gcc/fortran/ChangeLog:

	PR fortran/96724
	* iresolve.cc (gfc_resolve_repeat): Force conversion of NCOPIES to
	gfc_charlen_int_kind before call to gfc_multiply.

gcc/testsuite/ChangeLog:

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

Co-authored-by: José Rui Faustino de Sousa <jrfsousa@gmail.com>
---
 gcc/fortran/iresolve.cc                |   7 +-
 gcc/testsuite/gfortran.dg/repeat_8.f90 | 123 +++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/repeat_8.f90

diff --git a/gcc/fortran/iresolve.cc b/gcc/fortran/iresolve.cc
index 5bb0bbc477b..f25a5ba3aef 100644
--- a/gcc/fortran/iresolve.cc
+++ b/gcc/fortran/iresolve.cc
@@ -2363,7 +2363,12 @@ gfc_resolve_repeat (gfc_expr *f, gfc_expr *string,
     }
 
   if (tmp)
-    f->ts.u.cl->length = gfc_multiply (tmp, gfc_copy_expr (ncopies));
+    {
+      /* Force-convert ncopies to gfc_charlen_int_kind.  */
+      gfc_expr *e = gfc_copy_expr (ncopies);
+      gfc_convert_type_warn (e, &tmp->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 00000000000..9dd379ac96b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/repeat_8.f90
@@ -0,0 +1,123 @@
+! { dg-do compile }
+! { dg-additional-options "-Wconversion-extra" }
+!
+! Test fix for PR fortran/96724
+!
+! Contributed by José Rui Faustino de Sousa 
+
+program repeat_p
+  use, intrinsic :: iso_fortran_env, only: int8, int16, int32, int64
+  implicit none
+
+  integer, parameter :: n = 20
+  integer, parameter :: ucs4 = selected_char_kind ('ISO_10646')
+
+  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,kind=1)    :: c
+  character(len=n,kind=ucs4) :: d
+
+  i08 = p08
+  c = repeat('X', 20_int8)
+  c = repeat('X', i08)
+  c = repeat('X', p08)
+  c = repeat('X', len08(c))
+  d = repeat(ucs4_'X', 20_int8)
+  d = repeat(ucs4_'X', i08)
+  d = repeat(ucs4_'X', p08)
+  d = repeat(ucs4_'X', len08(c))
+  i16 = p16
+  c = repeat('X', 20_int16)
+  c = repeat('X', i16)
+  c = repeat('X', p16)
+  c = repeat('X', len16(c))
+  d = repeat(ucs4_'X', 20_int16)
+  d = repeat(ucs4_'X', i16)
+  d = repeat(ucs4_'X', p16)
+  d = repeat(ucs4_'X', len16(c))
+  i32 = p32
+  c = repeat('X', 20_int32)
+  c = repeat('X', i32)
+  c = repeat('X', p32)
+  c = repeat('X', len32(c))
+  d = repeat(ucs4_'X', 20_int32)
+  d = repeat(ucs4_'X', i32)
+  d = repeat(ucs4_'X', p32)
+  d = repeat(ucs4_'X', len32(c))
+  i64 = p64
+  c = repeat('X', 20_int64)
+  c = repeat('X', i64)
+  c = repeat('X', p64)
+  c = repeat('X', len64(c))
+  d = repeat(ucs4_'X', 20_int64)
+  d = repeat(ucs4_'X', i64)
+  d = repeat(ucs4_'X', p64)
+  d = repeat(ucs4_'X', len64(c))
+
+contains
+
+  function len08(x) result(l)
+    character(len=*), intent(in) :: x
+    integer(kind=int8) :: l
+
+    l = int(len(x), kind=int8)
+  end function len08
+
+  function len16(x) result(l)
+    character(len=*), intent(in) :: x
+    integer(kind=int16) :: l
+
+    l = int(len(x), kind=int16)
+  end function len16
+
+  function len32(x) result(l)
+    character(len=*), intent(in) :: x
+    integer(kind=int32) :: l
+
+    l = int(len(x), kind=int32)
+  end function len32
+
+  function len64(x) result(l)
+    character(len=*), intent(in) :: x
+    integer(kind=int64) :: l
+
+    l = int(len(x), kind=int64)
+  end function len64
+
+  function ulen08(x) result(l)
+    character(len=*,kind=ucs4), intent(in) :: x
+    integer(kind=int8) :: l
+
+    l = int(len(x), kind=int8)
+  end function ulen08
+
+  function ulen16(x) result(l)
+    character(len=*,kind=ucs4), intent(in) :: x
+    integer(kind=int16) :: l
+
+    l = int(len(x), kind=int16)
+  end function ulen16
+
+  function ulen32(x) result(l)
+    character(len=*,kind=ucs4), intent(in) :: x
+    integer(kind=int32) :: l
+
+    l = int(len(x), kind=int32)
+  end function ulen32
+
+  function ulen64(x) result(l)
+    character(len=*,kind=ucs4), intent(in) :: x
+    integer(kind=int64) :: l
+
+    l = int(len(x), kind=int64)
+  end function ulen64
+
+end program repeat_p
-- 
2.35.3

