[Bug fortran/83763] New: PDT variable sees content deallocated if variable is passed as an input to a function, and the function result is assigned to that same variable

2018-01-09 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83763

Bug ID: 83763
   Summary: PDT variable sees content deallocated if variable is
passed as an input to a function, and the function
result is assigned to that same variable
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: berke.durak at gmail dot com
  Target Milestone: ---

% cat test_case.f90
module bar
  implicit none

  type :: foo(n)
 integer, len :: n=10
 real :: vec(n)
  end type foo

contains

  function baz(a) result(b)
type(foo(n=*)), intent(in) :: a
type(foo(n=a%n)) :: b

b%vec(1)=a%vec(1)
  end function baz

end module bar

program test
  use bar
  implicit none
  call main

contains

  subroutine main
!type(foo(n)) :: x,y
type(foo) :: x!,y

x%vec=0
x=baz(x)! Segmentation fault
!x=baz(y)   ! THIS WORKS
  end subroutine main

end program test

% /usr/local/gfortran-bin/bin/gfortran test_case.f90 -o tc -Wall -Wextra -g -O0
-fdump-tree-all


% ./tc  

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7f455c722aef in ???
#1  0x4008f2 in __bar_MOD_baz
at /home/alpha/src/fortran/24_Autoder_with_PDTs/test_case.f90:15
#2  0x4009ec in main
at /home/alpha/src/fortran/24_Autoder_with_PDTs/test_case.f90:32
zsh: segmentation fault  ./tc

Looking at some intermediate representation, it seems to me that x%vec
is deallocated just before calling the function baz(), probably because the
result will be assigned to x:

% cat test_case.f90.088t.fixup_cfg4

...

   :
  _2 = x.vec.data;
  if (_2 != 0B)
goto ; [INV]
  else
goto ; [INV]

   :
  _3 = x.vec.data;
  __builtin_free (_3);

   :
  x.vec.data = 0B;
  D.3841 = baz (&x); [return slot optimization]

...


% /usr/local/gfortran-bin/bin/gfortran --version
GNU Fortran (GCC) 8.0.0 20180109 (experimental) [trunk revision 256361]
Copyright (C) 2018 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.

[Bug fortran/83762] New: ICE on variable declaration with undefined PDT parameter

2018-01-09 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83762

Bug ID: 83762
   Summary: ICE on variable declaration with undefined PDT
parameter
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: berke.durak at gmail dot com
  Target Milestone: ---

% cat test_case2.f90
module bar
  implicit none
  type :: foo(n)
 integer, len :: n=10
  end type foo
contains
  subroutine main
type(foo(undefined)) :: x
  end subroutine main
end module bar



% /usr/local/gfortran-bin/bin/gfortran test_case2.f90 -o tc -Wall -Wextra
test_case2.f90:11:0:

 end module bar

internal compiler error: in gfc_typenode_for_spec, at
fortran/trans-types.c:1116
0x6a0840 ???
../sysdeps/x86_64/elf/start.S:113
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.



% /usr/local/gfortran-bin/bin/gfortran --version 
GNU Fortran (GCC) 8.0.0 20180109 (experimental) [trunk revision 256361]
Copyright (C) 2018 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.

[Bug fortran/83747] New: Errors using the max intrinsic in a PDT length parameter expression for a function result

2018-01-08 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83747

Bug ID: 83747
   Summary: Errors using the max intrinsic in a PDT length
parameter expression for a function result
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: berke.durak at gmail dot com
  Target Milestone: ---

Found another one.  When a length parameter for a function
result is dynamically computed from argument parameters,
the max() intrinsic causes spurious warnings and the parameter
is set incorrectly, possibly uninitialized at run time:

module pdt_m
  implicit none
  type :: vec(k)
 integer, len :: k=0
 integer :: foo(k)
  end type vec
contains
  elemental function diy_max(a,b) result(c)
integer, intent(in) :: a,b
integer :: c
c=max(a,b)
  end function diy_max

  function add(a,b) result(c)
type(vec(k=*)), intent(in) :: a,b
type(vec(k=max(a%k,b%k))) :: c  ! Fails
!type(vec(k=diy_max(a%k,b%k))) :: c ! Works with diy_max
!type(vec(k=a%k+b%k)) :: c  ! Works with +

c%foo=0
c%foo(1:a%k)=a%foo
c%foo(1:b%k)=c%foo(1:b%k)+b%foo

print *,'c%k=',c%k
  end function add
end module pdt_m

program test_pdt
  use pdt_m
  implicit none
  type(vec(k=2)) :: u
  type(vec(k=3)) :: v,w

  print *,'w%k=',w%k
  print *,'size(w%foo)=',size(w%foo)

  u%foo=[1,2]
  v%foo=[10,20,30]
  w=add(u,v)

  print *,'w%k=',w%k
  print *,'size(w%foo)=',size(w%foo)
