2010-07-28  Tobias Burnus  <burnus@net-b.de>

	PR fortran/45087
	* trans-decl.c (gfc_get_extern_function_decl): Correctly handle
	external procedure declarations in modules.

2010-07-28  Tobias Burnus  <burnus@net-b.de>

	PR fortran/45087
	* gfortran.dg/whole_file_25.f90: New.
	* gfortran.dg/whole_file_26.f90: New.

Index: gcc/fortran/trans-decl.c
===================================================================
--- gcc/fortran/trans-decl.c	(Revision 162653)
+++ gcc/fortran/trans-decl.c	(Arbeitskopie)
@@ -1409,7 +1409,7 @@ gfc_get_extern_function_decl (gfc_symbol
   gsym =  gfc_find_gsymbol (gfc_gsym_root, sym->name);
 
   if (gfc_option.flag_whole_file
-	&& !sym->attr.use_assoc
+	&& (!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL)
 	&& !sym->backend_decl
 	&& gsym && gsym->ns
 	&& ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
@@ -1450,12 +1450,17 @@ gfc_get_extern_function_decl (gfc_symbol
 	    }
 	}
       else
-	{
-	  sym->backend_decl = gsym->ns->proc_name->backend_decl;
-	}
+	sym->backend_decl = gsym->ns->proc_name->backend_decl;
 
       if (sym->backend_decl)
-	return sym->backend_decl;
+	{
+	  /* Avoid problems of double deallocation of the backend declaration
+	     later in gfc_trans_use_stmts; cf. PR 45087.  */
+	  if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc)
+	    sym->attr.use_assoc = 0;
+
+	  return sym->backend_decl;
+	}
     }
 
   /* See if this is a module procedure from the same file.  If so,
Index: gcc/testsuite/gfortran.dg/whole_file_26.f90
===================================================================
--- gcc/testsuite/gfortran.dg/whole_file_26.f90	(Revision 0)
+++ gcc/testsuite/gfortran.dg/whole_file_26.f90	(Revision 0)
@@ -0,0 +1,26 @@
+! { dg-do compile }
+! { dg-options "-fwhole-program  --param ggc-min-expand=0 --param ggc-min-heapsize=0" }
+!
+! PR fortran/45087
+!
+
+module INTS
+  interface
+    subroutine NEXT
+    end subroutine NEXT
+    subroutine VALUE()
+    end subroutine VALUE
+  end interface
+end module INTS
+
+subroutine NEXT
+end subroutine NEXT
+
+subroutine VALUE()
+  use INTS, only: NEXT
+  CALL NEXT
+end subroutine VALUE
+
+end
+
+! { dg-final { cleanup-modules "ints" } }
Index: gcc/testsuite/gfortran.dg/whole_file_25.f90
===================================================================
--- gcc/testsuite/gfortran.dg/whole_file_25.f90	(Revision 0)
+++ gcc/testsuite/gfortran.dg/whole_file_25.f90	(Revision 0)
@@ -0,0 +1,21 @@
+! { dg-do compile }
+! { dg-options "-fwhole-program" }
+!
+! PR fortran/45087
+!
+
+module ints
+   INTERFACE
+      SUBROUTINE NOZZLE()
+      END SUBROUTINE NOZZLE
+   END INTERFACE
+end module ints
+
+      SUBROUTINE NOZZLE()
+      END SUBROUTINE NOZZLE
+      program CORTESA 
+      USE INTS
+      CALL NOZZLE ()
+      END program CORTESA
+
+! { dg-final { cleanup-modules "ints" } }
