Bug 45016 - Support pointer assignment with bound-spec; wrong bounds for pointer assignment
Summary: Support pointer assignment with bound-spec; wrong bounds for pointer assignment
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Daniel Kraft
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-21 10:12 UTC by Tobias Burnus
Modified: 2010-08-19 16:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-08-17 16:00:55


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2010-07-21 10:12:39 UTC
This PR is about two related topics:
a) Wrong code: Fortran 90+: The lower bound shall be the same as the one of the object on the RHS
b) F2003+ pointer assignment with bounds-spec

Related to PR 29785 (bounds remapping).

 * * *

a) WRONG CODE

The test case should print twice a lower bound of 2, but currently it prints "1" for the lower bound of pointer "p":

!-------------------------------
integer, target :: a(2:5,2:5)
integer, pointer :: p(:,:)
!p(-1:,-1:) => a
p => a
print *, 'a: ', lbound(a)
print *, 'p: ', lbound(p)
end
!-------------------------------


Fortran 90 has:

"R736 pointer-assignment-stmt is pointer-object => target"
"If the target is not a pointer, the
pointer assignment statement associates the pointer-object with the target. If the target is a pointer that is associated, the pointer-object is associated with the same object as the target."

Due to the bounds-spec (cf. below) Fortran 2003 is more explicit:

"[...] the extent of a dimension of data-pointer-object is the extent of the corresponding dimension of data-target. [...] the lower bound of each dimension is the result of the intrinsic function LBOUND (13.7.60) applied to the corresponding dimension of data-target. The upper bound of each dimension is one less than the sum of the lower bound and the extent."

b) BOUND SPEC

(cf. 1.7 in the F2003 intro at ftp://ftp.nag.co.uk/sc22wg5/N1601-N1650/N1648.pdf)

The following program should have a lower bound of "-1" for the pointer "p". (Works, e.g., with the Cray compiler.)

Older gfortrans, e.g. 4.3, accepted this and set the lower bound to 1. Current gfortran reports:
  Error: Pointer assignment to non-POINTER at (1)

!--------------------------------
integer, target :: a(2:5,2:5)
integer, pointer :: p(:,:)
p(-1:,-1:) => a
print *, 'a: ', lbound(a) ! OK, prints "2 2"
print *, 'p: ', lbound(p) ! WRONG: prints "1 1", expected: "-1 -1"
end
!--------------------------------

While Fortran 95 only has:
"R736 pointer-assignment-stmt  is  pointer-object => target"

Fortran 2003 (and F2008 as R733) have:
"R735 pointer-assignment-stmt is data-pointer-object [ (bounds-spec-list) ] => data-target
                              or data-pointer-object (bounds-remapping-list ) => data-target
                              or proc-pointer-object => proc-target"

Further Fortran 2008 quotes (mostly identical to F2003, cf. also item (a)):

C716 (R733) If bounds-spec-list is specified, the number of bounds-specs shall equal the rank of data-pointer-object.

C718 (R733) If bounds-remapping-list is not specified, the ranks of data-pointer-object and data-target shall be the same.

R735 bounds-spec   is   lower-bound-expr :

[Remark: Note that only a lower bound can be specified; no upper or stride]

"If no bounds-remapping-list is specified, the extent of a dimension of the pointer object is the extent of the corresponding dimension of the pointer target. If bounds-spec-list appears, it specifies the lower bounds; otherwise, the lower bound of each dimension is the result of the intrinsic function LBOUND (13.7.90) applied to the corresponding dimension of the pointer target. The upper bound of each dimension is one less than the sum of
the lower bound and the extent."

Expected: The bounds are correctly set. Additionally, the bounds are rejected with -std=f95,
Comment 1 Tobias Burnus 2010-07-21 10:18:00 UTC
Mark as regression; for "p => a" GCC 4.3.2 prints the correct bounds ("2 2"). I currently cannot try 4.4 or 4.5.
Comment 2 Tobias Burnus 2010-07-21 10:26:20 UTC
Scratch the regression thing. (a) Works also with 4.6. I accidentally was running 4.1.2 (system compiler, "gfortran") instead of 4.6.0 ("gfortran46"). [On my usual system "gfortran" = 4.6.]

However, (b) is still true.  Sorry for the confusion!
Comment 3 Daniel Kraft 2010-08-17 16:00:55 UTC
Also working on this, as suggested by Tobias in PR 29785 maybe this can be done together.
Comment 4 Daniel Kraft 2010-08-19 16:02:50 UTC
Subject: Bug 45016

Author: domob
Date: Thu Aug 19 16:02:30 2010
New Revision: 163377

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163377
Log:
2010-08-19  Daniel Kraft  <d@domob.eu>

	PR fortran/29785
	PR fortran/45016
	* trans.h (struct gfc_se): New flag `byref_noassign'.
	* trans-array.h (gfc_conv_shift_descriptor_lbound): New method.
	(gfc_conv_array_extent_dim), (gfc_conv_descriptor_size): New methods.
	* expr.c (gfc_check_pointer_assign): Allow bounds and rank remapping
	and check for compile-time errors with those.
	* trans-decl.c (trans_associate_var): Use new routine
	`gfc_conv_shift_descriptor_lbound' instead of doing it manually.
	* trans-array.c (gfc_conv_shift_descriptor_lbound): New method.
	(gfc_conv_array_extent_dim), (gfc_conv_descriptor_size): New methods.
	(gfc_array_init_size): Use new `gfc_conv_array_extent_dim'.
	(gfc_conv_expr_descriptor): Handle new flag `byref_noassign'.
	* trans-expr.c (gfc_trans_pointer_assignment): Handle bounds and
	rank remapping for assignment.

2010-08-19  Daniel Kraft  <d@domob.eu>

	PR fortran/29785
	PR fortran/45016
	* gfortran.dg/pointer_assign_5.f90: Remove 'not implemented' error.
	* gfortran.dg/pointer_remapping_1.f90: New test.
	* gfortran.dg/pointer_remapping_2.f03: New test.
	* gfortran.dg/pointer_remapping_3.f08: New test.
	* gfortran.dg/pointer_remapping_4.f03: New test.
	* gfortran.dg/pointer_remapping_5.f08: New test.
	* gfortran.dg/pointer_remapping_6.f08: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_1.f90
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_2.f03
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_3.f08
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_4.f03
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_5.f08
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_6.f08
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-array.h
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/pointer_assign_5.f90

Comment 5 Daniel Kraft 2010-08-19 16:07:11 UTC
Fixed the lower bounds remapping, and the F95 issue claimed was already working.  Thus closing.