Fortran: Fix handling of optional argument to SIZE intrinsic [PR103898]

Message ID e8d7da83-00ab-8fb0-eb87-915c52c4e85c@codesourcery.com
State New
Headers
Series Fortran: Fix handling of optional argument to SIZE intrinsic [PR103898] |

Commit Message

Sandra Loosemore Jan. 6, 2022, 8:11 p.m. UTC
  This patch fixes an ICE introduced with the recent-ish rewrite to inline 
the SIZE intrinsic, using a helper function to do the bulk of the work 
in producing the expansion.  It turns out to be a simple think-o type 
mistake in the wrapper around the helper rather than anything deeply 
wrong with the logic.

OK to check in?

-Sandra
  

Comments

Harald Anlauf Jan. 6, 2022, 9:16 p.m. UTC | #1
Hi Sandra,

Am 06.01.22 um 21:11 schrieb Sandra Loosemore:
> This patch fixes an ICE introduced with the recent-ish rewrite to inline
> the SIZE intrinsic, using a helper function to do the bulk of the work
> in producing the expansion.  It turns out to be a simple think-o type
> mistake in the wrapper around the helper rather than anything deeply
> wrong with the logic.
>
> OK to check in?

LGTM.

> -Sandra

Thanks for addressing this swiftly!
  

Patch

commit 0e5b74440572f988dd96a6e9c33c11b59323d6cf
Author: Sandra Loosemore <sandra@codesourcery.com>
Date:   Thu Jan 6 11:23:18 2022 -0800

    Fortran: Fix handling of optional argument to SIZE intrinsic [PR103898]
    
    This patch fixes a think-o in the code that triggered an ICE
    in the test case.
    
    2021-01-06  Sandra Loosemore  <sandra@codesourcery.com>
    
    	gcc/fortran/
    	* trans-intrinsic.c (gfc_conv_intrinsic_size): Make size_var
    	actually be a variable and fix surrounding code.
    
    	gcc/testsuite/
    	* gfortran.dg/pr103898.f90: New test.

diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c
index 41252c9..aae34b0 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -8006,10 +8006,14 @@  gfc_conv_intrinsic_size (gfc_se * se, gfc_expr * expr)
 	  cond = gfc_evaluate_now (cond, &se->pre);
 	  /* 'block2' contains the arg2 absent case, 'block' the arg2 present
 	      case; size_var can be used in both blocks. */
-	  tree size_var = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
+	  tree size_var = gfc_create_var (TREE_TYPE (size), "size");
 	  tmp = fold_build2_loc (input_location, MODIFY_EXPR,
 				 TREE_TYPE (size_var), size_var, size);
 	  gfc_add_expr_to_block (&block, tmp);
+	  size = gfc_tree_array_size (&block2, arg1, e, NULL_TREE);
+	  tmp = fold_build2_loc (input_location, MODIFY_EXPR,
+				 TREE_TYPE (size_var), size_var, size);
+	  gfc_add_expr_to_block (&block2, tmp);
 	  tmp = build3_v (COND_EXPR, cond, gfc_finish_block (&block),
 			  gfc_finish_block (&block2));
 	  gfc_add_expr_to_block (&se->pre, tmp);
diff --git a/gcc/testsuite/gfortran.dg/pr103898.f90 b/gcc/testsuite/gfortran.dg/pr103898.f90
new file mode 100644
index 0000000..6b4bb30
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr103898.f90
@@ -0,0 +1,15 @@ 
+! { dg-do compile }
+
+! This test used to ICE during gimplification (PR103898).
+
+Module g
+contains
+  function mysize(array, dim)
+    integer :: mysize
+    integer, dimension(:), intent(in)   :: array
+    integer, optional,     intent(in)   :: dim
+    if (present(dim)) then
+       mysize = size(array, dim=dim)
+    endif
+  end function mysize
+end module