Bug 51864 - [OOP] ALLOCATE with polymorphic array constructor as SOURCE=
Summary: [OOP] ALLOCATE with polymorphic array constructor as SOURCE=
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2012-01-15 12:55 UTC by Tobias Burnus
Modified: 2014-07-20 08:33 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-07-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2012-01-15 12:55:12 UTC
See also http://gcc.gnu.org/ml/fortran/2012-01/msg00147.html

The following program causes gfortran to ICE with a segfault. The program compiles with NAG f95 5.1 and crayftn 7.1 - though it segfaults at run time.

I think the program is valid F2003/F2008.

type t
  integer :: i = 5
end type t
type, extends(t) :: t2
  integer :: j = 6
end type t2

class(t), allocatable :: a(:), b(:), c(:)
allocate(t2 :: a(3))
allocate(t2 :: b(5))
!allocate(c, source=[ a, b ]) ! F2008, PR 44672
allocate(c(8), source=[ a, b ])
! c = [ a, b ] ! F2008, PR 43366
select type(c)
  type is(t)
    print '(8(i2))', c%i
  type is(t2)
    print '(8(i2))', c%i
    print '(8(i2))', c%j
end select
end
Comment 1 Tobias Burnus 2012-01-15 23:25:55 UTC
(In reply to comment #0)
> class(t), allocatable :: a(:), b(:), c(:)
> allocate(t2 :: a(3))
> allocate(t2 :: b(5))
> allocate(c(8), source=[ a, b ])

Actually, this part of the program is invalid as Fortran 2008 demands (4.8 Construction of array values):

"The dynamic type of an array constructor is the same as its declared type."

Thus, one can use
 [ a, b ]
but only if both are of the declared and dynamic type "t", cf.

"If type-spec is omitted, each ac-value expression in the array constructor shall have the same length type parameters; in this case, the declared type and type parameters of the array constructor are those of the ac-value expressions."

However, one is still allowed to use:

 [ t2 :: a, b]

in that case, "a" and "b" need to have a declared type "t2" or an extension of "t2" as declared type - and can have any dynamic type.

"If type-spec appears, it specifies the declared type and type parameters of the array constructor. Each ac-value expression in the array-constructor shall be compatible with intrinsic assignment to a variable of this type and type parameters. Each value is converted to the type parameters of the array-constructor in accordance with the rules of intrinsic assignment (7.2.1.3)."

And in 4.3.1.3 CLASS, one finds "type compatible" defined as:

"A polymorphic entity that is not an unlimited polymorphic entity is type compatible with entities of the same declared type or any of its extensions. Even though an unlimited polymorphic entity is not considered to have a declared type, it is type compatible with all entities. An entity is type compatible with a type if it is type compatible with entities of that type."
Comment 2 Dominique d'Humieres 2014-07-20 08:33:09 UTC
Compiling the code in comment 0 still gives an ICE with gfortran 4.10.0 r212833 (the same ICE as for pr53653):

pr51864.f90: In function 'MAIN__':
pr51864.f90:12:0: internal compiler error: in gfc_conv_array_constructor_expr, at fortran/trans-expr.c:5668
 allocate(c(8), source=[ a, b ])
 ^

I also get the same ICE with the following code

  call pr53876
end

subroutine pr53876
  IMPLICIT NONE
  TYPE :: individual
    integer :: icomp ! Add an extra component to test offset
    REAL, DIMENSION(:), ALLOCATABLE :: genes
  END TYPE
  CLASS(individual), DIMENSION(:), ALLOCATABLE :: indv, indv1
  allocate (indv(2), source = [individual(1, [99,999]), &
                               individual(2, [999,9999])])
  allocate (indv1(2), source = [indv])
END

If I understand correctly comment 1, this code should be valid. Replacing '[indv]' with '[indv(1),indv(2)]' does not fix the ICE, but replacing it with 'imdv' does.