diff mbox series

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

Message ID CAGkQGiLyCitnePwtiEucjcAhK8q_PBKuSdsvUpjKkE3iDVu06A@mail.gmail.com
State New
Headers show
Series [fortran] Fix default type bugs in gfortran [PR99139, PR99368] | expand

Commit Message

Paul Richard Thomas July 8, 2023, 2:23 p.m. UTC
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  <sgk@troutmask.apl.washington.edu>

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  <sgk@troutmask.apl.washington.edu>

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

Comments

Harald Anlauf July 8, 2023, 2:46 p.m. UTC | #1
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  <sgk@troutmask.apl.washington.edu>
> 
> 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  <sgk@troutmask.apl.washington.edu>
> 
> 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
Li, Pan2 via Gcc-patches July 8, 2023, 2:57 p.m. UTC | #2
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  <sgk@troutmask.apl.washington.edu>

kargl@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  <sgk@troutmask.apl.washington.edu>

kargl@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.
diff mbox series

Patch

diff --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.  */
 
   /* Determine if the symbol may not have an initializer.  */