https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107874
kargl at gcc dot gnu.org changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |kargl at gcc dot gnu.org --- Comment #2 from kargl at gcc dot gnu.org --- John thanks for posting the bug. Harald, you are likely right the patch can be moved down. I'll programmed up the example from the Fortran 2018 standard, which works as expected. So, there is definitely something about a scalar mask choosing the actual argument before both are evaluated. program foo call bah contains subroutine bah logical, parameter :: t = .true., f = .false. logical, parameter :: mask(2,3)= reshape([t, f, f, f, t, t], [2,3]) integer tsrc(2,3), fsrc(2,3), res(2,3) ! ! Direct reference to merge ! tsrc = reshape([1, 2, 6, 4, 5, 6], [2,3]) fsrc = reshape([0, 7, 3, 4, 2, 8], [2,3]) res = 42 res = merge(tsrc, fsrc, mask) write(*,'(*(I0,1X))') res(1,:) ! Should be 1 3 5 write(*,'(*(I0,1X))') res(2,:) ! Should be 7 4 6 write(*,*) ! ! Load matrices via a function. ! res = 0 ! Clear res = merge(load('t'), load('f'), mask) ! Clear write(*,'(*(I0,1X))') res(1,:) ! Should be 1 3 5 write(*,'(*(I0,1X))') res(2,:) ! Should be 7 4 6 end subroutine bah function load(c) result(r) integer r(2,3) character, intent(in) :: c if (c == 't') then r = reshape([1, 2, 6, 4, 5, 6], [2,3]) print *, 'loaded t' else if (c == 'f') then r = reshape([0, 7, 3, 4, 2, 8], [2,3]) print *, 'loaded f' else stop 'whoops' end if end function load end program foo