Bug 31229 - kind parameter in function declaration fails to find use-associated parameters
Summary: kind parameter in function declaration fails to find use-associated parameters
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: rejects-valid
: 32578 (view as bug list)
Depends on:
Blocks: 31392 32834
  Show dependency treegraph
 
Reported: 2007-03-16 19:15 UTC by Brooks Moses
Modified: 2007-10-02 07:22 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.1.3 4.2.0 4.3.0
Last reconfirmed: 2007-09-22 18:40:42


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brooks Moses 2007-03-16 19:15:17 UTC
Bil Kleb posted the following code on comp.lang.fortran on 2007/3/16; Richard Maine agreed that it is valid but pointed out that it's a consistent weak spot in many compilers.  I tested it on GFortran, and it does indeed fail here.

debian-gfortran:~/test> more kleb1.f90
  module kinds
   integer, parameter :: dp = selected_real_kind(15)
  end module kinds

  module test_undeclared_kind; contains
    REAL(DP) function declared_dp_before_defined()
      use kinds, only: dp
      declared_dp_before_defined = 1.0_dp
    end function
  end module

debian-gfortran:~/test> ~/bin-trunk/bin/gfortran kleb1.f90
kleb1.f90:6.9:

    REAL(DP) function declared_dp_before_defined()
        1
Error: Parameter 'dp' at (1) has not been declared or is a variable, which does not reduce to a constant expression
[...]

debian-gfortran:~/test> ~/bin-trunk/bin/gfortran --version
GNU Fortran (GCC) 4.3.0 20070316 (experimental)
Comment 1 Brooks Moses 2007-03-16 22:40:34 UTC
This is probably related to Bug #31154
Comment 2 Tobias Burnus 2007-05-26 23:24:15 UTC
The problem is the resolution happens too early. In decl.c the function is matched and immediately the kind parameter is resolved:

gfc_match_kind_spec (gfc_typespec *ts)

n = gfc_match_init_expr (&e); // "has not been declared or is a variable"
msg = gfc_extract_int (e, &ts->kind); // "Constant expression required"

For character lengths, it is easier as length is an expr and not as typespec's kind an enum.
Comment 3 kargl 2007-07-02 14:54:25 UTC
*** Bug 32578 has been marked as a duplicate of this bug. ***
Comment 5 Paul Thomas 2007-09-22 18:40:42 UTC
Since I made a start on it, I had better finish it!

Paul
Comment 6 Paul Thomas 2007-10-02 07:17:30 UTC
Subject: Bug 31229

Author: pault
Date: Tue Oct  2 07:17:01 2007
New Revision: 128948

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128948
Log:
2007-10-02  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/31154
	PR fortran/31229
	PR fortran/33334
	* decl.c : Declare gfc_function_kind_locs and
	gfc_function_type_locus.
	(gfc_match_kind_spec): Add second argument kind_expr_only.
	Store locus before trying to match the expression. If the
	current state corresponds to a function declaration and there
	is no match to the expression, read to the parenthesis, return
	kind = -1, dump the expression and return.
	(gfc_match_type_spec): Renamed from match_type_spec and all
	references changed.  If an interface or an external function,
	store the locus, set kind = -1 and return.  Otherwise, if kind
	is already = -1, use gfc_find_symbol to try to find a use
	associated or imported type.
	match.h : Prototype for gfc_match_type_spec.
	* parse.c (match_deferred_characteristics): New function.
	(parse_spec): If in a function, statement is USE or IMPORT
	or DERIVED_DECL and the function kind=-1, call
	match_deferred_characteristics.  If kind=-1 at the end of the
	specification expressions, this is an error.
	* parse.h : Declare external gfc_function_kind_locs and
	gfc_function_type_locus.

2007-10-02  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/31154
	PR fortran/31229
	PR fortran/33334
	* gfortran.dg/function_kinds_1.f90: New test.
	* gfortran.dg/function_kinds_2.f90: New test.
	* gfortran.dg/derived_function_interface_1.f90: Correct illegal
	use association into interfaces.


Added:
    trunk/gcc/testsuite/gfortran.dg/function_kinds_1.f90
    trunk/gcc/testsuite/gfortran.dg/function_kinds_2.f90
    trunk/gcc/testsuite/gfortran.dg/intent_out_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/fortran/match.h
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/parse.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/derived_function_interface_1.f90

Comment 7 Paul Thomas 2007-10-02 07:22:44 UTC
Fixed on trunk

Paul