https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85599
Bug ID: 85599 Summary: invalid optimization: function not always evaluated in logical expression Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: janus at gcc dot gnu.org Target Milestone: --- Consider the following test case: program lazy logical :: flag flag = .false. flag = check() .and. flag ! 'check' is executed as expected flag = flag .and. check() ! bug: 'check' is not executed contains logical function check() integer, save :: i = 1 print *, "check", i i = i + 1 check = .true. end function end The problem here is that gfortran executes the function 'check' only once, although it should be executed twice AFAICS. Optimizing out the function call might be valid for a PURE function, but for an impure one with side effects, I don't think this is allowed. Both ifort and flang execute the function twice. For gfortran, the optimization level does not seem to influence the result: -O0 and -O3 behave in the same way. I tried with gfortran versions 5, 6, 7 and 8, which all exhibit the same buggy behavior.