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]

[Patch,Fortran] PR40851 Fix wrong default initialization of DT pointers


The following patch is close to being obvious. With the introduction of
pointer intents, the assumption that INTENT_OUT is not a pointer and
thus the default initialization can be/has to be applied is wrong.

Without the patch, gfortran first tries to deallocate "ptr" which fails
as the pointer is uninitialized. Afterwards, it tries to apply the
default initialization due to the allocatable component, which gives an
assert of the run-time linker.

Thanks to Jürgen Reuter for including both - otherwise, I might have
forgotten to change resolve.c as well.

Build and regtested on x86-64-linux. OK for the trunk and 4.4? Shall we
also backport it to 4.3?

Tobias
2009-07-26  Tobias Burnus  <burnus@net-b.de>

	PR fortran/40851
	* resolve.c (resolve_symbol): Do not initialize pointer derived-types.
	* trans-decl.c (init_intent_out_dt): Ditto.
	(generate_local_decl): No need to set attr.referenced for DT pointers.

2009-07-26  Tobias Burnus  <burnus@net-b.de>

	PR fortran/40851
	* gfortran.dg/derived_init_3.f90: New test.

Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 150086)
+++ gcc/fortran/resolve.c	(working copy)

@@ -10036,7 +10111,7 @@ resolve_symbol (gfc_symbol *sym)
       if ((!a->save && !a->dummy && !a->pointer
 	   && !a->in_common && !a->use_assoc
 	   && !(a->function && sym != sym->result))
-	  || (a->dummy && a->intent == INTENT_OUT))
+	  || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
 	apply_default_init (sym);
     }
 
Index: gcc/fortran/trans-decl.c
===================================================================
--- gcc/fortran/trans-decl.c	(revision 150086)
+++ gcc/fortran/trans-decl.c	(working copy)
@@ -2958,7 +3009,8 @@ init_intent_out_dt (gfc_symbol * proc_sy
   gfc_init_block (&fnblock);
   for (f = proc_sym->formal; f; f = f->next)
     if (f->sym && f->sym->attr.intent == INTENT_OUT
-	  && f->sym->ts.type == BT_DERIVED)
+	&& !f->sym->attr.pointer
+	&& f->sym->ts.type == BT_DERIVED)
       {
 	if (f->sym->ts.derived->attr.alloc_comp)
 	  {
@@ -3708,6 +3760,7 @@ generate_local_decl (gfc_symbol * sym)
       if (!sym->attr.referenced
 	    && sym->ts.type == BT_DERIVED
 	    && sym->ts.derived->attr.alloc_comp
+	    && !sym->attr.pointer
 	    && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
 		  ||
 		(sym->attr.result && sym != sym->result)))
Index: gcc/testsuite/gfortran.dg/derived_init_3.f90
===================================================================
--- gcc/testsuite/gfortran.dg/derived_init_3.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/derived_init_3.f90	(revision 0)
@@ -0,0 +1,34 @@
+! { dg-do run }
+!
+! PR fortran/40851
+!
+! Make sure the an INTENT(OUT) dummy is not initialized
+! when it is a pointer.
+!
+! Contributed by Juergen Reuter <juergen.reuter@desy.de>.
+!
+program main
+
+  type :: string
+     character,dimension(:),allocatable :: chars
+  end type string
+
+  type :: string_container
+     type(string) :: string
+  end type string_container
+
+  type(string_container), target :: tgt
+  type(string_container), pointer :: ptr
+
+  ptr => tgt
+  call set_ptr (ptr)
+  if (associated(ptr)) call abort()
+
+contains
+
+  subroutine set_ptr (ptr)
+    type(string_container), pointer, intent(out) :: ptr
+    ptr => null ()
+  end subroutine set_ptr
+
+end program main

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