http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53692
Bug #: 53692 Summary: OPTIONAL: Scalarizing over the wrong array Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran AssignedTo: unassig...@gcc.gnu.org ReportedBy: bur...@gcc.gnu.org CC: mik...@gcc.gnu.org Blocks: 50981 The following program of Daniel C Chen fails due to invalid OPTIONAL handling: The scalarizer uses the first array instead of the first array belonging to a nonoptional argument. Cf. http://j3-fortran.org/pipermail/j3/2012-June/005356.html See also: 12.5.2.12p3(6), "An optional dummy argument that is not present is subject to the following restrictions. ... "(6) If it is an array, it shall not be supplied as an actual argument to an elemental procedure unless an array of the same rank is supplied as an actual argument corresponding to a nonoptional dummy argument of that elemental procedure." Untested draft patch. I think one might need to have a fall back if there is no actual argument (which is an array) belonging to a nonoptional dummy. In that case (cf. above) one can assume that the first array in the list has to be present. --- a/gcc/fortran/trans-array.c +++ b/gcc/fortran/trans-array.c @@ -4356,3 +4356,4 @@ set_loop_bounds (gfc_loopinfo *loop) || ss_type == GFC_SS_TEMP - || ss_type == GFC_SS_REFERENCE) + || ss_type == GFC_SS_REFERENCE + || ss->info->can_be_null_ref) continue; Program main implicit none integer :: arr(2) arr = [ 1, 2 ] call sub1(arg2=arr) contains subroutine sub1(arg1,arg2) integer, optional :: arg1(:) integer :: arg2(:) print *,fun1 (arg1, arg2) if (size (fun1 (arg1, arg2)) /= 2) call abort() if (any (fun1 (arg1, arg2) /= [1,2])) call abort() end subroutine elemental function fun1(arg1,arg2) integer,intent(in), optional :: arg1 integer,intent(in) :: arg2 integer :: fun1 fun1 = arg2 end function end program