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

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

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!

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
    ! 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

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".


  • 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 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

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Retired 12/31/2016

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

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today