Bug 34429

Summary: Fails: character(len=use_associated_const) function foo()
Product: gcc Reporter: Tobias Burnus <burnus>
Component: fortranAssignee: Paul Thomas <pault>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs
Priority: P3 Keywords: rejects-valid
Version: 4.3.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2008-01-08 12:57:15
Bug Depends on:    
Bug Blocks: 32834    

Description Tobias Burnus 2007-12-11 09:53:19 UTC
I believe the following program is valid; it is compiled by g95, ifort, NAG f95, but gfortran rejects it with:

  Error: Expression at (1) must be of INTEGER type

For the for KIND we had a similar problem, see
        PR fortran/31154
        PR fortran/31229
        PR fortran/33334
and     PR 34254

module m
  integer, parameter :: strlen = 5
end module m

character(strlen) function test()
  use m
  test = 'A'
end function test
Comment 1 Paul Thomas 2008-01-08 12:57:15 UTC
This is fixed in a patch that I am working on that sorts out the whole business of function characteristics in one go.

Paul
Comment 2 Paul Thomas 2008-01-17 07:19:52 UTC
Subject: Bug 34429

Author: pault
Date: Thu Jan 17 07:19:04 2008
New Revision: 131592

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131592
Log:
2008-01-17  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34429
	PR fortran/34431
	PR fortran/34471
	* decl.c : Remove gfc_function_kind_locus and
	gfc_function_type_locus. Add gfc_matching_function.
	(match_char_length): If matching a function and the length
	does not match, return MATCH_YES and try again later.
	(gfc_match_kind_spec): The same.
	(match_char_kind): The same.
	(gfc_match_type_spec): The same for numeric and derived types.
	(match_prefix): Rename as gfc_match_prefix.
	(gfc_match_function_decl): Except for function valued character
	lengths, defer applying kind, type and charlen info until the
	end of specification block.
	gfortran.h (gfc_statement): Add ST_GET_FCN_CHARACTERISTICS.
	parse.c (decode_specification_statement): New function.
	(decode_statement): Call it when a function has kind = -1. Set
	and reset gfc_matching function, as function statement is being
	matched.
	(match_deferred_characteristics): Simplify with a single call
	to gfc_match_prefix. Do appropriate error handling. In any
	case, make sure that kind = -1 is reset or corrected.
	(parse_spec): Call above on seeing ST_GET_FCN_CHARACTERISTICS.
	Throw an error if kind = -1 after last specification statement.
	parse.h : Prototype for gfc_match_prefix.

2008-01-17  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34429
	* gfortran.dg/function_charlen_1.f90: New test.

	PR fortran/34431
	* gfortran.dg/function_types_1.f90: New test.
	* gfortran.dg/function_types_2.f90: New test.

	PR fortran/34471
	* gfortran.dg/function_kinds_4.f90: New test.
	* gfortran.dg/function_kinds_5.f90: New test.

	* gfortran.dg/defined_operators_1.f90: Errors now at function
	declarations.
	* gfortran.dg/private_type_4.f90: The same.
	* gfortran.dg/interface_15.f90: The same.
	* gfortran.dg/elemental_args_check_2.f90: The same.
	* gfortran.dg/auto_internal_assumed.f90: The same.


Added:
    trunk/gcc/testsuite/gfortran.dg/function_charlen_1.f90
    trunk/gcc/testsuite/gfortran.dg/function_kinds_4.f90
    trunk/gcc/testsuite/gfortran.dg/function_kinds_5.f90
    trunk/gcc/testsuite/gfortran.dg/function_types_1.f90
    trunk/gcc/testsuite/gfortran.dg/function_types_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/misc.c
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/parse.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/auto_internal_assumed.f90
    trunk/gcc/testsuite/gfortran.dg/defined_operators_1.f90
    trunk/gcc/testsuite/gfortran.dg/elemental_args_check_2.f90
    trunk/gcc/testsuite/gfortran.dg/interface_15.f90
    trunk/gcc/testsuite/gfortran.dg/private_type_4.f90

Comment 3 Paul Thomas 2008-01-17 10:12:21 UTC
Fixed on trunk - see below for some remaining wrinkles, dug up by Tobias.  I will keep this pR open until both are fixed.

Paul

