public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kargl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/109500] SIGABRT when calling a function that returns an unallocated value
Date: Thu, 20 Apr 2023 05:22:59 +0000	[thread overview]
Message-ID: <bug-109500-4-q9pACjiVIl@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109500-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #18 from kargl at gcc dot gnu.org ---
(In reply to anlauf from comment #17)
> (In reply to Steve Kargl from comment #16)
> > First, note, 'allocated(f())' throws an error.
> 
> Agree.
> 
> > Now, with the original code,
> > 
> >       is_allocated(f())
> > 
> > The function reference is evaluated, and then the function result
> > (aka it's value) is argument associated with an allocatable dummy
> > argument.  This is technically wrong as the actual argument (aka
> > value returned by the function reference) should not have the
> > allocatable attribute.
> 
> Agree.  See also the discussion on the J3 mailing list.
> 
> > I'll repeat.  The actual argument is the value resulting from
> > the function reference.  The "shall" in "shall be allocatable"
> > applies to something the programmer must ensure.
> 
> Agree.
> 
> > If we go back to the original code and modify to allocate
> > f by say doing 'f = 42' in f(), gfortran produces 
> > 
> > % gfcx -o z -Wall a.f90 && ./z
> >  T
> > 
> > This is the problem.  Yes, f is allocated and assigned 
> > 42.  The printed 'T' is bogus because 42 is value of
> > the function.  42 is no allocatable.
> 
> Agree again.
> 
> The point is that there is a bug in gfortran which currently effectively
> generates code which resembles
> 
>   integer, allocatable :: p
>   p = f()
>   print *, is_allocated(p)
>   deallocate (p)
> 
> (of course with a temporary for the function result).
> The technical reason for the crash is the copying of the function
> (non)result.
> 
> The patch in comment#9 rejects all related misuses.
> 
> Given the lengthy thread on the J3 mailing list, I am wondering whether there
> ever was an explicit IR on the issue, or was it considered so obvious that
> the clarification was deferred to the F2018 document.
> 
> 
> 
> > One place to possibly check for an error is when 
> > gfortran resolves argument association.  If a dummy
> > argument is allocatable, the actual argument needs 
> > to be allocatable and cannot not also be a function
> > result variable.

I think we agree on all points.  Here's the diff I envision.
NOte, I've restricted it to user defined functions.  Remove 
the esym check will enable it for any subprogram.


diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index db79b104dc2..f3bcdd76d6a 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -3622,6 +3622,18 @@ gfc_compare_actual_formal (gfc_actual_arglist **ap,
gfc_formal_arglist *formal,
          goto match;
        }

+      if (a->expr->expr_type == EXPR_FUNCTION
+         && a->expr->value.function.esym
+         && f->sym->attr.allocatable)
+       {
+         if (where)
+           gfc_error ("Actual argument for %qs at %L is a function result "
+                       "and the dummy argument is ALLOCATABLE",
+                       f->sym->name, &a->expr->where);
+         ok = false;
+         goto match;
+       }
+
       /* Check intent = OUT/INOUT for definable actual argument.  */
       if (!in_statement_function
          && (f->sym->attr.intent == INTENT_OUT

This gives

% gfcx -c a.f90
a.f90:5:25:

    5 |    print *, is_allocated(f())
      |                         1
Error: Actual argument for 'p' at (1) is a function result and the dummy
argument is ALLOCATABLE

  parent reply	other threads:[~2023-04-20  5:23 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 13:24 [Bug fortran/109500] New: " leandro.lupori at linaro dot org
2023-04-13 15:05 ` [Bug fortran/109500] " kargl at gcc dot gnu.org
2023-04-13 17:06 ` leandro.lupori at linaro dot org
2023-04-13 18:12 ` kargl at gcc dot gnu.org
2023-04-13 18:32 ` anlauf at gcc dot gnu.org
2023-04-13 19:44 ` leandro.lupori at linaro dot org
2023-04-13 21:28 ` anlauf at gcc dot gnu.org
2023-04-13 21:34 ` sgk at troutmask dot apl.washington.edu
2023-04-14 19:24 ` anlauf at gcc dot gnu.org
2023-04-16 19:42 ` anlauf at gcc dot gnu.org
2023-04-17 12:14 ` leandro.lupori at linaro dot org
2023-04-17 20:20 ` anlauf at gcc dot gnu.org
2023-04-19 17:25 ` leandro.lupori at linaro dot org
2023-04-19 19:02 ` sgk at troutmask dot apl.washington.edu
2023-04-19 19:06 ` anlauf at gcc dot gnu.org
2023-04-19 19:15 ` anlauf at gcc dot gnu.org
2023-04-19 19:42 ` sgk at troutmask dot apl.washington.edu
2023-04-19 21:03 ` anlauf at gcc dot gnu.org
2023-04-20  5:22 ` kargl at gcc dot gnu.org [this message]
2023-04-20 16:29 ` sgk at troutmask dot apl.washington.edu
2023-04-20 19:02 ` anlauf at gcc dot gnu.org
2023-04-20 19:04 ` anlauf at gcc dot gnu.org
2023-04-20 20:02 ` anlauf at gcc dot gnu.org
2023-04-22 18:50 ` cvs-commit at gcc dot gnu.org
2023-04-23 18:59 ` anlauf at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109500-4-q9pACjiVIl@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).