Re: [PATCH] Fortran: simplification of FINDLOC for constant complex arguments [PR110585]

2023-07-08 Thread Paul Richard Thomas via Fortran
Hi Harald,

This is indeed obvious :-)

Thanks for the patch.

Paul

On Fri, 7 Jul 2023 at 19:32, Harald Anlauf via Fortran
 wrote:
>
> Dear all,
>
> I intend to commit the attached obvious patch within 24h unless
> someone objects.  gfc_compare_expr() did not handle the case of
> complex constants, which may be compared for equality.  This
> case is needed in the simplification of the FINDLOC intrinsic.
>
> Regtested on x86_64-pc-linux-gnu.
>
> Thanks,
> Harald
>


-- 
"If you can't explain it simply, you don't understand it well enough"
- Albert Einstein


Re: [PATCH] Fortran: fixes for procedures with ALLOCATABLE,INTENT(OUT) arguments [PR92178]

2023-07-08 Thread Mikael Morin

Hello,

Le 07/07/2023 à 20:23, Harald Anlauf a écrit :

Hi Mikael,

Am 07.07.23 um 14:21 schrieb Mikael Morin:

I'm attaching what I have (lightly) tested so far, which doesn't work.
It seems gfc_conv_class_to_class reevaluates part of the original
expression, which is not correct after deallocation.


this looks much more elegant than my attempt that passed an additional
argument to gfc_conv_class_to_class, to achieve what your patch does.


Will have a look again tonight.


Great.

Harald



here is what I'm finally coming to.  This patch fixes my example, but is 
otherwise untested.
The patch has grown enough that I'm tempted to fix my example 
separately, in its own commit.


Mikaeldiff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index e7c51bae052..1c2af55d436 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -3271,6 +3271,7 @@ gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
   gfc_add_block_to_block (block, &se.pre);
   info->descriptor = se.expr;
   ss_info->string_length = se.string_length;
+  ss_info->class_container = se.class_container;
 
   if (base)
 {
@@ -7687,6 +7688,8 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr)
 	  else if (deferred_array_component)
 	se->string_length = ss_info->string_length;
 
+	  se->class_container = ss_info->class_container;
+
 	  gfc_free_ss_chain (ss);
 	  return;
 	}
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index ebef1a36577..01386bceaeb 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -529,24 +529,10 @@ gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
 }
 
 
-/* Reset the vptr to the declared type, e.g. after deallocation.  */
-
-void
-gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
+static void
+reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_expr)
 {
-  gfc_symbol *vtab;
-  tree vptr;
-  tree vtable;
-  gfc_se se;
-
-  /* Evaluate the expression and obtain the vptr from it.  */
-  gfc_init_se (&se, NULL);
-  if (e->rank)
-gfc_conv_expr_descriptor (&se, e);
-  else
-gfc_conv_expr (&se, e);
-  gfc_add_block_to_block (block, &se.pre);
-  vptr = gfc_get_vptr_from_expr (se.expr);
+  tree vptr = gfc_get_vptr_from_expr (class_expr);
 
   /* If a vptr is not found, we can do nothing more.  */
   if (vptr == NULL_TREE)
@@ -556,6 +542,9 @@ gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
 gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
   else
 {
+  gfc_symbol *vtab;
+  tree vtable;
+
   /* Return the vptr to the address of the declared type.  */
   vtab = gfc_find_derived_vtab (e->ts.u.derived);
   vtable = vtab->backend_decl;
@@ -568,6 +557,24 @@ gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
 }
 
 
+/* Reset the vptr to the declared type, e.g. after deallocation.  */
+
+void
+gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
+{
+  gfc_se se;
+
+  /* Evaluate the expression and obtain the vptr from it.  */
+  gfc_init_se (&se, NULL);
+  if (e->rank)
+gfc_conv_expr_descriptor (&se, e);
+  else
+gfc_conv_expr (&se, e);
+  gfc_add_block_to_block (block, &se.pre);
+  reset_vptr (block, e, se.expr);
+}
+
+
 /* Reset the len for unlimited polymorphic objects.  */
 
 void
@@ -1266,6 +1273,8 @@ gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
 
   slen = build_zero_cst (size_type_node);
 }
