diff mbox series

[v2] PR fortran/104573 - ICE in resolve_structure_cons, at fortran/resolve.cc:1299

Message ID 2b5ca3ac-d9d7-b6df-5b48-306a112c87be@gmx.de
State New
Headers show
Series [v2] PR fortran/104573 - ICE in resolve_structure_cons, at fortran/resolve.cc:1299 | expand

Commit Message

Harald Anlauf March 1, 2022, 10:18 p.m. UTC
Hi Mikael,

Am 28.02.22 um 22:38 schrieb Mikael Morin:
> Le 28/02/2022 à 22:32, Mikael Morin a écrit :
>> So please use a condition on expr->ts.type instead.
>> I said «instead», but «as well» is more appropriate; both expr.ts.type
> and expr.ts.u.derived conditions are probably necessary.
>

I do hope I got you right.  The attached patch fixes your variant
as well as the original testcase, and regtests fine.
Just to be sure: is this what you were thinking of?

Thanks for the very constructive review!

Harald

Comments

Mikael Morin March 2, 2022, 11:37 a.m. UTC | #1
Le 01/03/2022 à 23:18, Harald Anlauf via Fortran a écrit :
> 
> I do hope I got you right.  The attached patch fixes your variant
> as well as the original testcase, and regtests fine.
> Just to be sure: is this what you were thinking of?
> 
Indeed, that’s what I had in mind.
Nice, I didn’t expect that the requested change would be enough to fix 
my testcase, as there was a different ICE.

Thanks.
diff mbox series

Patch

From e4816e150c31e127c3b6dc0032ae5555533a2d42 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 1 Mar 2022 23:13:17 +0100
Subject: [PATCH] Fortran: error recovery after invalid assumed type
 declaration

gcc/fortran/ChangeLog:

	PR fortran/104573
	* resolve.cc (resolve_structure_cons): Avoid NULL pointer
	dereference when there is no valid component.

gcc/testsuite/ChangeLog:

	PR fortran/104573
	* gfortran.dg/assumed_type_14.f90: New test.
---
 gcc/fortran/resolve.cc                        | 10 ++++++---
 gcc/testsuite/gfortran.dg/assumed_type_14.f90 | 22 +++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/assumed_type_14.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 753aa27e23f..0afa5d3346a 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -1288,15 +1288,19 @@  resolve_structure_cons (gfc_expr *expr, int init)
 	}
     }
 
-  cons = gfc_constructor_first (expr->value.constructor);
-
   /* A constructor may have references if it is the result of substituting a
      parameter variable.  In this case we just pull out the component we
      want.  */
   if (expr->ref)
     comp = expr->ref->u.c.sym->components;
-  else
+  else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
+	    || expr->ts.type == BT_UNION)
+	   && expr->ts.u.derived)
     comp = expr->ts.u.derived->components;
+  else
+    return false;
+
+  cons = gfc_constructor_first (expr->value.constructor);
 
   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
     {
diff --git a/gcc/testsuite/gfortran.dg/assumed_type_14.f90 b/gcc/testsuite/gfortran.dg/assumed_type_14.f90
new file mode 100644
index 00000000000..112cde34b27
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/assumed_type_14.f90
@@ -0,0 +1,22 @@ 
+! { dg-do compile }
+! PR fortran/104573 - ICE in resolve_structure_cons
+! Contributed by G.Steinmetz
+! Contributed by M.Morin
+
+program p
+  type t
+  end type
+  type(*), parameter :: x = t() ! { dg-error "Assumed type of variable" }
+  print *, x
+end
+
+subroutine s
+  type t
+     integer :: a
+  end type
+  character(3), parameter :: x = t(2) ! { dg-error "Cannot convert" }
+  character(3), parameter :: y = x    ! { dg-error "Unclassifiable statement" }
+  print *, y
+end
+
+! { dg-prune-output "Cannot convert" }
-- 
2.34.1