[Bug fortran/97864] New: Homebrew Operator Overload ICE

everythingfunctional at protonmail dot com gcc-bugzilla@gcc.gnu.org
Mon Nov 16 20:33:17 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97864

            Bug ID: 97864
           Summary: Homebrew Operator Overload ICE
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: everythingfunctional at protonmail dot com
  Target Milestone: ---

I have a project that was building fine on linux, but when I switched to MacOS
and tried to build, I got an ICE. I was able to narrow it down to the operator
overload. I'm completely baffled as to why this would build and run just fine
on linux, but cause an ICE on MacOS.

Version info:

GNU Fortran (Homebrew GCC 10.2.0) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

MWE (this could probably be simplified a bit more, but this seemed far enough):

module string_m
    implicit none
    private

    type, public :: VARYING_STRING
        private
        character(len=1), allocatable :: characters(:)
    end type

    interface operator(==)
        module procedure character_EQ_String
        module procedure string_EQ_String
    end interface

    interface CHAR
        module procedure stringToChar
    end interface

    public :: &
            operator(==), &
            CHAR, &
            VAR_STR
contains
    elemental function character_EQ_String(lhs, rhs) result(equals)
        character(len=*), intent(in) :: lhs
        type(VARYING_STRING), intent(in) :: rhs
        logical :: equals

        equals = lhs == char(rhs)
    end function character_EQ_String

    elemental function string_EQ_String(lhs, rhs) result(equals)
      type(VARYING_STRING), intent(in) :: lhs
      type(VARYING_STRING), intent(in) :: rhs
      logical :: equals

      equals = char(lhs) == char(rhs)
    end function

    pure function stringToChar(string) result(chars)
        type(VARYING_STRING), intent(in) :: string
        character(len=size(string%characters)) :: chars

        integer :: i
        integer :: length_input
        integer :: length_output

        length_output = len(chars)
        if (allocated(string%characters)) then
            length_input = size(string%characters)
            do concurrent (i = 1 : min(length_input, length_output))
                chars(i:i) = string%characters(i)
            end do
            if (length_input < length_output) then
                do concurrent (i = length_input+1 : length_output)
                    chars(i:i) = " "
                end do
            end if
        else
            do concurrent (i = 1 : length_output)
                chars(i:i) = " "
            end do
        end if
    end function stringToChar

    elemental function VAR_STR(char)
        character(len=*), intent(in) :: char
        type(VARYING_STRING) :: VAR_STR

        integer :: i
        integer :: length

        length = len(char)
        allocate(VAR_STR%characters(length))
        do concurrent (i = 1 : length)
            VAR_STR%characters(i) = char(i:i)
        end do
    end function VAR_STR
end module

module equal_test
    use string_m, only: VARYING_STRING, operator(==), char

    implicit none
    private

    public :: do_compare
contains
  pure function do_compare(first, second) result(result_)
    type(VARYING_STRING), intent(in) :: first
    type(VARYING_STRING), intent(in) :: second
    logical :: result_

    result_ = char(first) == char(second) ! works
    result_ = first == second ! works
    ! result_ = char(first) == second ! causes ice
  end function
end module

program main
  use equal_test, only: do_compare
  use string_m, only: var_str

  implicit none

  print *, do_compare(var_str("Hello"), var_str("World"))
end program main


More information about the Gcc-bugs mailing list