https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119948

--- Comment #2 from kargls at comcast dot net ---
(In reply to Damian Rouson from comment #0)
>
> % gfortran -c source-allocate-pure-function-result-component.f90 
> source-allocate-pure-function-result.f90:17:20:
> 
>    17 |     allocate(test%i, source = 0)
>       |                    1
> Error: Bad allocate-object at (1) for a PURE procedure

This one is interesting.  The error checks for an impure variable,
but it is not set up to deal with result-name.

>From match.cc(lines 4684-4689):

      bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
      if (impure && gfc_pure (NULL))
        {
          gfc_error ("Bad allocate-object at %C for a PURE procedure");
          goto cleanup;
        }

The function gfc_impure_variable is contained in resolve.cc.  The result-name
is not marked as pure (, yet?), so the function returns true.

(gdb) p tail->expr->symtree->n.sym->attr.result
$9 = 1
(gdb) p tail->expr->symtree->n.sym->attr.pure
$10 = 0

(gdb) p tail->expr->symtree->n.sym->ns->proc_name->name
$13 = 0x804e174b0 "construct_test"
(gdb) p tail->expr->symtree->n.sym->ns->proc_name->result->name
$15 = 0x804e15198 "test"

So, this patch seems to do the right things.

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index f03708efef7..18bc0254cb3 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -18460,6 +18460,17 @@ gfc_impure_variable (gfc_symbol *sym)
   if (sym->attr.use_assoc || sym->attr.in_common)
     return 1;

+  /* If sym is for a result-name, then its pure attribute may not be set.
+     Check the namespace to see if this a procedure, and if it is a 
+     pure function.  FIXME: what about elemental?  */
+  if (sym->attr.result && !sym->attr.pure)
+    {
+      if (sym->ns->proc_name
+         && sym->ns->proc_name->attr.pure
+         && sym->ns->proc_name->attr.function)
+       return 0;
+    }
+
   /* Check if the symbol's ns is inside the pure procedure.  */
   for (ns = gfc_current_ns; ns; ns = ns->parent)
     {

Reply via email to