Bug 42309 - Problem with a pointer array passed to a subroutine
Summary: Problem with a pointer array passed to a subroutine
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: 4.4.4
Assignee: Paul Thomas
URL:
Keywords: wrong-code
Depends on:
Blocks: 42361
  Show dependency treegraph
 
Reported: 2009-12-06 13:57 UTC by Gilbert Scott
Modified: 2010-02-10 15:14 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.2 4.2.1
Known to fail: 4.4.2 4.5.0 4.3.4
Last reconfirmed: 2010-01-31 20:40:04


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gilbert Scott 2009-12-06 13:57:45 UTC
$ uname -a
Linux localhost.localdomain 2.6.29.4-167.fc11.i686.PAE #1 SMP Wed May 27 17:28:22 EDT 2009 i686 i686 i386 GNU/Linux
$ /usr/local/gfortran/bin/gfortran --version
GNU Fortran (GCC) 4.5.0 20091205 (experimental) [trunk revision 155016]
$ cat x.f95
PROGRAM X
TYPE T
   INTEGER :: I
END TYPE T
TYPE(T), TARGET :: T1(3)
INTEGER, POINTER :: P(:)

T1(1)%I = 1
T1(2)%I = 2
T1(3)%I = 3
P => T1%I
CALL Z(P)
PRINT *, T1%I

CONTAINS

SUBROUTINE Z(Q)
INTEGER, POINTER :: Q(:)
Q(1) = 999
END SUBROUTINE Z

END PROGRAM X

$ /usr/local/gfortran/bin/gfortran -o x x.f95
$ ./x
           1         999           3

!! The output is wrong.
I am expecting this output:
         999           2           3
Comment 1 Jerry DeLisle 2009-12-06 15:00:54 UTC
using Q(0) puts the 999 in the first position.  We have an off by one error here.
Comment 2 Paul Thomas 2010-01-31 20:40:04 UTC
I think that I know how to fix this one, so am assigning myself.  I would regard this as a "serious" bug.

Paul
Comment 3 Paul Thomas 2010-02-02 05:17:29 UTC
I am not at all happy with designating this bug a regression.  This worked with 4.1 and 4.2 because the stride measure for the derived type was the same as an integer; add more components and see what happens....

I am at that stage, where I can fix the testcase but other things break :-)

Watch this space.

This is another bug, where the array descriptor reform will afford a permanent fix.  If I do not see a quick fix soon, I will concentrate on array descriptors.

Paul
Comment 4 Tobias Burnus 2010-02-03 16:21:26 UTC
Paul's patch: http://gcc.gnu.org/ml/fortran/2010-02/msg00019.html
Comment 5 Paul Thomas 2010-02-05 05:28:51 UTC
Subject: Bug 42309

Author: pault
Date: Fri Feb  5 05:28:37 2010
New Revision: 156512

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

	PR fortran/42309
	* trans-expr.c (gfc_conv_subref_array_arg): Add new argument
	'formal_ptr'. If this is true, give returned descriptor unity
	lbounds, in all dimensions, and the appropriate offset.
	(gfc_conv_procedure_call); If formal is a pointer, set the last
	argument of gfc_conv_subref_array_arg to true.
	* trans.h : Add last argument for gfc_conv_subref_array_arg.
	* trans-io.c (set_internal_unit, gfc_trans_transfer): Set the
	new arg of gfc_conv_subref_array_arg to false.
	* trans-stmt.c (forall_make_variable_temp): The same.

2010-02-05  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/42309
	* gfortran.dg/subref_array_pointer_4.f90 : New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/subref_array_pointer_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-io.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 6 Paul Thomas 2010-02-06 19:44:52 UTC
Subject: Bug 42309

Author: pault
Date: Sat Feb  6 19:44:41 2010
New Revision: 156554

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

	PR fortran/42309
	* trans-expr.c (gfc_conv_subref_array_arg): Add new argument
	'formal_ptr'. If this is true, give returned descriptor unity
	lbounds, in all dimensions, and the appropriate offset.
	(gfc_conv_procedure_call); If formal is a pointer, set the last
	argument of gfc_conv_subref_array_arg to true.
	* trans.h : Add last argument for gfc_conv_subref_array_arg.
	* trans-io.c (set_internal_unit, gfc_trans_transfer): Set the
	new arg of gfc_conv_subref_array_arg to false.
	* trans-stmt.c (forall_make_variable_temp): The same.

