https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83745
Bug ID: 83745 Summary: Errors using the max intrinsic in a PDT length parameter expression for a function result Product: gcc Version: 8.0 Status: RESOLVED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: berke.durak at gmail dot com Target Milestone: --- Status: RESOLVED Resolution: DUPLICATE 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 from Dominique d'Humieres <dominiq at lps dot ens.fr> --- Dup. *** This bug has been marked as a duplicate of bug 83746 ***