[committed] Fortran: catch invalid kind in character conversion [PR69636,PR103779]

Message ID trinity-be314e88-a322-42a7-ad50-5058cc1eb34b-1675974508480@3c-app-gmx-bap49
State Repeat Merge
Headers
Series [committed] Fortran: catch invalid kind in character conversion [PR69636,PR103779] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Harald Anlauf Feb. 9, 2023, 8:28 p.m. UTC
  Dear all,

the attached trivial and obvious patch replaces an internal error
on encountering an invalid kind in a character conversion by a
regular error recovery.

Pushed after regtesting on x86_64-pc-linux-gnu as
r13-5760-ga618b45ac41cf480f54c4fa4014aed6218931290

Thanks,
Harald
  

Patch

From a618b45ac41cf480f54c4fa4014aed6218931290 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Thu, 9 Feb 2023 21:16:14 +0100
Subject: [PATCH] Fortran: catch invalid kind in character conversion
 [PR69636,PR103779]

gcc/fortran/ChangeLog:

	PR fortran/69636
	PR fortran/103779
	* intrinsic.cc (gfc_convert_chartype): Recover on invalid character
	kind in conversion instead of generating an internal error.

gcc/testsuite/ChangeLog:

	PR fortran/69636
	PR fortran/103779
	* gfortran.dg/pr103779.f90: New test.

Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
---
 gcc/fortran/intrinsic.cc               |  3 ++-
 gcc/testsuite/gfortran.dg/pr103779.f90 | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr103779.f90

diff --git a/gcc/fortran/intrinsic.cc b/gcc/fortran/intrinsic.cc
index 68f481473b4..17ee999c3b9 100644
--- a/gcc/fortran/intrinsic.cc
+++ b/gcc/fortran/intrinsic.cc
@@ -5419,7 +5419,8 @@  gfc_convert_chartype (gfc_expr *expr, gfc_typespec *ts)
   gcc_assert (expr->ts.type == BT_CHARACTER && ts->type == BT_CHARACTER);

   sym = find_char_conv (&expr->ts, ts);
-  gcc_assert (sym);
+  if (sym == NULL)
+    return false;

   /* Insert a pre-resolved function call to the right function.  */
   old_where = expr->where;
diff --git a/gcc/testsuite/gfortran.dg/pr103779.f90 b/gcc/testsuite/gfortran.dg/pr103779.f90
new file mode 100644
index 00000000000..3d76c901e75
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr103779.f90
@@ -0,0 +1,18 @@ 
+! { dg-do compile }
+! PR fortran/69636
+! PR fortran/103779
+! Contributed by G.Steinmetz
+
+character(1,) function f() ! { dg-error "Expected initialization expression" }
+  f = 'a'
+end
+
+character(1,kind=) function g() ! { dg-error "Expected initialization expression" }
+  g = 'a'
+end
+
+character(1,n) function h() ! { dg-error "has not been declared" }
+  h = 'a'
+end
+
+! { dg-prune-output "Bad kind expression" }
--
2.35.3