+  else if (parmse->class_container != NULL_TREE)
+tmp = parmse->class_container;
   else
 {
   /* Remove everything after the last class reference, convert the
@@ -3078,6 +3087,11 @@ gfc_conv_variable (gfc_se * se, gfc_expr * expr)
 	  return;
 	}
 
+  if (sym->ts.type == BT_CLASS
+	  && sym->attr.class_ok
+	  && sym->ts.u.derived->attr.is_class)
+	se->class_container = se->expr;
+
   /* Dereference the expression, where needed.  */
   se->expr = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
 	is_classarray);
@@ -3135,6 +3149,15 @@ gfc_conv_variable (gfc_se * se, gfc_expr * expr)
 	conv_parent_component_references (se, ref);
 
 	  gfc_conv_component_ref (se, ref);
+
+	  if (ref->u.c.component->ts.type == BT_CLASS
+	  && ref->u.c.component->attr.class_ok
+	  && ref->u.c.component->ts.u.derived->attr.is_class)
+	se->class_container = se->expr;
+	  else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
+		 && ref->u.c.sym->attr.is_class))
+	se->class_container = NULL_TREE;
+		
 	  if (!ref->next && ref->u.c.sym->attr.codimension
 	  && se->want_pointer && se->descriptor_only)
 	return;
@@ -6784,6 +6807,21 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 		  stmtblock_t block;
 		  tree ptr;
 
+		  /* In case the data reference to deallocate is dependent on
+		 its own content, save the resulting pointer to a variable
+		 and only use that variable from now on, before the
+		 expression becomes invalid.  */
+		  tree t = gfc_build_addr_expr 

Re: [PATCH] Fortran: fixes for procedures with ALLOCATABLE,INTENT(OUT) arguments [PR92178]

2023-07-08 Thread Harald Anlauf via Fortran

Hi Mikael,

Am 08.07.23 um 14:07 schrieb Mikael Morin:

here is what I'm finally coming to.  This patch fixes my example, but is
otherwise untested.
The patch has grown enough that I'm tempted to fix my example
separately, in its own commit.


alright.  I've interpreted this as a green light for v2 of my patch
and pushed it as r14-2395-gb1079fc88f082d

https://gcc.gnu.org/g:b1079fc88f082d3c5b583c8822c08c5647810259

so that you can build upon it.


Mikael


Thanks,
Harald



[Patch, fortran] Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08 Thread Paul Richard Thomas via Fortran
The attached patch incorporates two of Steve's "Orphaned Patches" -
https://gcc.gnu.org/pipermail/fortran/2023-June/059423.html

They have in common that they both involve faults in use of default
type and that I was the ultimate cause of the bugs.

The patch regtests with the attached testcases.

I will commit in the next 24 hours unless there are any objections.

Paul

Fortran: Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08  Steve Kargl  

gcc/fortran
PR fortran/99139
PR fortran/99368
* match.cc (gfc_match_namelist): Check for host associated or
defined types before applying default type.
(gfc_match_select_rank): Apply default type to selector of
unlnown type if possible.
* resolve.cc (resolve_fl_variable): Do not apply local default
initialization to assumed rank entities.

gcc/testsuite/
PR fortran/999139
* gfortran.dg/pr99139.f90 : New test

PR fortran/99368
* gfortran.dg/pr99368.f90 : New test

Fortran: Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08  Steve Kargl  

gcc/fortran
PR fortran/99139
PR fortran/99368
* match.cc (gfc_match_namelist): Check for host associated or
defined types before applying default type.
(gfc_match_select_rank): Apply default type to selector of
unlnown type if possible.
* resolve.cc (resolve_fl_variable): Do not apply local default
initialization to assumed rank entities.

