Bug 34788 - Error diagnostic issued passing array element to explicit shape dummy argument
Summary: Error diagnostic issued passing array element to explicit shape dummy argument
Status: RESOLVED DUPLICATE of bug 34796
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-15 07:45 UTC by Keith Refson
Modified: 2008-01-15 19:30 UTC (History)
3 users (show)

See Also:
Host: x86_64-unknown-linux-gnu
Target: x86_64-unknown-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Keith Refson 2008-01-15 07:45:37 UTC
Gfortran version 4.3.0 20080114 (experimental) [trunk revision 131520]
issues a diagnostic

   call b(z(1))
         1
Error: Element of assumed-shaped array passed to dummy argument 'x' at (1)

on the attached program.  This diagnostic is (a) misleading - b is NOT
an assumed-shape array, and (b) incorrect.  I believe the code is
standard conforming Fortran under the provision allowing an array
element to be passed to an array dummy argument.

Gfortran 4.3.0 20071126 (experimental) [trunk revision 130431] compiles
the code without any diagnostic, as do the NAG, g95, ifort, IBM XLF, pathscale,
pgi compilers.

module passtest
integer, dimension(:),allocatable,save :: z
integer, dimension(4) :: t
contains
 subroutine a
   call b(z(1))
   call b(t(1))
 end subroutine a
 subroutine b(x)
   integer, dimension(2) :: x
 end subroutine b
end module passtest
Comment 1 Tobias Burnus 2008-01-15 19:30:08 UTC
Confirmed. Your bugreport was there even before mine, but since my contains a link to the patch I mark yours as duplicate.

Thanks for finding the bug.

> This diagnostic is (a) misleading - b is NOT an assumed-shape array,
> and (b) incorrect.  I believe the code is standard conforming Fortran
> under the provision allowing an array element to be passed to an array
> dummy argument.

(a) is true: It is an array of deferred shape and thus the error message is indeed wrong. (Both from the message and from showing an error message at all.)

Otherwise the error message is correct: You may not pass an element of an assumed-shape array (or a pointer) to an assumed-shape dummy argument.

The reason is that there is no reason for such arrays to be contiguous in memory.

Assume for instance:

subroutine func(a)
  integer :: a(:)
  call func2(a(4))
contains
  subroutine func2(b)
    integer :: b(5) ! or "integer :: b(*)"
  end subroutine func2
end subroutine func

If one now calls "func" as "call func(array(1:100:20))", the array A is not contiguous in memory because of the strides.

Passing "A(4)" now to func2 poses a problem: b(2) is outside of the array "A" (though within array "array").

In order to prevent this problem, the standard does not allow to pass elements of assumed-shaped arrays or pointers as actual argument to array dummy arguments.

The bug is only that my patch accidentally also rejected arrays with deferred shape (as your example); since here the allocation is in the same subroutine as the call, the memory is contiguous and there is no problem.

(In principle, the array "A" could be copied into a temporary array with no strides, but this is not what the standard mandates here. If one calls "func2" as "call func2(a(11:15))" this is actually done so - if "A" has strides.)

*** This bug has been marked as a duplicate of 34796 ***