&& (set_by_optional || arg->expr->rank != rank)
&& !(isym && isym->id == GFC_ISYM_CONVERSION))
{
- gfc_warning (OPT_Wpedantic,
- "%qs at %L is an array and OPTIONAL; IF IT IS "
- "MISSING, it cannot be the actual argument of an "
- "ELEMENTAL procedure unless there is a non-optional "
- "argument with the same rank (12.4.1.5)",
- arg->expr->symtree->n.sym->name, &arg->expr->where);
+ bool t = false;
+ gfc_actual_arglist *a;
+
+ /* Scan the argument list for a non-optional argument with the
+ same rank as arg. */
+ for (a = arg0; a; a = a->next)
+ if (a != arg
+ && a->expr->rank == arg->expr->rank
+ && !a->expr->symtree->n.sym->attr.optional)
+ {
+ t = true;
+ break;
+ }
+
+ if (!t)
+ gfc_warning (OPT_Wpedantic,
+ "%qs at %L is an array and OPTIONAL; If it is not "
+ "present, then it cannot be the actual argument of "
+ "an ELEMENTAL procedure unless there is a non-optional"
+ " argument with the same rank "
+ "(Fortran 2018, 15.5.2.12)",
+ arg->expr->symtree->n.sym->name, &arg->expr->where);
}
}
integer, optional :: arg1(:)
integer :: arg2(:)
! print *, fun1 (arg1, arg2)
- if (size (fun1 (arg1, arg2)) /= 2) STOP 1 ! { dg-warning "is an array and OPTIONAL" }
- if (any (fun1 (arg1, arg2) /= [1,2])) STOP 2 ! { dg-warning "is an array and OPTIONAL" }
+ if (size (fun1 (arg1, arg2)) /= 2) STOP 1
+ if (any (fun1 (arg1, arg2) /= [1,2])) STOP 2
end subroutine
elemental function fun1 (arg1, arg2)
--- /dev/null
+! { dg-do compile }
+! { dg-options "-pedantic-errors" }
+!
+! Contributed by Martin Diehl <m.diehl@mpie.de>
+
+program elemental_optional
+ implicit none
+ integer :: m(5), r(5)
+
+ m = 1
+
+ r = outer()
+ r = outer(m)
+
+ contains
+
+ function outer(o) result(l)
+ integer, intent(in), optional :: o(:)
+ integer :: u(5), l(5)
+
+ l = inner(o,u)
+
+ end function outer
+
+ elemental function inner(a,b) result(x)
+ integer, intent(in), optional :: a
+ integer, intent(in) :: b
+ integer :: x
+
+ if(present(a)) then
+ x = a*b
+ else
+ x = b
+ endif
+ end function inner
+
+end program elemental_optional
+