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] PR28771 - gfortran accepts invalid variable definition


:ADDPATCH fortran:

This problem is due to an initializer setting the character length of an assumed character length variable. In consequence, the compiler fails to recognise that it is assumed length and does not throw an error if it is declared in the main program.

Fortunately, the block of code that sets the character length is aberrant; there is no case where an assumed length character is permitted to take an initializer, except for a parameter. Parameters take another route than add_init_expr_to_sym, so we are free to rmove this chunk of code to fix the problem. The testcase is that submitted by the reporter.

Regtested on FC5/Athlon - OK for trunk and 4.1?

Paul

2006-08-18 Paul Thomas <pault@gcc.gnu.org>

   PR fortran/28771
   * decl.c (add_init_expr_to_sym): Remove setting of charlen for
   an initializer of an assumed charlen variable.

2006-08-18 Paul Thomas <pault@gcc.gnu.org>

   PR fortran/28771
   * gfortran.dg/assumed_charlen_in_main.f90: New test.


Index: gcc/fortran/decl.c
===================================================================
*** gcc/fortran/decl.c	(revision 116226)
--- gcc/fortran/decl.c	(working copy)
*************** add_init_expr_to_sym (const char *name, 
*** 875,886 ****
  	      sym->ts.cl = gfc_get_charlen ();
  	      sym->ts.cl->next = gfc_current_ns->cl_list;
  	      gfc_current_ns->cl_list = sym->ts.cl;
- 
- 	      if (init->expr_type == EXPR_CONSTANT)
- 		sym->ts.cl->length =
- 			gfc_int_expr (init->value.character.length);
- 	      else if (init->expr_type == EXPR_ARRAY)
- 		sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
  	    }
  	  /* Update initializer character length according symbol.  */
  	  else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
--- 875,880 ----
Index: gcc/testsuite/gfortran.dg/assumed_charlen_in_main.f90
===================================================================
*** gcc/testsuite/gfortran.dg/assumed_charlen_in_main.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/assumed_charlen_in_main.f90	(revision 0)
***************
*** 0 ****
--- 1,13 ----
+ ! { dg-do compile }
+ ! Tests the fix for PR28771 in which an assumed character length variable with an initializer could
+ ! survive in the main program without causing an error.
+ !
+ ! Contributed by Martin Reinecke  <martin@mpa-garching.mpg.de>
+ !
+ program test
+   character(len=*), parameter :: foo = 'test'     ! Parameters must work.
+   character(len=4) :: bar = foo
+   character(len=*) :: foobar = 'This should fail' ! {  dg-error "must be a dummy" }
+   print *, bar
+ end
+ 

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