diff mbox series

[Committed] A target in pointer initialization requires SAVE

Message ID 20190620222052.GA49955@troutmask.apl.washington.edu
State New
Headers show
Series [Committed] A target in pointer initialization requires SAVE | expand

Commit Message

Steve Kargl June 20, 2019, 10:20 p.m. UTC
In taking a look at PR fortran/77632, I stumbled acrossed
the testcase in the attached patch cause an ICE.  I originally
thought that it was realated to the topic of the PR, but is
in fact an unrelated bug.

If a variable is a target in a pointer initialization, then 
it must have the SAVE attribute.  A variable in PROGRAM, 
MODULE, or SUBMODULE scope is implicitly SAVEd.  So, the
patch explicitly sets the save attribute to SAVE_IMPLICIT.

2019-06-20  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/77632
	* /decl.c (variable_decl): Mark a variable that is a target in pointer
	initialization when in PROGRAM, MODULE, or SUBMODULE scope with an
	implicit save.

2019-06-20  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/77632
	* gfortran.dg/pr77632_1.f90: New test.
diff mbox series

Patch

Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 272523)
+++ gcc/fortran/decl.c	(working copy)
@@ -2779,6 +2779,16 @@  variable_decl (int elem)
 	  m = match_pointer_init (&initializer, 0);
 	  if (m != MATCH_YES)
 	    goto cleanup;
+
+	  /* The target of a pointer initialization must have the SAVE
+	     attribute.  A variable in PROGRAM, MODULE, or SUBMODULE scope
+	     is implicit SAVEd.  Explicitly, set the SAVE_IMPLICIT value.  */
+	  if (initializer->expr_type == EXPR_VARIABLE
+	      && initializer->symtree->n.sym->attr.save == SAVE_NONE
+	      && (gfc_current_state () == COMP_PROGRAM
+		  || gfc_current_state () == COMP_MODULE
+		  || gfc_current_state () == COMP_SUBMODULE))
+	    initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT;
 	}
       else if (gfc_match_char ('=') == MATCH_YES)
 	{
Index: gcc/testsuite/gfortran.dg/pr77632_1.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr77632_1.f90	(nonexistent)
+++ gcc/testsuite/gfortran.dg/pr77632_1.f90	(working copy)
@@ -0,0 +1,7 @@ 
+! { dg-do run }
+program foo
+   implicit none
+   real, target :: a
+   real, pointer :: b => a
+   if (associated(b, a) .eqv. .false.) stop 1
+end program foo