User account creation filtered due to spam.

Bug 51758

Summary: ICE with optional arguments of elemental procedures
Product: gcc Reporter: Mikael Morin <mikael>
Component: fortranAssignee: Mikael Morin <mikael>
Status: RESOLVED FIXED    
Severity: normal CC: burnus
Priority: P3 Keywords: ice-on-valid-code
Version: 4.6.4   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2012-01-05 00:00:00

Description Mikael Morin 2012-01-05 00:46:31 UTC
Follow-up to PR50981.

gfortran chokes with the testcase below.

with 4.7:
   internal compiler error: in gfc_trans_assignment_1, at fortran/trans-expr.c:6794

The code is accepted without the elemental cases, and the null() seems to be handled correctly.


with 4.5:
  a = foo((/ 1, 1 /), null())
                      1
Error: Rank mismatch in argument 'b' at (1) (1 and 0)

The code is accepted without the non-elemental case, but the null() is mishandled.




program p

  integer :: a(2)
  integer :: b

  a = 0
  a = foo((/ 1, 1 /), null())
  print *, a
  !if (any(a /= 2)) call abort

  a = 0
  a = bar((/ 1, 1 /), null())
  print *, a
  !if (any(a /= 2)) call abort

! b = 0
! b = bar(1, null())
! print *, a
  !if (b /= 2) call abort

contains
  
  function foo(a, b)
    integer           :: a(:)
    integer, optional :: b(:)
    integer           :: foo(size(a))

    if (present(b)) call abort

    foo = 2
  end function foo

  elemental function bar(a, b)
    integer, intent(in)           :: a
    integer, intent(in), optional :: b
    integer                       :: bar

    bar = 2

    if (present(b)) bar = 1

  end function bar

end program p
Comment 1 Mikael Morin 2012-01-05 00:56:59 UTC
Related bugs (null as actual argument):
PR12841 (fixed long ago), PR44666 (fixed on 4.6)
Comment 2 Tobias Burnus 2012-01-05 08:03:28 UTC
Compiles and runs with ifort 12.1.1; compiles but fails at run time with ifort 12.0.5.

Nit: The commented
  ! print *, a
should be 
  ! print *, b
Comment 3 Tobias Burnus 2012-01-06 11:29:32 UTC
The assert
  rse.ss == gfc_ss_terminator
is not fulfilled in gfc_trans_assignment_1.

If one manually sets "rse.see = gfc_ss_terminator" in the debugger, the original dump looks fine.

Thus, it looks like some issue with the scalarizer handling.
Comment 4 Tobias Burnus 2012-01-06 15:55:36 UTC
Draft patch.

--- trans-expr.c        (revision 182957)
+++ trans-expr.c        (working copy)
@@ -3408,6 +3408,9 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol *
          parmse.expr = null_pointer_node;
          if (arg->missing_arg_type == BT_CHARACTER)
            parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
+         if (se->ss && (se->ss->info->type == GFC_SS_SCALAR
+                        || se->ss->info->type == GFC_SS_REFERENCE))
+           gfc_advance_se_ss_chain (se);
        }
       else if (fsym && fsym->ts.type == BT_CLASS
                 && e->ts.type == BT_DERIVED)
Comment 5 Mikael Morin 2012-01-09 13:43:35 UTC
I'm taking care.
Comment 6 Dominique d'Humieres 2012-01-09 13:48:14 UTC
See patch at http://gcc.gnu.org/ml/fortran/2012-01/msg00066.html .
Comment 7 Mikael Morin 2012-01-09 19:01:44 UTC
Author: mikael
Date: Mon Jan  9 19:01:34 2012
New Revision: 183024

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183024
Log:
2012-01-09  Mikael Morin  <mikael@gcc.gnu.org>

	PR fortran/51758
	* trans-array.c (gfc_walk_elemental_function_args):
	Skip over NULL() actual arguments.

2012-01-09  Tobias Burnus  <burnus@net-b.de>

	PR fortran/51758
	* gfortran.dg/optional_absent_2.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/optional_absent_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Mikael Morin 2012-07-14 15:24:17 UTC
Author: mikael
Date: Sat Jul 14 15:24:12 2012
New Revision: 189480

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189480
Log:
2012-07-14  Mikael Morin  <mikael@gcc.gnu.org>

	Backport from trunk:
	2012-01-09  Mikael Morin  <mikael@gcc.gnu.org>

	PR fortran/51758
	* trans-array.c (gfc_walk_elemental_function_args):
	Skip over NULL() actual arguments.

2012-07-14  Mikael Morin  <mikael.morin@gcc.gnu.org>

	Backport from trunk:
	2012-01-09  Tobias Burnus  <burnus@net-b.de>

	PR fortran/51758
	* gfortran.dg/optional_absent_2.f90: New.



Added:
    branches/gcc-4_6-branch/gcc/testsuite/gfortran.dg/optional_absent_2.f90
Modified:
    branches/gcc-4_6-branch/gcc/fortran/ChangeLog
    branches/gcc-4_6-branch/gcc/fortran/trans-array.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 9 Mikael Morin 2012-07-14 16:01:43 UTC
Fixed for 4.7.0 and 4.6.4.