https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127637

--- Comment #2 from Mikael Morin <mikael at gcc dot gnu.org> ---
(In reply to Mikael Morin from comment #0)
> And the testcase below is another failing example with the
> optional attribute which has the same problem, even if it is using a
> dedicated variable to track the attribute through the reference chain.
> 
> program prog
>    implicit none
>    integer, parameter :: n = 5
>    type :: t
>       integer :: c
>    end type
>    type(t), allocatable :: x(:)
>    integer :: i
>    call sub()
> contains
>    elemental function f(a) result(r)
>       type(t), optional, intent(in) :: a
>       integer :: r
>       r = 42
>       if (present(a)) r = a%c
>    end function
>    subroutine sub(o)
>       class(t), optional, intent(in) :: o(:)
>       integer, allocatable :: y(:)
>       allocate(y(n))
>       y(:) = f(o)
>       if (any(y /= 42)) error stop 1
>    end 
> end program
> 
> This fails at runtime because the scalarization is using the size of o as
> loop upper bound in the scalarization of the assignment y(:) = f(o), which
> is an uninitialized variable as o is absent.  The scalarizer should see that
> o is optional and pick the loop bound from y instead, but it doesn't happen
> here as o is polymorphic and gfc_expr_attr doesn't return optional among the
> set of attributes.

Wait, with -pedantic-errors, the compiler reports:
> ./comment_0.f90:27:15:
> 
>    27 |       y(:) = f(o)
>       |               1
> Error: ‘o’ at (1) is an array and OPTIONAL; If it is not present, then it
> cannot be the actual argument of an ELEMENTAL procedure unless there is a non-
> optional argument with the same rank (Fortran 2018, 15.5.2.12)
> [-Werror=pedantic]

So the testcase is actually invalid.

Reply via email to