This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Committed] PR fortran/88138 -- can't initialize a derived type


I've committed the attached patch after testing on i586-*-freebsd
and x86_64-*-freebsd.

2019-12-15  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/88138
	* decl.c (variable_decl): Check that a derived isn't being assigned
	an incompatible entity in an initialization.
 
2019-12-15  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/88138
	* gfortran.dg/pr88138.f90: new test.
-- 
Steve
Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 267173)
+++ gcc/fortran/decl.c	(working copy)
@@ -2784,6 +2784,22 @@ variable_decl (int elem)
 	param->value = gfc_copy_expr (initializer);
     }
 
+  /* Before adding a possible initilizer, do a simple check for compatibility
+     of lhs and rhs types.  Assigning a REAL value to a derive type is not a
+     good thing.  */
+  if (current_ts.type == BT_DERIVED && initializer
+      && (gfc_numeric_ts (&initializer->ts)
+	  || initializer->ts.type == BT_LOGICAL
+	  || initializer->ts.type == BT_CHARACTER))
+    {
+      gfc_error ("Incompatible initialization between a derive type "
+		 "entity and an entity with %qs type at %C",
+		  gfc_typename (&initializer->ts));
+      m = MATCH_ERROR;
+      goto cleanup;
+    }
+
+
   /* Add the initializer.  Note that it is fine if initializer is
      NULL here, because we sometimes also need to check if a
      declaration *must* have an initialization expression.  */
Index: gcc/testsuite/gfortran.dg/pr88138.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr88138.f90	(nonexistent)
+++ gcc/testsuite/gfortran.dg/pr88138.f90	(working copy)
@@ -0,0 +1,9 @@
+! { dg-do compile }
+program p
+   type t
+      character :: c = 'c'
+   end type
+   type(t), parameter :: x  = 1.e1  ! { dg-error "Incompatible initialization between a" }s
+   print *, 'a' // x%c
+end
+! { dg-prune-output "has no IMPLICIT type" }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]