Class dummy argument for array of custom types stuck on -fcheck=bounds

Viewed 41

I have the following simple Fortran code

program test

    type vec
        integer :: x(3)
    end type

    type(vec) :: v(2)

    call sub(v)

contains

    subroutine sub (v)

        class(vec), intent(in) :: v(:)

        integer :: k, q(3)

        q = [ (v(1)%x(k), k = 1, 3) ]   ! <-- fails here

   end subroutine

end program

which, when compiled by GNU Fortran 11 (but not other versions) with -fcheck=bounds, fails with the error

At line 19 of file test.f90
Fortran runtime error: Index '3' of dimension 1 of array 'v%_data%x' above upper bound of 2

It look as if the compiler simply interchanged the lengths of x and v and this can be confirmed by changing the numbers.

When the word class is replaced by type, the problem goes away.

I believe that the code is valid as it is. Or is it violating some Fortran language restriction that results in this surprising behaviour?

0 Answers
Related