Bug 57696 - [OOP] Defined assignment for components not used when those are ALLOCATABLE
Summary: [OOP] Defined assignment for components not used when those are ALLOCATABLE
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2013-06-24 10:15 UTC by Tobias Burnus
Modified: 2013-09-15 14:22 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-06-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2013-06-24 10:15:17 UTC
The following prints
          42
          42
with gfortran and
 42
 20
with crayftn. Thus, gfortran does not use defined assignment - but it should.

Note: If "right%foo" is not allocated, the code segfaults with crayftn.


module m0
  implicit none
  type component
    integer :: i = 42
  contains
    procedure :: assign0
    generic :: assignment(=) => assign0
  end type
  type parent
    type(component), allocatable :: foo
  end type
contains
  elemental subroutine assign0(lhs,rhs)
    class(component), intent(INout) :: lhs
    class(component), intent(in) :: rhs
    lhs%i = 20
  end subroutine
end module

program main
  use m0
  implicit none
  type(parent) :: left, right
  allocate(right%foo)
  print *, right%foo
  left = right
  print *, left%foo
end
Comment 1 Tobias Burnus 2013-06-24 10:23:28 UTC
From Fortran 2008, "7.2.1.3 Interpretation of intrinsic assignments", paragraph 13 (excerpt):

"For a noncoarray allocatable component the following sequence of operations is applied.
(1) If the component of the variable is allocated, it is deallocated.
(2) If the component of the value of expr is allocated, the corresponding
    component of the variable is allocated with the same dynamic type and type
    parameters as the component of the value of expr. If it is an array, it is
    allocated with the same bounds. The value of the component of the value of
    expr is then assigned to the corresponding component of the variable using
    defined assignment if the declared type of the component has a type-bound
    defined assignment consistent with the component, and intrinsic assignment
    for the dynamic type of that component otherwise."
Comment 2 Dominique d'Humieres 2013-06-24 10:56:51 UTC
Confirmed.
Comment 3 janus 2013-08-21 12:25:10 UTC
Related to PR 46897, I guess. The difference is that we are dealing with ALLOCATABLE components here.
Comment 4 Tobias Burnus 2013-09-15 14:22:10 UTC
See also PR 57697; the a similar needs to be also addressed when fixing this bug, namely the case that for:
  var.foo.bar
"foo" is NULL/unallocated while "bar" appears in the defined assignment.

See also PR46897 for another missing feature.