end program test_pdt

% /usr/local/gfortran-bin/bin/gfortran pdt.f90 -o pdt -Wall -Wextra -g
pdt.f90:25:0:

   end function add

Warning: ‘M.4’ is used uninitialized in this function [-Wuninitialized]
pdt.f90:25:0:

   end function add

note: ‘M.4’ was declared here
pdt.f90:25:0:

   end function add

Warning: ‘M.5’ is used uninitialized in this function [-Wuninitialized]
pdt.f90:25:0:

   end function add

note: ‘M.5’ was declared here

% ./pdt
 w%k=   3
 size(w%foo)=   3
 c%k=   329917520
 w%k=   329917520
 size(w%foo)=   0

And with -fcheck=all:

 w%k=   3
 size(w%foo)=   3
At line 21 of file pdt.f90
Fortran runtime error: Index '1' of dimension 1 of array 'c' outside of
expected range (1:0)

Error termination. Backtrace:
#0  0x400ded in __pdt_m_MOD_add
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:21
#1  0x40183d in test_pdt
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:39
#2  0x401a61 in main
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:29

Version:

% /usr/local/gfortran-bin/bin/gfortran --version
GNU Fortran (GCC) 8.0.0 20180109 (experimental) [trunk revision 256361]
Copyright (C) 2018 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.

[Bug fortran/83746] New: Errors using the max intrinsic in a PDT length parameter expression for a function result

2018-01-08 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83746

Bug ID: 83746
   Summary: Errors using the max intrinsic in a PDT length
parameter expression for a function result
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: berke.durak at gmail dot com
  Target Milestone: ---

Found another one.  When a length parameter for a function
result is dynamically computed from argument parameters,
the max() intrinsic causes spurious warnings and the parameter
is set incorrectly, possibly uninitialized at run time:

module pdt_m
  implicit none
  type :: vec(k)
 integer, len :: k=0
 integer :: foo(k)
  end type vec
contains
  elemental function diy_max(a,b) result(c)
integer, intent(in) :: a,b
integer :: c
c=max(a,b)
  end function diy_max

  function add(a,b) result(c)
type(vec(k=*)), intent(in) :: a,b
type(vec(k=max(a%k,b%k))) :: c  ! Fails
!type(vec(k=diy_max(a%k,b%k))) :: c ! Works with diy_max
!type(vec(k=a%k+b%k)) :: c  ! Works with +

c%foo=0
c%foo(1:a%k)=a%foo
c%foo(1:b%k)=c%foo(1:b%k)+b%foo

print *,'c%k=',c%k
  end function add
end module pdt_m

program test_pdt
  use pdt_m
  implicit none
  type(vec(k=2)) :: u
  type(vec(k=3)) :: v,w

  print *,'w%k=',w%k
  print *,'size(w%foo)=',size(w%foo)

  u%foo=[1,2]
  v%foo=[10,20,30]
  w=add(u,v)

  print *,'w%k=',w%k
  print *,'size(w%foo)=',size(w%foo)
end program test_pdt

% /usr/local/gfortran-bin/bin/gfortran pdt.f90 -o pdt -Wall -Wextra -g
pdt.f90:25:0:

   end function add

Warning: ‘M.4’ is used uninitialized in this function [-Wuninitialized]
pdt.f90:25:0:

   end function add

note: ‘M.4’ was declared here
pdt.f90:25:0:

   end function add

Warning: ‘M.5’ is used uninitialized in this function [-Wuninitialized]
pdt.f90:25:0:

   end function add

note: ‘M.5’ was declared here

% ./pdt
 w%k=   3
 size(w%foo)=   3
 c%k=   329917520
 w%k=   329917520
 size(w%foo)=   0

And with -fcheck=all:

 w%k=   3
 size(w%foo)=   3
At line 21 of file pdt.f90
Fortran runtime error: Index '1' of dimension 1 of array 'c' outside of
expected range (1:0)

Error termination. Backtrace:
#0  0x400ded in __pdt_m_MOD_add
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:21
#1  0x40183d in test_pdt
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:39
#2  0x401a61 in main
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:29

Version:

% /usr/local/gfortran-bin/bin/gfortran --version
GNU Fortran (GCC) 8.0.0 20180109 (experimental) [trunk revision 256361]
Copyright (C) 2018 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.

[Bug fortran/83731] PDT length parameter incorrectly rejected at run-time with -fcheck=bounds

2018-01-08 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83731

