[Fortran] Fix result-variable handling of MODULE PROCEDURE (PR94348)
authorTobias Burnus <tobias@codesourcery.com>
Sat, 28 Mar 2020 13:01:57 +0000 (14:01 +0100)
committerTobias Burnus <tobias@codesourcery.com>
Sat, 28 Mar 2020 13:01:57 +0000 (14:01 +0100)
PR fortran/94348
* decl.c (gfc_match_submod_proc): Add result var to the
proc's namespace.

PR fortran/94348
* gfortran.dg/module_procedure_3.f90: New.

gcc/fortran/ChangeLog
gcc/fortran/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/module_procedure_3.f90 [new file with mode: 0644]

index 02f0141bebfd5b098018ebed87b20d6da1cbff2c..48bf8bfc7fa2152c95f059b17d03acdc4317b313 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-28  Tobias Burnus  <tobias@codesourcery.com>
+
+       PR fortran/94348
+       * decl.c (gfc_match_submod_proc): Add result var to the
+       proc's namespace.
+
 2020-03-27  Tobias Burnus  <tobias@codesourcery.com>
 
        PR fortran/93957
index 2f458c4faac6f3a840e2fe869e2d495e13bb38db..79c951003ad5572f4604017a25b2433c05fc4df7 100644 (file)
@@ -9699,13 +9699,20 @@ gfc_match_submod_proc (void)
   if (get_proc_name (name, &sym, false))
     return MATCH_ERROR;
 
-  /* Make sure that the result field is appropriately filled, even though
-     the result symbol will be replaced later on.  */
+  /* Make sure that the result field is appropriately filled.  */
   if (sym->tlink && sym->tlink->attr.function)
     {
-      if (sym->tlink->result
-         && sym->tlink->result != sym->tlink)
-       sym->result= sym->tlink->result;
+      if (sym->tlink->result && sym->tlink->result != sym->tlink)
+       {
+         sym->result = sym->tlink->result;
+         if (!sym->result->attr.use_assoc)
+           {
+             gfc_symtree *st = gfc_new_symtree (&gfc_current_ns->sym_root,
+                                                sym->result->name);
+             st->n.sym = sym->result;
+             sym->result->refs++;
+           }
+       }
       else
        sym->result = sym;
     }
index 3422550ae023ce99340e4bef53896a76b2bcc7eb..92f161f197aef8c892c1d0b56a72a23d6a602cf5 100644 (file)
@@ -1,3 +1,8 @@
+2020-03-28  Tobias Burnus  <tobias@codesourcery.com>
+
+       PR fortran/94348
+       * gfortran.dg/module_procedure_3.f90: New.
+
 2020-03-28  Patrick Palka  <ppalka@redhat.com>
 
        PR c++/94306
diff --git a/gcc/testsuite/gfortran.dg/module_procedure_3.f90 b/gcc/testsuite/gfortran.dg/module_procedure_3.f90
new file mode 100644 (file)
index 0000000..50a83d9
--- /dev/null
@@ -0,0 +1,27 @@
+! { dg-do run }
+!
+! PR fortran/94348
+!
+! Contributed by Damian Rouson
+
+module foo_module
+  implicit none
+
+  interface
+     module function foo() result(bar)
+       implicit none
+       integer bar
+     end function
+  end interface
+
+contains
+  module procedure foo
+    bar = 5
+  end procedure
+end module
+
+program main
+  use foo_module
+  implicit none
+  if (foo() /= 5) stop 1
+end program main