Bug 33566 - fortran : wrong rank of derived type parameters array components
Summary: fortran : wrong rank of derived type parameters array components
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: wrong-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2007-09-26 18:00 UTC by Mikael Morin
Modified: 2007-10-02 08:46 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-09-28 15:48:17


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mikael Morin 2007-09-26 18:00:08 UTC
gfortran seems to miscalculate the rank when trying to get a component of an array member of a derived type parameter. 
I think that the following program explains better than my cryptic explanations :

      program test_vec

      implicit none


      integer :: i
      real    :: x

      type vec3
        real, dimension(3) :: coords
      end type vec3

      type(vec3),parameter :: v1 = vec3((/ 1.0, 2.0, 3.0 /))
      type(vec3)           :: v2

      v2 = vec3((/ 1.0, 2.0, 3.0 /))


      x = v1%coords(1)  ! works

      do i=1,3
        x = v1%coords(i)  ! fails
        x = v2%coords(i)  ! works
      end do

      end program test_vec


Here is the error returned ...
$ gfortran -c test.f
test.f:27.9:

        x = v1%coords(i)  ! fails                                       
        1
Error: Incompatible ranks 0 and 1 in assignment at (1)



... and the version of gfortran used :
$ gfortran -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-svn/gcc/configure --enable-languages=fortran : (reconfigured) ../gcc-svn/gcc/configure --enable-languages=fortran : (reconfigured) ../gcc-svn/gcc/configure --enable-languages=fortran
Thread model: posix
gcc version 4.3.0 20070926 (experimental) (GCC)
Comment 1 Paul Thomas 2007-09-28 15:48:17 UTC
It transpires that the fix that I used for use associated, derived type parameters works here too:

Index: gcc/fortran/primary.c
===================================================================
*** gcc/fortran/primary.c       (révision 128833)
--- gcc/fortran/primary.c       (copie de travail)
*************** gfc_match_rvalue (gfc_expr **result)
*** 2148,2159 ****
        if (sym->ts.is_c_interop || sym->ts.is_iso_c)
        break;

!       /* Variable array references to use associated derived type
!        parameters cause all sorts of headaches in simplification.
!        For this reason, we write the parameter to the module and
!        treat them as variable references.  */
!       if (sym->value && sym->ts.type == BT_DERIVED
!           && sym->attr.use_assoc && e->ref)
        {
          for (ref = e->ref; ref; ref = ref->next)
            if (ref->type == REF_ARRAY)
--- 2148,2157 ----
        if (sym->ts.is_c_interop || sym->ts.is_iso_c)
        break;

!       /* Variable array references to derived type parameters cause
!        all sorts of headaches in simplification.  For this reason,
!        we treat them as variable references.  */
!       if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
        {
          for (ref = e->ref; ref; ref = ref->next)
            if (ref->type == REF_ARRAY)

I'll regtest and submit it.

Cheers

Paul.
Comment 2 Paul Thomas 2007-09-29 08:48:11 UTC
(In reply to comment #1)

This causes one regression - data_components_1.f90. I either have to check if the reference is not constant or to try to simplify the expression and see if the result is constant.

Paul
Comment 3 Paul Thomas 2007-09-29 09:21:12 UTC
(In reply to comment #2)

I take that back - I had to copy the patch by hand and, inevitably, got it wrong.

I'll regtest over again and then submit it.

Je te remercie, Mikael!

Paul
Comment 4 patchapp@dberlin.org 2007-09-29 09:40:20 UTC
Subject: Bug number PR33566

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-09/msg02054.html
Comment 5 Paul Thomas 2007-10-02 08:12:23 UTC
Subject: Bug 33566

Author: pault
Date: Tue Oct  2 08:12:11 2007
New Revision: 128951

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

	PR fortran/33566
	* primary.c (gfc_match_rvalue): Make all expressions with array
	references to structure parameters into variable expressions.

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

	PR fortran/33566
	* gfortran.dg/derived_comp_array_ref_5.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/derived_comp_array_ref_5.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/primary.c
    trunk/gcc/testsuite/ChangeLog

Comment 6 Paul Thomas 2007-10-02 08:46:46 UTC
Fixed on trunk

Paul