| 1 |
! { dg-do compile } |
| 2 |
! |
| 3 |
! Contributed by Brad Richardson <[email protected]> |
| 4 |
! |
| 5 |
module sub_m |
| 6 |
type :: sub_t |
| 7 |
private |
| 8 |
integer :: val |
| 9 |
end type |
| 10 |
|
| 11 |
interface sub_t |
| 12 |
module procedure constructor |
| 13 |
end interface |
| 14 |
|
| 15 |
interface sub_t_val |
| 16 |
module procedure t_val |
| 17 |
end interface |
| 18 |
contains |
| 19 |
function constructor(val) result(sub) |
| 20 |
integer, intent(in) :: val |
| 21 |
type(sub_t) :: sub |
| 22 |
|
| 23 |
sub%val = val |
| 24 |
end function |
| 25 |
|
| 26 |
function t_val(val) result(res) |
| 27 |
integer :: res |
| 28 |
type(sub_t), intent(in) :: val |
| 29 |
res = val%val |
| 30 |
end function |
| 31 |
end module |
| 32 |
|
| 33 |
module obj_m |
| 34 |
use sub_m, only: sub_t |
| 35 |
type :: obj_t |
| 36 |
private |
| 37 |
type(sub_t) :: sub_obj_ |
| 38 |
contains |
| 39 |
procedure :: sub_obj |
| 40 |
end type |
| 41 |
|
| 42 |
interface obj_t |
| 43 |
module procedure constructor |
| 44 |
end interface |
| 45 |
contains |
| 46 |
function constructor(sub_obj) result(obj) |
| 47 |
type(sub_t), intent(in) :: sub_obj |
| 48 |
type(obj_t) :: obj |
| 49 |
|
| 50 |
obj%sub_obj_ = sub_obj |
| 51 |
end function |
| 52 |
|
| 53 |
function sub_obj(self) |
| 54 |
class(obj_t), intent(in) :: self |
| 55 |
type(sub_t) :: sub_obj |
| 56 |
|
| 57 |
sub_obj = self%sub_obj_ |
| 58 |
end function |
| 59 |
end module |
| 60 |
|
| 61 |
program main |
| 62 |
use sub_m, only: sub_t, sub_t_val |
| 63 |
use obj_m, only: obj_t |
| 64 |
type(sub_t), allocatable :: z |
| 65 |
|
| 66 |
associate(initial_sub => sub_t(42)) |
| 67 |
associate(obj => obj_t(initial_sub)) |
| 68 |
associate(sub_obj => obj%sub_obj()) |
| 69 |
allocate (z, source = obj%sub_obj()) |
| 70 |
end associate |
| 71 |
end associate |
| 72 |
end associate |
| 73 |
if (sub_t_val (z) .ne. 42) stop 1 |
| 74 |
end program |