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

            Bug ID: 108680
           Summary: Wrong DTIO arguments with -fdefault-integer-8
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: albandil at atlas dot cz
  Target Milestone: ---

Wrong DTIO arguments with -fdefault-integer-8

It seems that gfortran miscompiles the following simple program when
`-fdefault-integer-8` compiler option is used:

    module types

        type dtype
        contains
            procedure :: write_formatted
            generic, public :: write(formatted) => write_formatted
        end type

    contains

        subroutine write_formatted (this, unit, iotype, v_list, iostat, iomsg)

            class(dtype), intent(in)    :: this
            integer,      intent(in)    :: unit, v_list(:)
            character(*), intent(in)    :: iotype
            integer,      intent(out)   :: iostat
            character(*), intent(inout) :: iomsg

            iostat = 0

            print *, 'v_list', v_list

        end subroutine write_formatted

    end module types


    program p

        use types, only: dtype

        type(dtype) :: data
        integer     :: u

        open (file = 'output.txt', newunit = u, form = 'formatted')
        write (u, '(dt(1,2,3))') data
        close (u)

    end program p

The derived type `dtype` should be given integers 1,2,3 in the v_list parameter
of the `write(formatted)` subroutine, which only echoes them out for feedback.
This works with Intel compilers regardless of the default integer type.
However, the output is wrong if I combine gfortran `-fdefault-integer-8`. The
behaviour is the same with the current git version as well as with GCC release
11.3, so the problem has been around for some time.

    $ /opt/gcc-git/bin/gfortran --version | head -n 1
    GNU Fortran (GCC) 13.0.1 20230205 (experimental)
    $ /opt/gcc-git/bin/gfortran main.f90 
    $ ./a.out 
     v_list           1           2           3
    $ /opt/gcc-git/bin/gfortran -fdefault-integer-8 main.f90 
    $ ./a.out 
     v_list           8589934593                    3                    0

    $ gfortran-11 --version | head -n 1
    GNU Fortran (SUSE Linux) 11.3.1 20221024 [revision
bd0c76a2329e7fe6d6612c2259647bbb67f5866a]
    $ gfortran-11 -fdefault-integer-8 main.f90 
    $ ./a.out 
     v_list           8589934593                    3                    0

    $ ifx --version | head -n 1
    ifx (IFORT) 2023.0.0 20221201
    $ ifx -i8 main.f90 
    $ ./a.out 
     v_list                     1                     2                     3

    $ ifort --version | head -n 1
    ifort (IFORT) 2021.8.0 20221119
    $ ifort -i8 main.f90 
    $ ./a.out 
     v_list                     1                     2                     3

It looks as if the subroutine was actually getting pointers to 4-byte integers
regardless of the switch, because the large value pritned first is actually
composition of the missing 2 and 1:

    8589934593 ~ 00000000000000000000000000000010
00000000000000000000000000000001
               ~ 2 1

Reply via email to