From patchwork Tue Sep 14 19:04:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 44995 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 190523857C66 for ; Tue, 14 Sep 2021 19:04:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 190523857C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1631646297; bh=nUe390K8iMl8uax5LpS7mDlVoZH5Wmh/CWOlm2nRnew=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=yOgEJ0podzWkeZBhn5qANkPBsvV0YHc1eiKxHC3i9EJrtdv9jM09CgUBe1Oxyzgfe ARyZHcM2TLvoGtNi9Li4UG9xlR1ZlGKa+vB2Q/XIHbU9H4mI4+/KRe0DwtTQqkeQAr JX4zkoUiRG2CVC1PXqM1ncrzn68qailgP412p90U= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id B42723858402; Tue, 14 Sep 2021 19:04:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B42723858402 X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [79.251.7.109] ([79.251.7.109]) by web-mail.gmx.net (3c-app-gmx-bap28.server.lan [172.19.172.98]) (via HTTP); Tue, 14 Sep 2021 21:04:09 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] PR fortran/102287 - optional allocatable array arguments (intent out) of derived types with allocatable components are not properly passed to subroutines Date: Tue, 14 Sep 2021 21:04:09 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:PcYXSH8ZkDMvzd8p78+48QmHSXflueONIAgG2P9yBcKcKDNoBBhX3KobKVFhW2KIhliw5 t+sIHwBjnyT4NP65qmmOQhndgv20gCy0F3nDWQak0595zQuzNBHDjWW+GmRwznV+Wl2WqKNFlGH3 JbgOefNqxmE+ibcvdFtNfbDAnKSbXvFAG2wajkeqp/8IlFMdUsEzF15UlORVXeSOEjmXgIeDwEJh 4Yn3UYeSelbk2gJMuxXSq7KxIElBb3qTiOcM88eSkZn0L04VB2lTRina2q2qeqQbuDhVXxMaTSvZ ao= X-UI-Out-Filterresults: notjunk:1;V03:K0:gqzRO5O5H5U=:coe2JNSvJlCu8QimIpaRgS s/X/7W5YfFSKrI4TwkCGHyx9kION095HWfllmY0J7UfLtsjpu2a0fb3P/0qUzuqLod7w7kNb7 1Cl6owPrWajEaFGf5kmbtwRFoQh5YGuoIuMzKk1DvGYprYjZDUmScwrciG5MBV6NPUrFQDeWS 5XHfkUPFTlnRq9sJSXTuS3bQTsUFKV3yLfGITF4RaOb7vuKp1KdTLWzyZtWNl/rZ6HpPZn8Yg kTwDS+TTYU8hXlMLwLj2opIJARve3TIjQaUJXHjospIYCfcHYA8Zzayj8Im+bkh44HCi7mwSU W6rYR0thea1b5KEkWzROJoyhoPo8/3VpP1hDYquQCCpRtv/A/UXIcxt8tnG2etJV7XQYiLxsO HnhuCA0RNvL1noAkpcl784yNoMFSHWWt9ZEaRGk3IX+GIF3jMG7ftE18Sn9LWlW8TdP0ea5q6 O5JcFff0U+My4esjZfHtefBvRoNzGLDHU/EcvrSJsguQYuAvUNkHHL8pajKJf0VhXHeGBXm8X 4GGOwDVj+Kx8VYYDuTIy0ZNBQw1EyZYsIlAmIUwcN8fZLAP5/uDNADAF104dSzQQdg4lGw9NI K7/8ZinQKpPfNRSQiDJ1rPUDrwjLyP5m7qHY6BYfzefwmw7pY4QSXn1FzvFDHiCryNQQSEHA7 OwHhrzcVElv3rnz1sdOETVca/PhLbv/XEHHOg75MvF5j1syUzn7q30mweeIcWPEK4SqGvxLTN ftsUxlSIGGusYlcaCMzAjOILzViIX4O9JRbOxAqYcvjex+owXSGY/UuPSixyLDKqHGzMcYNPA Gz1Rf4+ X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" As nicely described in the PR, we mishandled the case of passing optional allocatable DT arguments with allocatable components when the INTENT was declared as INTENT(OUT), as we unconditionally tried to deallocate these components even when the argument was not present. The obvious solution is to wrap the code for deallocation by a check for presence. Regtested on x86_64-pc-linux-gnu. OK for mainline? As this is a potentially nasty wrong-code bug, I'd like to backport to at least 11-branch. Thanks, Harald Fortran - fix handling of optional allocatable DT arguments with INTENT(OUT) gcc/fortran/ChangeLog: PR fortran/102287 * trans-expr.c (gfc_conv_procedure_call): Wrap deallocation of allocatable components of optional allocatable derived type procedure arguments with INTENT(OUT) into a presence check. gcc/testsuite/ChangeLog: PR fortran/102287 * gfortran.dg/intent_out_14.f90: New test. diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index 18d665192f0..4a81f4695d9 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -6548,6 +6548,17 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, // deallocate the components first tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived, parmse.expr, e->rank); + /* But check whether dummy argument is optional. */ + if (tmp != NULL_TREE + && fsym->attr.optional + && e->expr_type == EXPR_VARIABLE + && e->symtree->n.sym->attr.optional) + { + tree present; + present = gfc_conv_expr_present (e->symtree->n.sym); + tmp = build3_v (COND_EXPR, present, tmp, + build_empty_stmt (input_location)); + } if (tmp != NULL_TREE) gfc_add_expr_to_block (&se->pre, tmp); } diff --git a/gcc/testsuite/gfortran.dg/intent_out_14.f90 b/gcc/testsuite/gfortran.dg/intent_out_14.f90 new file mode 100644 index 00000000000..e5994635008 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/intent_out_14.f90 @@ -0,0 +1,24 @@ +! { dg-do run } +! PR fortran/102287 - optional allocatable DT array arguments (intent out) + +module m + type t + integer, allocatable :: a + end type t +contains + subroutine a (x, v) + type(t), optional, allocatable, intent(out) :: x(:) + type(t), optional, intent(out) :: v(:) + call b (x, v) + end subroutine a + + subroutine b (y, w) + type(t), optional, allocatable, intent(out) :: y(:) + type(t), optional, intent(out) :: w(:) + end subroutine b +end module m + +program p + use m + call a () +end