gcc/testsuite/
PR fortran/999139
* gfortran.dg/pr99139.f90 : New test

PR fortran/99368
* gfortran.dg/pr99368.f90 : New test
! { dg-do compile }
! { dg-options "-finit-local-zero" }
!
! Contributed by Gerhard Steinmetz  
!
! Original implicitly typed 'x' gave a bad symbol ICE
subroutine s1(x)
   target :: x(..)
   select rank (y => x)
   rank (1)
   rank (2)
   end select
end

! Comment #2: Failed with above option
subroutine s2(x, z)
   real, target :: x(..)
   real :: z(10)
   select rank (y => x) ! Error was:Assumed-rank variable y at (1) may only be
! used as actual argument
   rank (1)
   rank (2)
   end select
end
! { dg-do compile }
!
! Contributed by Gerhard Steinmetz  
!
program p
   type y ! { dg-error "Derived type" }
   end type
contains
   subroutine s1
  namelist /x/ y ! { dg-error "conflicts with namelist object" }
  character(3) y
   end
   subroutine s2
  namelist /z/ y ! { dg-error "conflicts with namelist object" }
  character(3) y
   end
enddiff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index ca64e59029e..a778bae0b9f 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -5622,10 +5622,32 @@ gfc_match_namelist (void)
 		  gfc_error_check ();
 		}
 	  else
-		/* If the type is not set already, we set it here to the
-		   implicit default type.  It is not allowed to set it
-		   later to any other type.  */
-		gfc_set_default_type (sym, 0, gfc_current_ns);
+		{
+		  /* Before the symbol is given an implicit type, check to
+		 see if the symbol is already available in the namespace,
+		 possibly through host association.  Importantly, the
+		 symbol may be a user defined type.  */
+
+		  gfc_symbol *tmp;
+
+		  gfc_find_symbol (sym->name, NULL, 1, &tmp);
+		  if (tmp
+		  && tmp->attr.generic
+		  && (tmp = gfc_find_dt_in_generic (tmp)))
+		{
+		  if (tmp->attr.flavor == FL_DERIVED)
+			{
+			  gfc_error ("Derived type %qs at %L conflicts with "
+ "namelist object %qs at %C",
+ tmp->name, &tmp->declared_at, sym->name);
+			  goto error;
+			}
+		}
+
+		  /* Set type of the symbol to its implicit default type.  It is
+		 not allowed to set it later to any other type.  */
+		  gfc_set_default_type (sym, 0, gfc_current_ns);
+		}
 	}
 	  if (sym->attr.in_namelist == 0
 	  && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
@@ -6805,8 +6827,20 @@ gfc_match_select_rank (void)
 
   gfc_current_ns = gfc_build_block_ns (ns);
   m = gfc_match (" %n => %e", name, &expr2);
+
   if (m == MATCH_YES)
 {
+  /* If expr2 corresponds to an implicitly typed variable, then the
+	 actual type of the variable may not have been set.  Set it here.  */
+  if (!gfc_current_ns->seen_implicit_none
+	  && expr2->expr_type == EXPR_VARIABLE
+	  && expr2->ts.type == BT_UNKNOWN
+	  && expr2->symtree && expr2->symtree->n.sym)
+	{
+	  gfc_set_default_type (expr2->symtree->n.sym, 0, gfc_current_ns);
+	  expr2->ts.type = expr2->symtree->n.sym->ts.type;
+	}
+
   expr1 = gfc_get_expr ();
   expr1->expr_type = EXPR_VARIABLE;
   expr1->where = expr2->where;
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 8e018b6e7e8..f7cfdfc133f 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -13510,7 +13510,8 @@ resolve_fl_variable (gfc_symbol *sym, int mp_flag)
 	}
 }
 
