From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id EF8B0386C589 for ; Thu, 15 Feb 2024 17:51:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF8B0386C589 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=york.ac.uk Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=york.ac.uk ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EF8B0386C589 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708019466; cv=none; b=uAT2oI/u1j9TK9Hr/B+Bg9+m7WQDNHUuzWjJiwKS1qqvkAY2i4IvsFnIeVQDH/hMs63W9+Gj6QQacRBapp0pmlh4ihYIvOmWOtEhzwW3qkCCnP4zrFfp4WHY75Q2e5avWh5vPw5IWD7GdrPH+7Dy16lybInz5O9V46gZEUnkPVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708019466; c=relaxed/simple; bh=q3yIhOVkwTZExF6nXBp+KWwWvaqxhvqY5+rRdiL8Tm4=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=SWFfjre1nkYYzc+wgMTHSUWcaA/8Hkf6YVJNJQF1XTlnRQ/nIMWE0aAKCWwCvPYH9aBkyGjNXHztCp+bV97jIfRachNNgpKgdWiKE5mJSb8J0n8imr8UzRjnH+5gV4ShEv0Dlczk4NGTwZYwrx5zIhu3RVM5xzzatjMwXLAuP7I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-a2d7e2e7fe0so219140666b.1 for ; Thu, 15 Feb 2024 09:51:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=york.ac.uk; s=20160523.google; t=1708019462; x=1708624262; darn=gcc.gnu.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=loWK68PtimNuA35WkI/oURkPrnQjIrqidg33Sdufr7M=; b=Fe/Nd5Z22qdGMhaq8yLUlCpqDnQ9dAsPPstV/QY0RVW8r5FtvSPgN5GD8H4yMgIqhQ DMWf0aGmVRIX9gE+7vq01+LTdDwoD1X2VWhirkETxeVBiWdT+So6Br9fCEJw24sdREth pJ8DYnWJ9ZGTs8EQd7I2YqOeSuND/87AlbLsiwArWuBXejGOZoSFngtA589hzbt6lZ2D Fh18Cy2TEORI2l3eCqaZ57AKEr6oEdypmql68x/s3TMy2e/JgYSJ0P7eqQt7YvMg7WG6 o18D3q7qxoqr1/4RLFw0LI3JzyRkR13VMMql2pUh0ER9VXlursbGR9+tsRaChO+f3kiF uSpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708019462; x=1708624262; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=loWK68PtimNuA35WkI/oURkPrnQjIrqidg33Sdufr7M=; b=Gh80GvDA4okF10ZBJq+VCLuzZVejpzoBM2kP71ZxFJkOIPXmVHe4DBhHzRwdOia/gV mQ0DB4V7VGN7Pqe1BW4ghKEHyaO+dOl4IX4Tp3Zak3NhweHSnXAabTck4tEZZPvS9mn+ bTpGs8zIuEM+cXMq+c6gMM98v7NqLl642Vw6cKXj3E1EE8TrJgcPRkF0lFEuvMfqVEOc h+Huh8NdCb+ORShguhTZjgGOSTxhX68QwmRpaLL1QNzORinPHGCrJPQ/QHYh7QHkhyNv xcFZdmc9kt1uTKXINRvH0W54kUaUY9VNWVY2fGAhg0ljqGViGcRNGRj3beEsMoJH0Inw X9WQ== X-Gm-Message-State: AOJu0Yz/7Fs7HyHyfUKdMywUthVVZtVUnr6SrFZL+zd9scKiKoBEQjAD EEGuAUf/pFnNcm7blUz4Ui5apmXpR3jBIRHP0/FMdqxdPJ0AIfVC2la7DZIaDexjZRBVX+ZOF0t 6v5k0yfJo9r4molvRhGO5dm0qUxQFdhnFexuaPPQrQFtFSAh915djIQ== X-Google-Smtp-Source: AGHT+IGnIwa+8NJtJjqxyduV24ubfQu/EGyZJ+pBPDse3bqrZQiAw7YBoQCk3yKx8x2wIiJi4g4IORFILtSUrlDEPgI= X-Received: by 2002:a17:906:b18:b0:a38:833b:c8ad with SMTP id u24-20020a1709060b1800b00a38833bc8admr5136527ejg.16.1708019462301; Thu, 15 Feb 2024 09:51:02 -0800 (PST) MIME-Version: 1.0 From: Peter Hill Date: Thu, 15 Feb 2024 17:50:51 +0000 Message-ID: Subject: [PATCH] Fortran: fix passing array component to polymorphic argument [PR105658] To: gcc-patches@gcc.gnu.org Cc: fortran@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Dear all, The attached patch fixes PR105658 by forcing an array temporary to be created. This is required when passing an array component, but this didn't happen if the dummy argument was an unlimited polymorphic type. The problem bit of code is in `gfc_conv_expr_descriptor`, near L7828: subref_array_target = (is_subref_array (expr) && (se->direct_byref || expr->ts.type == BT_CHARACTER)); need_tmp = (gfc_ref_needs_temporary_p (expr->ref) && !subref_array_target); where `need_tmp` is being evaluated to 0. The logic here isn't clear to me, and this function is used in several places, which is why I went with setting `parmse.force_tmp = 1` in `gfc_conv_procedure_call` and using the same conditional as the later branch for the non-polymorphic case (near the call to `gfc_conv_subref_array_arg`) If this patch is ok, please could someone commit it for me? This is my first patch for GCC, so apologies in advance if the commit message is missing something. Tested on x86_64-pc-linux-gnu. The bug is present in gfortran back to 4.9, so should it also be backported? Cheers, Peter PR fortran/105658 gcc/fortran/ChangeLog * trans-expr.cc (gfc_conv_procedure_call): When passing an array component reference of intrinsic type to a procedure with an unlimited polymorphic dummy argument, a temporary should be created. gcc/testsuite/ChangeLog * gfortran.dg/PR105658.f90: New test. --- gcc/fortran/trans-expr.cc | 8 ++++++++ gcc/testsuite/gfortran.dg/PR105658.f90 | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/PR105658.f90 diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index a0593b76f18..7fd3047c4e9 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -6439,6 +6439,14 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, CLASS object for the unlimited polymorphic formal. */ gfc_find_vtab (&e->ts); gfc_init_se (&parmse, se); + /* The actual argument is a component reference to an array + of derived types, so we need to force creation of a + temporary */ + if (e->expr_type == EXPR_VARIABLE + && is_subref_array (e) + && !(fsym && fsym->attr.pointer)) + parmse.force_tmp = 1; + gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts); } diff --git a/gcc/testsuite/gfortran.dg/PR105658.f90 b/gcc/testsuite/gfortran.dg/PR105658.f90 new file mode 100644 index 00000000000..407ee25f77c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/PR105658.f90 @@ -0,0 +1,25 @@ +! { dg-do compile } +! { dg-options "-Warray-temporaries" } +! Test fix for incorrectly passing array component to unlimited polymorphic procedure + +module test_PR105658_mod + implicit none + type :: foo + integer :: member1 + integer :: member2 + end type foo +contains + subroutine print_poly(array) + class(*), dimension(:), intent(in) :: array + select type(array) + type is (integer) + print*, array + end select + end subroutine print_poly + + subroutine do_print(thing) + type(foo), dimension(3), intent(in) :: thing + call print_poly(thing%member1) ! { dg-warning "array temporary" } + end subroutine do_print + +end module test_PR105658_mod -- 2.43.0