[Bug fortran/51208] [OOP] ALLOCATE with SOURCE= or MOLD=: Diagnose if variable occurs twice
kargl at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Fri Nov 18 16:10:00 GMT 2011
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51208
kargl at gcc dot gnu.org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |kargl at gcc dot gnu.org
--- Comment #1 from kargl at gcc dot gnu.org 2011-11-18 15:53:51 UTC ---
(In reply to comment #0)
> In ALLOCATE, one gets a diagnosis if one uses:
>
> integer, allocatable :: i
> allocate(i, stat=i)
> end
>
> Namely:
> allocate(i, stat=i)
> 1
> Error: Stat-variable at (1) shall not be ALLOCATEd within
> the same ALLOCATE statement
The above error is easy to produce, because gfortran
only has to check if an alloc-object is used as a
stat-variable.
>
> However, for SOURCE= and MOLD= it does not work:
>
> type t
> integer :: i = 4
> end type t
> class(t), allocatable :: x, y
>
> allocate(t :: y)
> allocate(x, mold=x) ! Not diagnosed
> allocate(x, source=x) ! Not diagnosed
Doing a check here requires a walk of the mold-expr
and the source-expr, which is of course much more
work (unless gfortran already has a helper function
to check if a variable is used within an expression).
Here's a simple example where walking source-expr
may find the use of the alloc-object:
module bar
real, allocatable :: x(:)
contains
function f(a)
real f
real :: a(:)
f = sum(a)
end function
end module bar
program foo
use bar
allocate(x(2), source=f(x))
print *, x
end program foo
!
!program foo
! use bar
! allocate(x(2), source=f([1., 2.]))
! print *, x
!end program foo
and here's a more complicated example.
module bar
real, allocatable :: x(:)
contains
function f(a)
real f
real :: a
f = sum(a * x) ! x used un-allocated?
end function
end module bar
program foo
use bar
allocate(x(2), source=f(2.))
print *, x
end program foo
More information about the Gcc-bugs
mailing list