--- Comment #6 from Berke Durak  ---
(In reply to Paul Thomas from comment #3)
> Hi Berke,
> 
> The fix for this turns out to be so trivial that I will roll it in to the
> patch for pr83611.
> 
> I know that this must be frustrating but please keep trying out PDTs. The
> bug reports that you are generating are invaluable at this stage.
> 
> Thanks
> 
> Paul

Hi Paul,

It worked.

Thanks to you and the others for the hard work!
Hopefully PDTs will be usable soon.  I will keep reporting
problems as I bump into them.

[Bug fortran/83731] New: PDT length parameter incorrectly rejected at run-time with -fcheck=bounds

2018-01-07 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83731

Bug ID: 83731
   Summary: PDT length parameter incorrectly rejected at run-time
with -fcheck=bounds
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: berke.durak at gmail dot com
  Target Milestone: ---

Using the nightly 20180107 trunk rev. 256317:

% cat pdt.f90

module pdt_m
  implicit none
  type :: vec(k)
 integer, len :: k=10
 integer :: foo(k)
  end type vec
contains
  function total(a)
type(vec(k=*)), intent(in) :: a
integer :: total

total=sum(a%foo)
  end function total
end module pdt_m

program test_pdt
  use pdt_m
  implicit none
  type(vec(k=123)) :: u

  u%foo=1
  print *,'total=',total(u)
end program test_pdt

% /usr/local/gfortran-bin/bin/gfortran pdt.f90 -fcheck=bounds -g -O0 -o pdt
-Wall -Wextra
% LD_LIBRARY_PATH=/usr/local/gfortran-bin/lib64:$LD_LIBRARY_PATH ./pdt 
In file 'pdt.f90', around line 14 
Fortran runtime error: The value of the PDT LEN parameter 'k' does not agree
with that in the dummy declaration

Error termination. Backtrace:
#0  0x40096a in __pdt_m_MOD_total
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:13
#1  0x400c45 in test_pdt
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:22
#2  0x400cce in main
at /home/alpha/src/fortran/23_Parametrized_derived_types/pdt.f90:17

GNU Fortran (GCC) 8.0.0 20180107 (experimental) [trunk revision 256317]
Copyright (C) 2018 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.

[Bug fortran/83567] New: Parametrized derived types: Segmentation fault when assigning a function return value

2017-12-23 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83567

Bug ID: 83567
   Summary: Parametrized derived types: Segmentation fault when
assigning a function return value
   Product: gcc
   Version: 8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: berke.durak at gmail dot com
  Target Milestone: ---

Just wrote a small program to test the new parametrized derived types. 
Unfortunately I get runtime memory corruption errors and segmentation faults.

% cat pdf.f90

module pdt_m
  implicit none
  type :: vec(k)
 integer, len :: k=3
 integer :: foo(k)=[1,2,3]
  end type vec
contains
  function addvv(a,b) result(c)
type(vec(k=*)), intent(in) :: a
type(vec(k=*)), intent(in) :: b
type(vec(k=a%k)) :: c

c%foo=a%foo+b%foo
  end function
end module pdt_m

program test_pdt
  use pdt_m
  implicit none
  type(vec) :: u,v,w

  u%foo=[1,2,3]
  v%foo=[2,3,4]
  w=addvv(u,v)
  print *,w
end program test_pdt

% gfortran-8 --version
GNU Fortran (Debian 8-20171223-1) 8.0.0 20171223 (experimental) [trunk revision
255988]

% gfortran-8 pdt.f90 -o pdt -Wall -O3 -g -fcheck=all -Wextra

% ./pdt

   3   0   0   7
*** Error in `./pdt': double free or corruption (fasttop): 0x55cc59c66630
***
=== Backtrace: =
/lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7f303fe40bfb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7f303fe46fc6]
/lib/x86_64-linux-gnu/libc.so.6(+0x7780e)[0x7f303fe4780e]
./pdt(+0xdf0)[0x55cc59a0fdf0]
./pdt(+0x89f)[0x55cc59a0f89f]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7f303fdf02e1]
./pdt(+0x8da)[0x55cc59a0f8da]
=== Memory map: 
55cc59a0f000-55cc59a11000 r-xp  fe:04 1316416   
/home/alpha/src/fortran/23_Parametrized_derived_types/pdt
55cc59c1-55cc59c11000 r--p 1000 fe:04 1316416   
/home/alpha/src/fortran/23_Parametrized_derived_types/pdt
55cc59c11000-55cc59c12000 rw-p 2000 fe:04 1316416   
/home/alpha/src/fortran/23_Parametrized_derived_types/pdt
55cc59c63000-55cc59c84000 rw-p  00:00 0  [heap]
7f303800-7f3038021000 rw-p  00:00 0 
7f3038021000-7f303c00 ---p  00:00 0 
7f303fbb6000-7f303fbcf000 r-xp  fe:04 393306
/lib/x86_64-linux-gnu/libz.so.1.2.8
7f303fbcf000-7f303fdce000 ---p 00019000 fe:04 393306
/lib/x86_64-linux-gnu/libz.so.1.2.8
7f303fdce000-7f303fdcf000 r--p 00018000 fe:04 393306
/lib/x86_64-linux-gnu/libz.so.1.2.8
7f303fdcf000-7f303fdd rw-p 00019000 fe:04 393306
/lib/x86_64-linux-gnu/libz.so.1.2.8
7f303fdd-7f303ff63000 r-xp  fe:04 396680
/lib/x86_64-linux-gnu/libc-2.24.so
7f303ff63000-7f3040163000 ---p 00193000 fe:04 396680
/lib/x86_64-linux-gnu/libc-2.24.so
7f3040163000-7f3040167000 r--p 00193000 fe:04 396680
/lib/x86_64-linux-gnu/libc-2.24.so
7f3040167000-7f3040169000 rw-p 00197000 fe:04 396680
/lib/x86_64-linux-gnu/libc-2.24.so
7f3040169000-7f304016d000 rw-p  00:00 0 
7f304016d000-7f30401ab000 r-xp  fe:04 5512042   
/usr/lib/x86_64-linux-gnu/libquadmath.so.0.0.0
7f30401ab000-7f30403ab000 ---p 0003e000 fe:04 5512042   
/usr/lib/x86_64-linux-gnu/libquadmath.so.0.0.0
7f30403ab000-7f30403ac000 r--p 0003e000 fe:04 5512042   
/usr/lib/x86_64-linux-gnu/libquadmath.so.0.0.0
7f30403ac000-7f30403ad000 rw-p 0003f000 fe:04 5512042   
/usr/lib/x86_64-linux-gnu/libquadmath.so.0.0.0
7f30403ad000-7f30403c4000 r-xp  fe:04 393267
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f30403c4000-7f30405c3000 ---p 00017000 fe:04 393267
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f30405c3000-7f30405c4000 r--p 00016000 fe:04 393267
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f30405c4000-7f30405c5000 rw-p 00017000 fe:04 393267
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f30405c5000-7f30406c8000 r-xp  fe:04 396688
/lib/x86_64-linux-gnu/libm-2.24.so
7f30406c8000-7f30408c7000 ---p 00103000 fe:04 396688
/lib/x86_64-linux-gnu/libm-2.24.so
7f30408c7000-7f30408c8000 r--p 00102000 fe:04 396688
/lib/x86_64-linux-gnu/libm-2.24.so
7f30408c8000-7f30408c9000 rw-p 00103000 fe:04 396688
/lib/x86_64-linux-gnu/libm-2.24.so
7f30408c9000-7f3040b1a000 r-xp  fe:04 5621520   
/usr/lib/x86_64-linux-gnu/libgfortran.so.5.0.0
7f3040b1a000-7f3040d19000 ---p 00251000 fe:04 5621520   
/usr/lib/x86_64-linux-gnu/libgfortran.so.5.0.0
7f3040d19000-7f3040d1a000 r--p 0025 fe:04 5621520   
/usr/lib/x86_64-linu

[Bug fortran/79440] internal compiler error: in fold_convert_loc, at fold-const.c:2373

2017-05-11 Thread berke.durak at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79440

Berke Durak  changed:

   What|Removed |Added

 CC||berke.durak at gmail dot com

--- Comment #2 from Berke Durak  ---
I get a similar error with 7.0.1:

% cat crash.f90
module gogol
  implicit none

  type plop
  end type plop
contains
  function create() result(this)
class(plop), allocatable :: this
allocate(this)
  end function create
end module gogol

program foo
  use gogol
  implicit none
  type(plop), allocatable :: x
  x=create()
contains
end program foo

% gfortran crash.f90
crash.f90:17:0:

   x=create()

internal compiler error: in fold_convert_loc, at fold-const.c:2361
0x864863 fold_convert_loc(unsigned int, tree_node*, tree_node*)
../../src/gcc/fold-const.c:2361
0x6a5d7a gfc_allocate_using_malloc(stmtblock_t*, tree_node*, tree_node*, tre
e_node*)   
../../src/gcc/fortran/trans.c:662
0x6de110 trans_class_assignment
../../src/gcc/fortran/trans-expr.c:9693
0x6de110 gfc_trans_assignment_1
../../src/gcc/fortran/trans-expr.c:10031
0x6a38ff trans_code
../../src/gcc/fortran/trans.c:1817
0x6c8907 gfc_generate_function_code(gfc_namespace*)
../../src/gcc/fortran/trans-decl.c:6296
0x65dac6 translate_all_program_units
../../src/gcc/fortran/parse.c:6051
0x65dac6 gfc_parse_file()
../../src/gcc/fortran/parse.c:6251
0x6a043f gfc_be_parse_file
../../src/gcc/fortran/f95-lang.c:204
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See  for instructions.