Skip to content

[flang] Modifying dummy argument via its global actual argument failed for array case with 2 or more elements #159149

@DanielCChen

Description

@DanielCChen

Consider the following code:

module m
    type base
        integer*4 :: id = -1
        contains
        procedure :: print => printBase
        procedure :: increaseID => increaseBaseID
    end type

    type, extends (base) :: child
        character*20 :: name = 'default'
        contains
        procedure :: print => printChild
    end type

    class (base), pointer :: b1_m (:)

    type (child), save, target :: c1_m (1)
    type (child), save, target :: c2_m (2)

    class (base), pointer :: b1_m_s
    type (child), save, target :: c1_m_s

    contains
    elemental subroutine increaseBaseID (b, i1)
        class (base), intent(inout) :: b
        integer*4, intent(in) :: i1

        b%id = b%id + i1
    end subroutine

    subroutine printBase (b)
        class (base), intent(in) :: b

        print *, b%id
    end subroutine

    subroutine printChild (b)
        class (child), intent(in) :: b

        print *, b%id, b%name
    end subroutine

    subroutine test1 (b)
        type (base), target, intent(inout) :: b (:)

        call b1_m%increaseID (10)
        call b(1)%print
    end subroutine

    subroutine test1_s (b)
        type (base), target, intent(inout) :: b

        call b1_m_s%increaseID (10)
        call b%print
    end subroutine
end module


program fArg033a3
use m

    c1_m = (/child (1,'c1_m')/)
    c2_m = (/(child (i,'c1_m'), i=1,2)/)

    b1_m => c2_m              !! ERROR case: array of 2 elements
    call test1 (b1_m)

    b1_m => c1_m              !! WORKING case: array of 1 element
    call test1 (b1_m)

    c1_m_s = child (1,'c1_m') !! WORKING case: scalar
    b1_m_s => c1_m_s
    call test1_s (b1_m_s)
end

Flang failed to update the global variable c2_m that is an array of 2 elements in the ERROR case.
It works fine for the c1_m case that is an array of 1 element.
It works fine for the c1_m_s, the scalar case.

Flang outputs

> a.out
 1
 11
 11

Expected output

> a.out
 11
 11
 11

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions