|| gfc_expr_attr (expr).pointer))
goto scalarize;
+ /* With array-bounds checking enabled, force scalarization in some
+ situations, e.g., when an array index depends on a function
+ evaluation or an expression and possibly has side-effects. */
+ if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
+ && ref
+ && ref->u.ar.type == AR_SECTION)
+ {
+ for (n = 0; n < ref->u.ar.dimen; n++)
+ if (ref->u.ar.dimen_type[n] == DIMEN_ELEMENT
+ && ref->u.ar.start[n])
+ {
+ switch (ref->u.ar.start[n]->expr_type)
+ {
+ case EXPR_FUNCTION:
+ case EXPR_OP:
+ goto scalarize;
+ }
+ }
+ }
+
if (!(gfc_bt_struct (expr->ts.type)
|| expr->ts.type == BT_CLASS)
&& ref && ref->next == NULL
--- /dev/null
+! { dg-do run }
+! { dg-additional-options "-fcheck=bounds -fdump-tree-original" }
+!
+! PR fortran/117791 - crash with bounds check writing array section
+! Contributed by Andreas van Hameren (hameren at ifj dot edu dot pl)
+
+program testprogram
+ implicit none
+ integer, parameter :: array(4,2)=reshape ([11,12,13,14 ,15,16,17,18], [4,2])
+ integer :: i(3) = [45,51,0]
+
+ write(*,*) 'line 1:',array(:, sort_2(i(1:2)) )
+ write(*,*) 'line 2:',array(:, 3 - sort_2(i(1:2)) )
+ write(*,*) 'line 3:',array(:, int (3 - sort_2(i(1:2))))
+
+contains
+
+ function sort_2(i) result(rslt)
+ integer,intent(in) :: i(2)
+ integer :: rslt
+ if (i(1) <= i(2)) then
+ rslt = 1
+ else
+ rslt = 2
+ endif
+ end function
+
+end program
+
+! { dg-final { scan-tree-dump-times "sort_2" 5 "original" } }
+! { dg-final { scan-tree-dump-not "_gfortran_transfer_array_write" "original" } }