Bug 83746 - Errors using the max intrinsic in a PDT length parameter expression for a function result
Summary: Errors using the max intrinsic in a PDT length parameter expression for a fun...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 83745 83747 (view as bug list)
Depends on:
Blocks: PDT
  Show dependency treegraph
 
Reported: 2018-01-09 05:16 UTC by Berke Durak
Modified: 2018-01-10 12:25 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-01-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Berke Durak 2018-01-09 05:16:13 UTC
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.
Comment 1 Dominique d'Humieres 2018-01-09 07:28:23 UTC
*** Bug 83747 has been marked as a duplicate of this bug. ***
Comment 2 Dominique d'Humieres 2018-01-10 12:23:35 UTC
Confirmed.
Comment 3 Dominique d'Humieres 2018-01-10 12:25:18 UTC
*** Bug 83745 has been marked as a duplicate of this bug. ***