| 1 |
! { dg-do compile } |
| 2 |
! { dg-additional-options "-fdump-tree-original" } |
| 3 |
! |
| 4 |
! PR fortran/90069 |
| 5 |
! |
| 6 |
! Contributed by Brad Richardson <[email protected]> |
| 7 |
! |
| 8 |
|
| 9 |
program returned_memory_leak |
| 10 |
implicit none |
| 11 |
|
| 12 |
type, abstract :: base |
| 13 |
end type base |
| 14 |
|
| 15 |
type, extends(base) :: extended |
| 16 |
end type extended |
| 17 |
|
| 18 |
type :: container |
| 19 |
class(*), allocatable :: thing |
| 20 |
end type |
| 21 |
|
| 22 |
call run() |
| 23 |
contains |
| 24 |
subroutine run() |
| 25 |
type(container) :: a_container |
| 26 |
|
| 27 |
a_container = theRightWay() |
| 28 |
a_container = theWrongWay() |
| 29 |
end subroutine |
| 30 |
|
| 31 |
function theRightWay() |
| 32 |
type(container) :: theRightWay |
| 33 |
|
| 34 |
class(base), allocatable :: thing |
| 35 |
|
| 36 |
allocate(thing, source = newAbstract()) |
| 37 |
theRightWay = newContainer(thing) |
| 38 |
end function theRightWay |
| 39 |
|
| 40 |
function theWrongWay() |
| 41 |
type(container) :: theWrongWay |
| 42 |
|
| 43 |
theWrongWay = newContainer(newAbstract()) |
| 44 |
end function theWrongWay |
| 45 |
|
| 46 |
function newAbstract() |
| 47 |
class(base), allocatable :: newAbstract |
| 48 |
|
| 49 |
allocate(newAbstract, source = newExtended()) |
| 50 |
end function newAbstract |
| 51 |
|
| 52 |
function newExtended() |
| 53 |
type(extended) :: newExtended |
| 54 |
end function newExtended |
| 55 |
|
| 56 |
function newContainer(thing) |
| 57 |
class(*), intent(in) :: thing |
| 58 |
type(container) :: newContainer |
| 59 |
|
| 60 |
allocate(newContainer%thing, source = thing) |
| 61 |
end function newContainer |
| 62 |
end program returned_memory_leak |
| 63 |
|
| 64 |
! { dg-final { scan-tree-dump-times "newabstract" 15 "original" } } |
| 65 |
! { dg-final { scan-tree-dump-times "__builtin_free" 8 "original" } } |
| 66 |
|