From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6BD733858D33; Thu, 20 Apr 2023 05:23:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6BD733858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681968180; bh=6RU6LN0g6oEdnV/BqBF5qmrL2xIElYOcxLme0O/TSY8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=TTxqUG7HHVZ3xb40QcH+ay4jcOR6YRxTegGH6QlrhHKXKRXjOz5KocPP2OkQutOHA 9QMPDQPhT3OopRU3fmqODOUQ8mAuKrTxBytWTU4T+On3iR53FvaCkpKFGXWWZBT2QZ Wv4BGS9ghLmixPn2LQ/ypwmtpNbXrORt1eKlIhfk= From: "kargl at gcc dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: accepts-invalid, diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: kargl at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P5 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109500 --- 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. >=20 > Agree. >=20 > > Now, with the original code, > >=20 > > is_allocated(f()) > >=20 > > 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. >=20 > Agree. See also the discussion on the J3 mailing list. >=20 > > 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. >=20 > Agree. >=20 > > If we go back to the original code and modify to allocate > > f by say doing 'f =3D 42' in f(), gfortran produces=20 > >=20 > > % gfcx -o z -Wall a.f90 && ./z > > T > >=20 > > This is the problem. Yes, f is allocated and assigned=20 > > 42. The printed 'T' is bogus because 42 is value of > > the function. 42 is no allocatable. >=20 > Agree again. >=20 > The point is that there is a bug in gfortran which currently effectively > generates code which resembles >=20 > integer, allocatable :: p > p =3D f() > print *, is_allocated(p) > deallocate (p) >=20 > (of course with a temporary for the function result). > The technical reason for the crash is the copying of the function > (non)result. >=20 > The patch in comment#9 rejects all related misuses. >=20 > Given the lengthy thread on the J3 mailing list, I am wondering whether t= here > ever was an explicit IR on the issue, or was it considered so obvious that > the clarification was deferred to the F2018 document. >=20 >=20 >=20 > > One place to possibly check for an error is when=20 > > gfortran resolves argument association. If a dummy > > argument is allocatable, the actual argument needs=20 > > 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=20 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 =3D=3D 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 =3D false; + goto match; + } + /* Check intent =3D OUT/INOUT for definable actual argument. */ if (!in_statement_function && (f->sym->attr.intent =3D=3D 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=