wrinkle #1:  This one is easily fixed.

Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c  (révision 131592)
+++ gcc/fortran/decl.c  (copie de travail)
@@ -2156,6 +2156,7 @@
      after the specification statements.  */
   if (gfc_matching_function
        && !(len && len->expr_type != EXPR_VARIABLE
+                && len->expr_type != EXPR_CONSTANT
                 && len->expr_type != EXPR_OP))
     {
       gfc_undo_symbols ();

module m
 integer, parameter :: l = 2
end module m

program test
 implicit none
 integer, parameter :: l = 5
 character(len=10) :: c
 c = f()
contains
 character(len=l) function f()
   use m
   print *, len(f)
! if (len(f) /= 2) call abort  ! gives 5 right now...
   f = "a"
 end function f
end program test
! { dg-final { cleanup-modules "m" } }

wrinkle #2:  This one is proving to be a bit recalcitrant.

module m
 character(2) :: l
end module m

 implicit none
!  character(4) :: l
 print *, f()
contains
 character(len=len(l)) function f()
   use m
   f = "4"
   if (len(f)==2) f= "2"
 end function f
end

where 'l' is found to be ambiguous; this must arise because the first match of the length is leaving the symbol intact.
Comment 4 Tobias Burnus 2008-01-17 17:26:16 UTC
For cross reference the commit patch was:
http://gcc.gnu.org/ml/fortran/2008-01/msg00197.html

Besides the previously mentioned problems, there is also the following:

 gfc_clear_ts (gfc_typespec *ts)
[...]
+  if (ts->kind != -1)
+    ts->kind = 0;

This causes a problem if *ts is uninitalized, which happens for instance in intrinsic.c's add_conv:

  gfc_typespec from, to;
[...]
  gfc_clear_ts (&from);

Assume now that the uninitialized memory contains "-1" ...
(Found using valgrind which has dozens of warnings of the type "jump depends on uninitialized memory".)
Comment 5 Dominique d'Humieres 2008-01-17 17:49:39 UTC
From comment #4:

+  if (ts->kind != -1)
+    ts->kind = 0;

I think this has been removed in the commit. However if you had it, it is not removed by svn update
(I had to remove it manually).

Comment 6 Tobias Burnus 2008-01-17 18:07:41 UTC
> +  if (ts->kind != -1)
> I think this has been removed in the commit.
Indeed. Thanks for the check, Dominique. Thus: Ignore that comment.

Comment 7 Paul Thomas 2008-01-17 22:09:11 UTC
(In reply to comment #6)
> > +  if (ts->kind != -1)
> > I think this has been removed in the commit.
> Indeed. Thanks for the check, Dominique. Thus: Ignore that comment.

Gents,

I tried to deal with the useful comment about branching on kind = -1.  I have used the somewhat brutal device that you will now find in the tree.

Cheers

Paul 

Comment 8 Paul Thomas 2008-01-30 06:56:57 UTC
Subject: Bug 34429

Author: pault
Date: Wed Jan 30 06:56:10 2008
New Revision: 131956

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131956
Log:
2008-01-30  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34975
	* symbol.c (gfc_delete_symtree, gfc_undo_symbols): Rename
	delete_symtree to gfc_delete_symtree.
	* gfortran.h : Add prototype for gfc_delete_symtree.
	* module.c (load_generic_interfaces): Transfer symbol to a
	unique symtree and delete old symtree, instead of renaming.
	(read_module): The rsym and the found symbol are the same, so
	the found symtree can be deleted.

	PR fortran/34429
	* decl.c (match_char_spec): Remove the constraint on deferred
	matching of functions and free the length expression.
	delete_symtree to gfc_delete_symtree.
	(gfc_match_type_spec): Whitespace.
	(gfc_match_function_decl): Defer characteristic association for
	all types except BT_UNKNOWN.
	* parse.c (decode_specification_statement): Only derived type
	function matching is delayed to the end of specification.

2008-01-30  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34975
	* gfortran.dg/use_only_3.f90: New test.
	* gfortran.dg/use_only_3.inc: Modules for new test.

	PR fortran/34429
	* gfortran.dg/function_charlen_2.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/function_charlen_2.f90
    trunk/gcc/testsuite/gfortran.dg/use_only_3.f90
    trunk/gcc/testsuite/gfortran.dg/use_only_3.inc
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/module.c
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/testsuite/ChangeLog

Comment 9 Paul Thomas 2008-01-30 07:03:16 UTC
Fixed on trunk

Paul