From patchwork Tue May 9 15:51:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Richard Thomas X-Patchwork-Id: 68982 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 E4B433858C2B for ; Tue, 9 May 2023 15:52:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4B433858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683647575; bh=cvltY3pkZkrDE3qJ6xQOrV5nqRddMcciAWgIxzsyLwY=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HmYYPQ3NLPg9zcLv9QDei5F9vypbn4pBopzrsLBPc/sR+k3Orxknk5nOwP5MzPjDB 6PXpw0dGeind6KKsSKcnA3CHQKXLDGvuH+YumDh3iDqjweIQRx3J+8dJ/db5cM8LAb TpwyF69uFMpQPXfW//KsGY0xJabFr6F5mvjRkQhE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 2166C3858D37; Tue, 9 May 2023 15:52:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2166C3858D37 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-64115eef620so43492106b3a.1; Tue, 09 May 2023 08:52:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683647528; x=1686239528; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+bMPNA78GHFjQH0DqS3OM1N3fdqN04aPOEr7mv2ygQg=; b=efhHKnbfDavMXLK6di/77ATBSrbpM+x/9TqIkERTxUHvl3TykMgAMlwYmsqz/ZI6QM Ql8Uw2AJE6S+o/cMqjHLlAbdUmuAZixYitmE2oFYYdKemwCJKGFu5EU5Ieaet6MWdxCd c5vW0/BuI8S8WXNziZOUNbNNTV2TLn8xPkkBIGJLIWXHVvF5YsHxR8Bj0SL0Q5U0Ltxp g2F3RGlcexVI5scQKc1pBRQy9EvQ4jrECRAbb6hjaUZk0MA2jk6FoHplyYKv5mbdx/ki xqbxzh2dbP8pQxaKdmmUTA0blVfIIRhdB8ms/AMvqyHqdc0YMCLJo+Iq6vsijmNB5NhL WjQQ== X-Gm-Message-State: AC+VfDwHoDRPHerPrgNdLfoFF0qAvYMsnH2QpDcyt+7KrtEZg9fSqH67 w+CegYCXwKHvDw5tZkN209KL/2JZ94Zu1gzrgcrK8Qe6gkezEA== X-Google-Smtp-Source: ACHHUZ49LrETUapbtOUG2b3ofBPN4orgNCqg6rVoTId3rlJA6rUSgqCVX1mrVvNE981DwTW59XB3Iz6Un2Ol96IXEHg= X-Received: by 2002:a17:903:2343:b0:1ac:85b0:1bd8 with SMTP id c3-20020a170903234300b001ac85b01bd8mr5613262plh.34.1683647527547; Tue, 09 May 2023 08:52:07 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 9 May 2023 16:51:56 +0100 Message-ID: Subject: [Patch, fortran] PR97122 - Spurious FINAL ... must be in the specification part of a MODULE To: "fortran@gcc.gnu.org" , gcc-patches Cc: "Steven G. Kargl" X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, 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 X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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: Paul Richard Thomas via Gcc-patches From: Paul Richard Thomas Reply-To: Paul Richard Thomas Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi All, Thanks to Steve Kargl for the fix. It caused finalize_8.f03 to fail because this testcase checked that finalizable derived types could not be specified in a submodule. I have replaced the original test with a test of the patch. Thanks also to Malcolm Cohen for guidance on this. OK for trunk? Paul Fortran: Allow declaration of finalizable DT in a submodule [PR97122] 2023-05-09 Paul Thomas Steven G. Kargl gcc/fortran PR fortran/97122 * decl.cc (variable_decl): Clean up white space issues. (gfc_match_final_decl): Declaration of finalizable derived type is allowed in a submodule. gcc/testsuite/ PR fortran/97122 * gfortran.dg/finalize_8.f03 : Replace testcase that checks declaration of finalizable derived types in submodules works. diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 233bf244d62..6d6ce0854de 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -2698,7 +2698,7 @@ variable_decl (int elem) } gfc_seen_div0 = false; - + /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not constant expressions shall appear only in a subprogram, derived type definition, BLOCK construct, or interface body. */ @@ -2769,7 +2769,7 @@ variable_decl (int elem) if (e->expr_type != EXPR_CONSTANT) { n = gfc_copy_expr (e); - if (!gfc_simplify_expr (n, 1) && gfc_seen_div0) + if (!gfc_simplify_expr (n, 1) && gfc_seen_div0) { m = MATCH_ERROR; goto cleanup; @@ -2784,12 +2784,12 @@ variable_decl (int elem) if (e->expr_type != EXPR_CONSTANT) { n = gfc_copy_expr (e); - if (!gfc_simplify_expr (n, 1) && gfc_seen_div0) + if (!gfc_simplify_expr (n, 1) && gfc_seen_div0) { m = MATCH_ERROR; goto cleanup; } - + if (n->expr_type == EXPR_CONSTANT) gfc_replace_expr (e, n); else @@ -11637,8 +11637,9 @@ gfc_match_final_decl (void) block = gfc_state_stack->previous->sym; gcc_assert (block); - if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous - || gfc_state_stack->previous->previous->state != COMP_MODULE) + if (gfc_state_stack->previous->previous + && gfc_state_stack->previous->previous->state != COMP_MODULE + && gfc_state_stack->previous->previous->state != COMP_SUBMODULE) { gfc_error ("Derived type declaration with FINAL at %C must be in the" " specification part of a MODULE"); diff --git a/gcc/testsuite/gfortran.dg/finalize_8.f03 b/gcc/testsuite/gfortran.dg/finalize_8.f03 index b2027a0ba6d..b7fa10dda31 100644 --- a/gcc/testsuite/gfortran.dg/finalize_8.f03 +++ b/gcc/testsuite/gfortran.dg/finalize_8.f03 @@ -1,35 +1,49 @@ -! { dg-do compile } - -! Parsing of finalizer procedure definitions. -! Check that FINAL-declarations are only allowed on types defined in the -! specification part of a module. - -MODULE final_type +! { dg-do run } +! +! PR97122: Declaration of a finalizable derived type in a submodule +! IS allowed. +! +! Contributed by Ian Harvey +! +MODULE m IMPLICIT NONE -CONTAINS + INTERFACE + MODULE SUBROUTINE other(i) + IMPLICIT NONE + integer, intent(inout) :: i + END SUBROUTINE other + END INTERFACE - SUBROUTINE bar - IMPLICIT NONE + integer :: mi - TYPE :: mytype - INTEGER, ALLOCATABLE :: fooarr(:) - REAL :: foobar - CONTAINS - FINAL :: myfinal ! { dg-error "in the specification part of a MODULE" } - END TYPE mytype - - CONTAINS +END MODULE m - SUBROUTINE myfinal (el) - TYPE(mytype) :: el - END SUBROUTINE myfinal +SUBMODULE (m) s + IMPLICIT NONE - END SUBROUTINE bar + TYPE :: t + integer :: i + CONTAINS + FINAL :: final_t ! Used to be an error here + END TYPE t -END MODULE final_type +CONTAINS -PROGRAM finalizer - IMPLICIT NONE - ! Do nothing here -END PROGRAM finalizer + SUBROUTINE final_t(arg) + TYPE(t), INTENT(INOUT) :: arg + mi = -arg%i + END SUBROUTINE final_t + + module subroutine other(i) ! 'ti' is finalized + integer, intent(inout) :: i + type(t) :: ti + ti%i = i + END subroutine other +END SUBMODULE s + + use m + integer :: i = 42 + call other(i) + if (mi .ne. -i) stop 1 +end