From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2071) id ADE3D3858C83; Sat, 22 Apr 2023 18:50:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADE3D3858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682189433; bh=JmoeLQWkY0l2DeO/wO5WIQD13g12PCzIh3jb7kTYYdQ=; h=From:To:Subject:Date:From; b=ZGKXONWKvR2czmId4IjLR0bM5FUARtNqzynv3zgzvtWKu0U9WOdc7HQrWB55gmnWi 3IwKgAU642Xy2xRpOOaNJYP3/QDejgrbi+MJzskZBtpFqDddp2BYvdtsh6In6NvfLs KGOVZrpj75eSgFj6JZaG+Mke3lR4gXg2TKFojBf4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Harald Anlauf To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-167] Fortran: function results never have the ALLOCATABLE attribute [PR109500] X-Act-Checkin: gcc X-Git-Author: Harald Anlauf X-Git-Refname: refs/heads/master X-Git-Oldrev: 7a0cbaf7f802df209840d78740ffc749dadd1ce3 X-Git-Newrev: eb385a4801c45a1c0574810b45fce9e712d34566 Message-Id: <20230422185033.ADE3D3858C83@sourceware.org> Date: Sat, 22 Apr 2023 18:50:33 +0000 (GMT) List-Id: https://gcc.gnu.org/g:eb385a4801c45a1c0574810b45fce9e712d34566 commit r14-167-geb385a4801c45a1c0574810b45fce9e712d34566 Author: Harald Anlauf Date: Thu Apr 20 21:47:34 2023 +0200 Fortran: function results never have the ALLOCATABLE attribute [PR109500] Fortran 2018 8.5.3 (ALLOCATABLE attribute) explains in Note 1 that the result of referencing a function whose result variable has the ALLOCATABLE attribute is a value that does not itself have the ALLOCATABLE attribute. gcc/fortran/ChangeLog: PR fortran/109500 * interface.cc (gfc_compare_actual_formal): Reject allocatable functions being used as actual argument for allocable dummy. gcc/testsuite/ChangeLog: PR fortran/109500 * gfortran.dg/allocatable_function_11.f90: New test. Co-authored-by: Steven G. Kargl Diff: --- gcc/fortran/interface.cc | 12 ++++++++ .../gfortran.dg/allocatable_function_11.f90 | 36 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc index e9843e9549c..968ee193c07 100644 --- a/gcc/fortran/interface.cc +++ b/gcc/fortran/interface.cc @@ -3638,6 +3638,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 diff --git a/gcc/testsuite/gfortran.dg/allocatable_function_11.f90 b/gcc/testsuite/gfortran.dg/allocatable_function_11.f90 new file mode 100644 index 00000000000..1a2831e186f --- /dev/null +++ b/gcc/testsuite/gfortran.dg/allocatable_function_11.f90 @@ -0,0 +1,36 @@ +! { dg-do compile } +! PR fortran/109500 - check F2018:8.5.3 Note 1 +! +! The result of referencing a function whose result variable has the +! ALLOCATABLE attribute is a value that does not itself have the +! ALLOCATABLE attribute. + +program main + implicit none + integer, allocatable :: p + procedure(f), pointer :: pp + pp => f + p = f() + print *, allocated (p) + print *, is_allocated (p) + print *, is_allocated (f()) ! { dg-error "is a function result" } + print *, is_allocated (pp()) ! { dg-error "is a function result" } + call s (p) + call s (f()) ! { dg-error "is a function result" } + call s (pp()) ! { dg-error "is a function result" } + +contains + subroutine s(p) + integer, allocatable :: p + end subroutine s + + function f() + integer, allocatable :: f + allocate (f, source=42) + end function + + logical function is_allocated(p) + integer, allocatable :: p + is_allocated = allocated(p) + end function +end program