2010-02-06  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/42309
	* gfortran.dg/subref_array_pointer_4.f90 : New test.


Added:
    branches/gcc-4_4-branch/gcc/testsuite/gfortran.dg/subref_array_pointer_4.f90
Modified:
    branches/gcc-4_4-branch/gcc/fortran/ChangeLog
    branches/gcc-4_4-branch/gcc/fortran/trans-expr.c
    branches/gcc-4_4-branch/gcc/fortran/trans-io.c
    branches/gcc-4_4-branch/gcc/fortran/trans-stmt.c
    branches/gcc-4_4-branch/gcc/fortran/trans.h
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog

Comment 7 Paul Thomas 2010-02-06 19:45:41 UTC
Fixed on trunk and 4.4.

Many thanks for the report!

Paul
Comment 8 H.J. Lu 2010-02-07 00:31:48 UTC
On Linux/x86-64, gcc 4.4 at revision 156554 gave

Executing on host: /export/gnu/import/svn/gcc-test/bld/gcc/testsuite/gfortran/../../gfortran -B/export/gnu/import/svn/gcc-test/bld/gcc/testsuite/gfortran/../../ /export/gnu/import/svn/gcc-test/src-4.4/gcc/testsuite/gfortran.dg/subref_array_pointer_2.f90   -O3 -g   -pedantic-errors  -B/export/gnu/import/svn/gcc-test/bld/x86_64-unknown-linux-gnu/32/libgfortran/.libs -L/export/gnu/import/svn/gcc-test/bld/x86_64-unknown-linux-gnu/32/libgfortran/.libs -L/export/gnu/import/svn/gcc-test/bld/x86_64-unknown-linux-gnu/32/libgfortran/.libs -L/export/gnu/import/svn/gcc-test/bld/x86_64-unknown-linux-gnu/32/libiberty  -lm   -m32 -o ./subref_array_pointer_2.exe    (timeout = 300)
/export/gnu/import/svn/gcc-test/src-4.4/gcc/testsuite/gfortran.dg/subref_array_pointer_2.f90: In function 'MAIN__':^M
/export/gnu/import/svn/gcc-test/src-4.4/gcc/testsuite/gfortran.dg/subref_array_pointer_2.f90:49: internal compiler error: in fold_binary, at fold-const.c:9704^M
Please submit a full bug report,^M
with preprocessed source if appropriate.^M
See <http://gcc.gnu.org/bugs.html> for instructions.^M
Comment 9 Tobias Burnus 2010-02-08 14:47:06 UTC
Regarding 4.4: It fails here (x86-64) for any option for:

  IF (ANY (T1%I .NE. [999, 2, 999, 4])) CALL ABORT

as the result is:    [1, 999, 3, 999].  (The other check succeeds.)
Comment 10 Tobias Burnus 2010-02-10 13:54:48 UTC
Patch for 4.4 and the 4.5-trunk by Jakub:
  http://gcc.gnu.org/ml/fortran/2010-02/msg00063.html
Comment 11 Jakub Jelinek 2010-02-10 15:11:04 UTC
Subject: Bug 42309

Author: jakub
Date: Wed Feb 10 15:10:53 2010
New Revision: 156659

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=156659
Log:
	PR fortran/42309
	* trans-expr.c (gfc_conv_subref_array_arg): Avoid accessing
	info->dimen after info has been freed.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c

Comment 12 Jakub Jelinek 2010-02-10 15:11:43 UTC
Subject: Bug 42309

Author: jakub
Date: Wed Feb 10 15:11:30 2010
New Revision: 156660

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=156660
Log:
	PR fortran/42309
	* trans-expr.c (gfc_conv_subref_array_arg): Avoid accessing
	info->dimen after info has been freed.

Modified:
    branches/gcc-4_4-branch/gcc/fortran/ChangeLog
    branches/gcc-4_4-branch/gcc/fortran/trans-expr.c

Comment 13 Jakub Jelinek 2010-02-10 15:14:48 UTC
Fixed.