From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 0B8CE3858CDB; Fri, 3 Nov 2023 10:23:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B8CE3858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1699006982; bh=PlSh0llj/Y96apnvjL/BKFq6HsS8kng/sziB9ySbg9w=; h=From:To:Subject:Date:In-Reply-To:References:From; b=TMpcn+FWdLSs/+73gZ63puYn75II+j1VGI3Fb5v+wpSH402UhoRw5XCrMldWmM7gn w22G6Vx1X0kdwoQ3g4cKrwo+0PVJnx9xmdesESyFblFTcYA7O67rhQLVWIuvBvF19h YaS0GUhfVFZtfp8+cOgCxloXP1w9r86LbiBHNakA= From: "federico at kircheis dot it" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112335] missed optimization on reset and assign unique_ptr Date: Fri, 03 Nov 2023 10:23:00 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: federico at kircheis dot it X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID 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: Message-ID: In-Reply-To: References: 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=3D112335 --- Comment #9 from Federico Kircheis --- Great, thank you for the clarifications, your redacted example makes now se= nse. > https://godbolt.org/z/WGPTesEb3 shows that bar3 is not the same as bar1, = because it runs an additional destructor if operator delete() plays silly g= ames. And this is what I meant before that such program is not supported by the standard ---- void bar3(std::unique_ptr& ps1, std::unique_ptr& ps2){ ps1.reset(); ps1.reset(); ps1 =3D std::move(ps2); } --- is equivalent to ---- void bar3(std::unique_ptr& ps1, std::unique_ptr& ps2){ ps1.reset(); assert(ps1 =3D=3D nullptr); ps1.reset(); assert(ps1 =3D=3D nullptr); ps1 =3D std::move(ps2); } ---- which is not. And the same holds if "if (!global) global.reset(new s);" is moved into "~s= ()", and removed from "operator delete". If the users changes delete in such a way, at this point we have UB, as the postcondition of unique_ptr::reset does not hold (the example given by Andr= ew) Yes, the second ps1.reset(); is trivial to remove.=