Re: [Fortran,patch] PR 41850 - Wrong-code with optional allocatable arrays

View: New views
5 Messages — Rating Filter:   Alert me  

Re: [Fortran,patch] PR 41850 - Wrong-code with optional allocatable arrays

by Dominique Dhumieres :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Tobias,

It seems that there is at least one typo in the second comment in the patch
for gcc/fortran/trans-expr.c:

+     the array needs to be packed an a check needs thus to be
                                          ^and (?)

I did not do any test yet.

Thanks for the patch,

Dominique


Parent Message unknown Re: [Fortran,patch] PR 41850 - Wrong-code with optional allocatable arrays

by Dominique Dhumieres :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Tobias,

With your patch the executable for the following code gives a bus error:

  integer, pointer :: a
!  nullify(a)
  allocate(a)
  a = 1
  call sub1a(a)
  call sub1b(a)
  call sub1c()
contains
  subroutine sub1a(a)
   integer, pointer :: a
   call sub2(a)
   call sub3(a)
  end subroutine sub1a
  subroutine sub1b(a)
   integer, pointer,optional :: a
   call sub2(a)
   call sub3(a)
  end subroutine sub1b
  subroutine sub1c(a)
   integer, pointer,optional :: a
   call sub2(a) ! <<< VALID or NOT?
!   call sub3(a) ! <<<< INVALID
  end subroutine sub1c
  subroutine sub2(b)
    integer, optional :: b
  end subroutine
  subroutine sub3(b)
    integer :: b
  end subroutine
end

It used to execute silently.

Cheers,

Dominique

Re: [Fortran,patch] PR 41850 - Wrong-code with optional allocatable arrays

by Tobias Burnus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Dominique,

Dominique Dhumieres wrote:
> With your patch the executable for the following code gives a bus error:
>
>   subroutine sub1c(a)
>    integer, pointer,optional :: a
>    call sub2(a) ! <<< VALID or NOT?
>   end subroutine sub1c
>   subroutine sub2(b)
>     integer, optional :: b
>  
NAG f95 -C=all and gfortran -fcheck=all agree that it is invalid:

Reference to OPTIONAL argument A which is not PRESENT
Program terminated by fatal error
In SUB1C, line 21 of aaa.f90
Called by $main$, line 7 of aaa.f90

At line 21 of file aaa.f90
Fortran runtime error: Pointer actual argument 'a' is not associated or
not present

And ifort runs silently until one uses "-check all" - then it crashes
with a segfault. While g95 crashes with default options.

I think it is invalid, though I have not (yet) checked the standard.

Tobias

Parent Message unknown Re: [Fortran,patch] PR 41850 - Wrong-code with optional allocatable arrays

by Dominique Dhumieres :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I think it is invalid, though I have not (yet) checked the standard.

If the previous code is invalid, is the following one also?

program optional
  call first(5)
  call first(6, c=4)
contains
subroutine first(a,b,c)
integer a
integer, optional :: b,c
    call second(a,b=b,c=c)
    call second(a,c=c)
    call second(a,b=b)
    call second(a)
return
end subroutine
subroutine second(a,b,c)
integer a
integer, optional :: b,c
print *, a, present(b), present(c)
return
end subroutine
end program

!from http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/d17f2ecd438fc639/385133c15a599b0d?lnk=gst&q=optional#385133c15a599b0d

which gives without complaining (even with -fcheck=all):

           5 F F
           5 F F
           5 F F
           5 F F
           6 F T
           6 F T
           6 F F
           6 F F

Is the difference between invalid and valid onlu due to the use of a pointer?

Dominique

PS I am not planning to use optional arguments whithout checking which
ones are present!

Re: [Fortran,patch] PR 41850 - Wrong-code with optional allocatable arrays

by Dominique Dhumieres :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think I have found the answer:

12.4.1.6 Restrictions on dummy arguments not present

...
(7) If it is a pointer, it shall not be allocated, deallocated, nullified, pointer-assigned, or supplied
as an actual argument corresponding to an optional nonpointer dummy argument.
                                                   ^^^^^^^^^^

so

   call sub2(a) ! <<< VALID or NOT?

is invalid for

  subroutine sub2(b)
    integer, optional :: b
  end subroutine

but valid for

  subroutine sub2(b)
    integer, pointer, optional :: b
  end subroutine

which is working with/without the patch.

Altough the burden is once again on the user, this could be detected
for contained procs and with -fwhole-file (is it worth opening a PR
for that?).

Dominique