【问题标题】:gfortran associates wrong type-bound proceduregfortran 关联错误的类型绑定过程
【发布时间】:2018-03-04 07:55:02
【问题描述】:

当我们编译(gfortran 5.3 或 7.2)并运行以下代码时,main.f03 的第 9 行最终出现在一个从未调用过的子例程中。谁能解释一下为什么?

main.f03:

program main
    use minimalisticcase
    implicit none

    type(DataStructure) :: data_structure
    type(DataLogger) :: data_logger

    call data_structure%init()
    call data_logger%init(data_structure)
end program

minimalisticcase.f03:

module minimalisticcase
    implicit none

    type, public :: DataStructure
        integer :: i
    contains
        procedure, pass :: init => init_data_structure
        procedure, pass :: a => beginning_of_alphabet
    end type



    type, public :: DataLogger
        type(DataStructure), pointer :: data_structure
        contains
                procedure, pass :: init => init_data_logger
                procedure, pass :: do_something => do_something
    end type

contains
    subroutine init_data_structure(self)
        implicit none
        class(DataStructure), intent(inout) :: self
        write(*,*) 'init_data_structure'
    end subroutine

    subroutine beginning_of_alphabet(self)
        implicit none
        class(DataStructure), intent(inout) :: self

        write(*,*) 'beginning_of_alphabet'
    end subroutine

    subroutine init_data_logger(self, data_structure)
        implicit none
        class(DataLogger), intent(inout) :: self
        class(DataStructure), target :: data_structure
        write(*,*) 'init_data_logger'

        self%data_structure => data_structure
        call self%do_something()
    end subroutine

    subroutine do_something(self)
        implicit none
        class(DataLogger), intent(inout) :: self

        write(*,*) 'do_something'
    end subroutine
end module

在“minimalisticcase.f03”的第 40 行,我们将 DataLogger 称为“do_something”。但改为执行 DataStructure 的 'beginning_of_alphabet' 子程序!

显然可以通过将“minimalisticcase.f03”中的第 13 行从 type(DataStructure), pointer :: data_structure 更改为 class(DataStructure), pointer :: data_structure 来解决此问题。

但是为什么呢?

【问题讨论】:

  • ifort 17 和 gfortran 7.2 对我来说一切正常。首先是init_data_structure,然后是init_data_logger,最后是do_something
  • @马特:这很有趣!我可以在我的 Windows 和我的 linux 机器上重现这种行为。您在哪个平台上工作?
  • 我在 Windows 10(64 位)上测试了这个,在 WSL 中使用 gfortran。

标签: fortran procedure gfortran derived-types


【解决方案1】:

这是 gfortran 中的一个错误。我在 Bugzilla 上以https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82312 的形式发布了它。该错误现已在 GCC 主干上得到修复。

临时解决方法是将指针分配包含在选择类型中,因此:

    select type (data_structure)
      type is (DataStructure)
        self%data_structure => data_structure
    end select

【讨论】:

  • 报告gcc.gnu.org/bugzilla/show_bug.cgi?id=82312欢迎保罗。顺便说一句,我们通常不会在此处签名和使用问候语,因为您的名字会自动放在帖子下方右侧图标旁边。
  • 您的解决方法与我在问题中提到的解决方法不同。有理由偏爱其中一个吗? (感谢您提交报告)
  • 该错误现已在 7-branch 上修复并已关闭。感谢您的报告。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多