summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/pointer_array_1.f90
blob: 1d080da52cc18cf8cd5678a2852748efcfe730a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
! { dg-do run }
!
! Check the fix for PR34640 comments 1 and 3.
!
! This involves passing and returning pointer array components that
! point to components of arrays of derived types.
!
MODULE test
  IMPLICIT NONE
  TYPE :: my_type
    INTEGER :: value
    integer :: tag
  END TYPE
CONTAINS
  SUBROUTINE get_values(values, switch)
    INTEGER, POINTER :: values(:)
    integer :: switch
    TYPE(my_type), POINTER :: d(:)
    allocate (d, source = [my_type(1,101), my_type(2,102)])
    if (switch .eq. 1) then
      values => d(:)%value
      if (any (values .ne. [1,2])) print *, values(2)
    else
      values => d(:)%tag
      if (any (values .ne. [101,102])) STOP 1
    end if
  END SUBROUTINE

  function return_values(switch) result (values)
    INTEGER, POINTER :: values(:)
    integer :: switch
    TYPE(my_type), POINTER :: d(:)
    allocate (d, source = [my_type(1,101), my_type(2,102)])
    if (switch .eq. 1) then
      values => d(:)%value
      if (any (values .ne. [1,2])) STOP 2
    else
      values => d(:)%tag
      if (any (values([2,1]) .ne. [102,101])) STOP 3
    end if
  END function
END MODULE

  use test
  integer, pointer :: x(:)
  type :: your_type
    integer, pointer :: x(:)
  end type
  type(your_type) :: y

  call get_values (x, 1)
  if (any (x .ne. [1,2])) STOP 4
  call get_values (y%x, 2)
  if (any (y%x .ne. [101,102])) STOP 5

  x => return_values (2)
  if (any (x .ne. [101,102])) STOP 6
  y%x => return_values (1)
  if (any (y%x .ne. [1,2])) STOP 7
end