When someone was getting bad answers with a new compiler after running
a program for a long time successfully with another it came down to the
somewhat non-intuitive fact that array(i) and array(i:i) are NOT the same
thing. And changing the code to use array(i) made it behave consistently
and as desired with both compilers.
Along the way I was using an analogy of logical operators basically
obeying the same rules as a user-defined operator for an elemental
function - that would mean comparing two arrays of a different size,
even if one array only had one element was not allowed; even if there
were other logical options like
- only comparing up to the size of the smaller array
- returning .false. for any remaining comparisons once the smallest
array was exhausted - repeating values in the smaller arrays till they matched the size
of the largest.
So when hoping to demonstrate this I was surprised that the first three
compilers I tried all compiled and ran the following program without
complaint, and even with a lot of debug flags I only got one to produce
a warning ( NOT an error, and they all continued to run).
So one padded out the answer to the size of the largest array with .FALSE.
and two appear to have stopped once the size of the smaller array was
reached, but no one returned a run-time error that I tried so far. I could
see how it would be a natural extension to treat a single element array
as a scalar but even when I increased the size of II() to two elments
the behavior continued. Is “ii.eq.[10,10,10,10]” always non-standard
unless II() has four elements, and it is just a case of the compilers
not reporting the error, or is one of these behaviors actually defined
by the standard?
program testit
integer,allocatable :: ii(:)
ii=[10]
do i=1,2
write(*,*) ii.eq.[10,10,10,10]
write(*,*) all(ii.eq.[10,10,10,10])
write(*,*) all(ii(1).eq.[10,10,10,10]) ! assumed OK
! everyone catches this as not conformable
!write(*,*) 'array ',all(ii(1:1).eq.[10,10,10,10])
ii=[10,10]
enddo
end program testit
gfortran app/main.f90
T F F F
F
T
T T F F
F
T
ifort app/main.f90
T
T
T
T T
T
T
nvfortran app/main.f90
T
T
T
T T
T
T
Is this undefined behavior the compiler can do what it wants with, or
is there an expected behavior, or are the compilers required to produce
an error per the standard? Especially with ALL() and ANY() involved I am not quite so sure anymore.