-  if (sym->value == NULL && sym->attr.referenced)
+  if (sym->value == NULL && sym->attr.referenced
+  && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
 apply_default_init_local (sym); /* Try to apply a default initialization.  */
 
   /* Det

Re: [Patch, fortran] Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08 Thread Harald Anlauf via Fortran

Hi Paul,

thanks for taking this.

I have just a minor comment regards coding style:

+ if (tmp
+ && tmp->attr.generic
+ && (tmp = gfc_find_dt_in_generic (tmp)))
+   {
+ if (tmp->attr.flavor == FL_DERIVED)

My reading of the guidelines says that I should rather write

  if (tmp && tmp->attr.generic)
{
  tmp = gfc_find_dt_in_generic (tmp);
  if (tmp && tmp->attr.flavor == FL_DERIVED)

Both variants are equally readable, though.

I haven't though long enough about possible minor memleaks,
i.e. if a freeing of gfc_symbol tmp is advised.
Running f951 under valgrind might give you a hint.

Thanks,
Harald


Am 08.07.23 um 16:23 schrieb Paul Richard Thomas via Gcc-patches:

The attached patch incorporates two of Steve's "Orphaned Patches" -
https://gcc.gnu.org/pipermail/fortran/2023-June/059423.html

They have in common that they both involve faults in use of default
type and that I was the ultimate cause of the bugs.

The patch regtests with the attached testcases.

I will commit in the next 24 hours unless there are any objections.

Paul

Fortran: Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08  Steve Kargl  

gcc/fortran
PR fortran/99139
PR fortran/99368
* match.cc (gfc_match_namelist): Check for host associated or
defined types before applying default type.
(gfc_match_select_rank): Apply default type to selector of
unlnown type if possible.
* resolve.cc (resolve_fl_variable): Do not apply local default
initialization to assumed rank entities.

gcc/testsuite/
PR fortran/999139
* gfortran.dg/pr99139.f90 : New test

PR fortran/99368
* gfortran.dg/pr99368.f90 : New test

Fortran: Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08  Steve Kargl  

gcc/fortran
PR fortran/99139
PR fortran/99368
* match.cc (gfc_match_namelist): Check for host associated or
defined types before applying default type.
(gfc_match_select_rank): Apply default type to selector of
unlnown type if possible.
* resolve.cc (resolve_fl_variable): Do not apply local default
initialization to assumed rank entities.

gcc/testsuite/
PR fortran/999139
* gfortran.dg/pr99139.f90 : New test

PR fortran/99368
* gfortran.dg/pr99368.f90 : New test




Re: [Patch, fortran] Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08 Thread Steve Kargl via Fortran
On Sat, Jul 08, 2023 at 03:23:31PM +0100, Paul Richard Thomas wrote:
> The attached patch incorporates two of Steve's "Orphaned Patches" -
> https://gcc.gnu.org/pipermail/fortran/2023-June/059423.html

Thanks Paul for picking up the pieces I left behind.

A few nits below.

> They have in common that they both involve faults in use of default
> type and that I was the ultimate cause of the bugs.
> 
> The patch regtests with the attached testcases.
> 
> I will commit in the next 24 hours unless there are any objections.
> 
> Paul
> 
> Fortran: Fix default type bugs in gfortran [PR99139, PR99368]
> 
> 2023-07-08  Steve Kargl  

ka...@gcc.gnu.org.

> gcc/fortran
> PR fortran/99139
> PR fortran/99368
> * match.cc (gfc_match_namelist): Check for host associated or
> defined types before applying default type.
> (gfc_match_select_rank): Apply default type to selector of
> unlnown type if possible.

s/unlnown/unknown

> * resolve.cc (resolve_fl_variable): Do not apply local default
> initialization to assumed rank entities.
> 
> gcc/testsuite/
> PR fortran/999139
> * gfortran.dg/pr99139.f90 : New test
> 
> PR fortran/99368
> * gfortran.dg/pr99368.f90 : New test
> 
> Fortran: Fix default type bugs in gfortran [PR99139, PR99368]
> 
> 2023-07-08  Steve Kargl  

ka...@gcc.gnu.org

> 
> gcc/fortran
> PR fortran/99139
> PR fortran/99368
> * match.cc (gfc_match_namelist): Check for host associated or
> defined types before applying default type.
> (gfc_match_select_rank): Apply default type to selector of
> unlnown type if possible.

s/unlnown/unknown

Other than the nits the patch looks fine.

-- 
Steve


Re: [Patch, fortran] Fix default type bugs in gfortran [PR99139, PR99368]

2023-07-08 Thread Paul Richard Thomas via Fortran
Hi Harald,

I don't believe that a memory leak is possible since tmp is only
non-null if it points to an existing symbol. I agree with you about
the style but have to plead innocence because I am not the author :-)
I will change it though.

Thanks for the nit, Steve.

Pushed as r14-2397-g9a2eab6172a8067e2f63e0fa2bcd5b2190656303

Paul

On Sat, 8 Jul 2023 at 15:46, Harald Anlauf  wrote:
>
> Hi Paul,
>
> thanks for taking this.
>
> I have just a minor comment regards coding style:
>
> + if (tmp
> + && tmp->attr.generic
> + && (tmp = gfc_find_dt_in_generic (tmp)))
> +   {
> + if (tmp->attr.flavor == FL_DERIVED)
>
> My reading of the guidelines says that I should rather write
>
>if (tmp && tmp->attr.generic)
>  {
>tmp = gfc_find_dt_in_generic (tmp);
>if (tmp && tmp->attr.flavor == FL_DERIVED)
>
> Both variants are equally readable, though.
>
> I haven't though long enough about possible minor memleaks,
> i.e. if a freeing of gfc_symbol tmp is advised.
> Running f951 under valgrind might give you a hint.
>
> Thanks,
> Harald
>
>
> Am 08.07.23 um 16:23 schrieb Paul Richard Thomas via Gcc-patches:
> > The attached patch incorporates two of Steve's "Orphaned Patches" -
> > https://gcc.gnu.org/pipermail/fortran/2023-June/059423.html
> >
> > They have in common that they both involve faults in use of default
> > type and that I was the ultimate cause of the bugs.
> >
> > The patch regtests with the attached testcases.
> >
> > I will commit in the next 24 hours unless there are any objections.
> >
> > Paul
> >
> > Fortran: Fix default type bugs in gfortran [PR99139, PR99368]
> >
> > 2023-07-08  Steve Kargl  
> >
> > gcc/fortran
> > PR fortran/99139
> > PR fortran/99368
> > * match.cc (gfc_match_namelist): Check for host associated or
> > defined types before applying default type.
> > (gfc_match_select_rank): Apply default type to selector of
> > unlnown type if possible.
> > * resolve.cc (resolve_fl_variable): Do not apply local default
> > initialization to assumed rank entities.
> >
> > gcc/testsuite/
> > PR fortran/999139
> > * gfortran.dg/pr99139.f90 : New test
> >
> > PR fortran/99368
> > * gfortran.dg/pr99368.f90 : New test
> >
> > Fortran: Fix default type bugs in gfortran [PR99139, PR99368]
> >
> > 2023-07-08  Steve Kargl  
> >
> > gcc/fortran
> > PR fortran/99139
> > PR fortran/99368
> > * match.cc (gfc_match_namelist): Check for host associated or
> > defined types before applying default type.
> > (gfc_match_select_rank): Apply default type to selector of
> > unlnown type if possible.
> > * resolve.cc (resolve_fl_variable): Do not apply local default
> > initialization to assumed rank entities.
> >
> > gcc/testsuite/
> > PR fortran/999139
> > * gfortran.dg/pr99139.f90 : New test
> >
> > PR fortran/99368
> > * gfortran.dg/pr99368.f90 : New test
>


-- 
"If you can't explain it simply, you don't understand it well enough"
- Albert Einstein