最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

fortran - What is the expected return value of `associated` in cases involving base and derived type pointers pointing to a deri

programmeradmin1浏览0评论

While updating some legacy code to run when compiled with gcc 12.3, I came across some tests that were checking the return value of associated(px,py). These tests had previously passed on gcc 8.3 and gcc 10 looking for a return of .true.. On gcc 12.3 some cases were returning .false. and failing.

The below code replicates the patterns of checks I came across. The one that returns .false. using gcc 12.3 is associated(p_foo,the_baz%the_foos(3)).

module foobarbaz
  type :: foo
    integer :: i = -1
  endtype

  type, extends(foo) :: bar
    real :: r = -1.0
  end type

  type :: baz
    class(foo),pointer :: the_foos(:)
  end type
end module foobarbaz

program test_associate
  use foobarbaz
  implicit none

  class(bar), pointer :: the_bars(:)
  class(foo), pointer :: p_foo => NULL()
  class(bar), pointer :: p_bar => NULL()
  type(baz) :: the_baz

  allocate(the_bars(3))
  the_baz%the_foos => the_bars

  p_foo => the_baz%the_foos(3)
  write(*,*) "p_foo is associated                          :: ",associated(p_foo)                     ! T
  write(*,*) "p_foo is associated with the_baz%the_foos(3) :: ",associated(p_foo,the_baz%the_foos(3)) ! F

  write(*,*)
  write(*,*) "Select type p_foo to be of type bar"
  select type(p_foo)
    type is (bar)
      p_bar => p_foo
      write(*,*) "p_bar is associated                          :: ",associated(p_bar)                 ! T
      write(*,*) "p_bar is associated with p_foo               :: ",associated(p_bar,p_foo)           ! T
  end select
  write(*,*) "end select"
  write(*,*)

  write(*,*)
  write(*,*) "Select type the_baz%the_foos(3) to be of type bar in local a"
  select type(a => the_baz%the_foos(3))
  type is (bar)
    write(*,*) "p_bar is associatd to a                      :: ",associated(p_bar,a)                 ! T

    write(*,*)
    write(*,*) "Select type p_foo to be of type bar"
    select type(p_foo)
      type is (bar)
        write(*,*) "p_foo is associated with a                   :: ",associated(p_bar,p_foo)         ! T
    end select
    write(*,*) "end select"
  end select
  write(*,*) "end select"

end program test_associate

Output from gcc 12.3 compiled binary:

% ./a.out                                                   
p_foo is associated                          ::  T
p_foo is associated with the_baz%the_foos(3) ::  F

Select type p_foo to be of type bar
p_bar is associated                          ::  T
p_bar is associated with p_foo               ::  T
end select


Select type the_baz%the_foos(3) to be of type bar in local a
p_bar is associatd to a                      ::  T

Select type p_foo to be of type bar
p_foo is associated with a                   ::  T
end select
end select

I expected was that associated(p_foo,the_baz%the_foos(3)) would be .true. without an interloping select type.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论