From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3205D3854EB4; Tue, 11 Jul 2023 09:40:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3205D3854EB4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689068410; bh=aikGWfJOgsrHZgI0GivOzZl8ODbbvcxg6xgYvxFBy7E=; h=From:To:Subject:Date:From; b=bocZqs5RpC6qrPSo0It88bLf4RnE1qNV0jTHp82c/4CfdW47qOkGEqU4mq0fmE0lU gEgYJwbfS+CIkPYilKDdz9COvyPq3azu7UXVHOJ//3phJsd5Ix6SpWttgNy/753Mtk VLSk83EBE2eI9CKY3jWi9YWMW0SscHsq7Hq23RdA= From: "habbit89 at hotmail dot es" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/110626] New: Duplicated finalization in derived Date: Tue, 11 Jul 2023 09:40:09 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: habbit89 at hotmail dot es X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: 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=3D110626 Bug ID: 110626 Summary: Duplicated finalization in derived Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: habbit89 at hotmail dot es Target Milestone: --- Created attachment 55520 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D55520&action=3Dedit Example module and test program After the changes to finalization in gfortran 13 there seems to be an issue under the following circumstances: * A derived type A has custom assignment and finalization * It is used as a component (or as a base) of another type B Then, assignment of type B will call the assignment of A only once, but the final subroutine twice, which breaks resource holding code such as reference counting. The example has two subroutines, one where two objects of type A are used directly, and one where objects of type B are used. In both cases, o1 is initialized to some value, then it is copied to o2, then o2 is overwritten again. The expected result would, in both cases, be a finalization of the target before the assignment, then the assignment call. Compiling and running the example will give the following correct results on gfortran 11 > $ gfortran-11 -Wall -Wextra -o a-11.out testmod.f90 > $ ./a-11.out > o1: 7FFFA5E1BD2C > o2: 7FFFA5E1BD28 > dtor: -1 7FFFA5E1BD28 > copy: 15 from 7FFFA5E1BD2C to 7FFFA5E1BD28 > dtor: 16 7FFFA5E1BD28 > copy: 15 from 7FFFA5E1BD2C to 7FFFA5E1BD28 > dtor: 16 7FFFA5E1BD28 > dtor: 15 7FFFA5E1BD2C > objects of type B in subroutine > o1: 7FFFA5E1BD2C > o2: 7FFFA5E1BD28 > dtor: 15 7FFFA5E1BD28 > copy: 15 from 7FFFA5E1BD2C to 7FFFA5E1BD28 > dtor: 15 7FFFA5E1BD28 > copy: 15 from 7FFFA5E1BD2C to 7FFFA5E1BD28 > dtor: 16 7FFFA5E1BD28 > dtor: 15 7FFFA5E1BD2C But the following *invalid* results in gfortran 13: > $ gfortran-13 -Wall -Wextra -o a-13.out testmod.f90 > $ ./a-13.out=20 > objects of type A in subroutine > o1: 7FFFCEEDBC2C > o2: 7FFFCEEDBC28 > dtor: -1 7FFFCEEDBC28 > copy: 15 from 7FFFCEEDBC2C to 7FFFCEEDBC28 > dtor: 16 7FFFCEEDBC28 > copy: 15 from 7FFFCEEDBC2C to 7FFFCEEDBC28 > dtor: 16 7FFFCEEDBC28 > dtor: 15 7FFFCEEDBC2C > objects of type B in subroutine > o1: 7FFFCEEDBC24 > o2: 7FFFCEEDBC20 > dtor: -1 7FFFCEEDBC20 > dtor: -1 7FFFCEEDBC2C > copy: 15 from 7FFFCEEDBC24 to 7FFFCEEDBC2C > dtor: 16 7FFFCEEDBC20 > dtor: 16 7FFFCEEDBC28 > copy: 15 from 7FFFCEEDBC24 to 7FFFCEEDBC28 > dtor: 16 7FFFCEEDBC20 > dtor: 15 7FFFCEEDBC24 The part where objects of type A are used directly works in both versions. However, when objects of type *B* are used, gfortran 13 shows the following behaviour: * There seems to be a "shadow"/temporary object created at a different loca= tion which is neither o1 nor o2, probably at a stack address. * The assignment operator runs only once, from o1 to this shadow object. * The value is then apparently blitted onto/used for o2, which might be okay except that... * Before the next assignment, the final subroutine of A runs *twice*, once = with the actual o2 and once with this shadow object. Thus, given that the assignment code runs once but the finalization runs *twice*, using this scheme to hold resources (e.g. via ref counting) breaks= . In particular, it is very weird that=20 I am _assuming_ that the two separate finalizations may be conceptually come from 1. the overall finalization of B before the assignment, and 2. the intent(out) for A in subroutine copy. However, both calls use the values _prior_ to the finalization (since it sets the value to -2 but the calls bo= th print 16)=