Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28632 Discussions

derived-type assignment (with alloc. component) in function call

Ferdinand_T_
New Contributor II
351 Views

Hello everyone,

the code below shows strange behaviour of an intrinsic derived-type assignment executed in a function call (when compiled with ifort 13.0.1).
Since I am not sure whether or not this code finally violates the Fortran standard, any advice and comments are appreciated!

[fortran]

module m
   implicit none

    type other_type
        integer :: i
    end type

    ! a type with allocatable and scalar data
    type :: my_type
        class(other_type), allocatable :: allocatable_data
        integer :: scalar_data = 1
    end type

contains

    ! returns a copy of a specific original type T
    function get_a_copy_fun()
        implicit none
        type(my_type) :: get_a_copy_fun, original

        original%scalar_data = 2    ! change original data
        get_a_copy_fun = original   ! intrinsic assignment
    end function

end module


program main
    use m
    implicit none

    type(my_type) :: t

    t = get_a_copy_fun()

    print *, t%scalar_data          ! prints "1" instead "2"

end program main

[/fortran]

The last print statement should output "2" if the assignment was carried out correctly (at least, to what I expect - but I may be wrong), but instead I get the default initialized value "1".

Remarks:

  • Reproduces ONLY if the intrinsic assignment is to the function output argument within a function, not in a subroutine or program statement;  and if the "class" keyword is used for allocatable_data ("type" produces correct results). Are there some restrictions that apply to function output arguments that I did violate here?
  • Executing the print statement on the result inside the function yields same incorrect result
  • It seems that the assignment here has the same effect as a structure constructor, e.g. without copying anything (also, allocatable_data on the lhs of the assignment doens't get allocated, if the rhs was allocated)
  • May this be related to the (fixed?) ifort issue at http://software.intel.com/en-us/forums/topic/287152 which now runs fine with ifort version 13.0.1? However, why does this only happen inside a function?

Thanks for having a look,

regards Ferdinand



0 Kudos
2 Replies
Steven_L_Intel1
Employee
351 Views

I can reproduce the problem - it isn't the same as the other thread. I will escalate this to the developers and let you know of progress. The issue ID is DPD200240809.

0 Kudos
Steven_L_Intel1
Employee
351 Views

The developers told me that they recently fixed this problem and that the fix is expected to appear in Update 3.

0 Kudos
Reply