c++: Fix handling of temporaries with consteval ctors and non-trivial dtors [PR104055]
Message ID | 20220118101005.GV2646553@tucnak |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 52FAF385801C for <patchwork@sourceware.org>; Tue, 18 Jan 2022 10:11:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 52FAF385801C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642500669; bh=xwFdpNooWAbR+CNcfpjYY5WwS5dQMECEVqjoRj2mH+k=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=k4bj35wv4i1PUmbdF+Wg5v92IHNyF9hWdQiEzc1oAPxq+4TtFveECf9Cs3bEDJJ14 JXZOTtKQvve2tRecTg1pgkjkRJX4LAG5lWsuWzR/DvM4uBiPk0DHrXVMVECH+eYy9E E8dwtUostm4G/WjPlarV00RB2Lxvw/+rtUNTNfps= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 717653858D39 for <gcc-patches@gcc.gnu.org>; Tue, 18 Jan 2022 10:10:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 717653858D39 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-336-SBqtRElGPgKyKwSlFMjNNg-1; Tue, 18 Jan 2022 05:10:32 -0500 X-MC-Unique: SBqtRElGPgKyKwSlFMjNNg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 21AC084DA41 for <gcc-patches@gcc.gnu.org>; Tue, 18 Jan 2022 10:10:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.40.192.74]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ADF6B70D59; Tue, 18 Jan 2022 10:10:07 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 20IAA5jT3067066 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 18 Jan 2022 11:10:05 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 20IAA5dZ3067065; Tue, 18 Jan 2022 11:10:05 +0100 Date: Tue, 18 Jan 2022 11:10:05 +0100 To: Jason Merrill <jason@redhat.com> Subject: [PATCH] c++: Fix handling of temporaries with consteval ctors and non-trivial dtors [PR104055] Message-ID: <20220118101005.GV2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Jakub Jelinek <jakub@redhat.com> Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: Fix handling of temporaries with consteval ctors and non-trivial dtors [PR104055]
|
|
Commit Message
Jakub Jelinek
Jan. 18, 2022, 10:10 a.m. UTC
Hi! The following testcase is miscompiled. We see the constructor is immediate, in build_over_call we trigger: if (obj_arg && is_dummy_object (obj_arg)) { call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain); obj_arg = NULL_TREE; } which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP, but then call cxx_constant_value on it. In cxx_eval_outermost_constant_expr it triggers the: else if (TREE_CODE (t) != CONSTRUCTOR) { r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); TREE_CONSTANT (r) = true; } which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without dtors (I think we need e.g. the TREE_CONSTANT for the callers), and finally build_over_call uses that. The following patch fixes that by reusing the earlier TARGET_EXPR and overwriting TARGET_EXPR_INITIAL in it; I think the TARGET_EXPR_SLOT can't appear in the TARGET_EXPR_INITIAL, because the CONSTRUCTOR has been built already before get_target_expr_sfinae has been called and addresses of an automatic var aren't constant expression. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-01-18 Jakub Jelinek <jakub@redhat.com> PR c++/104055 * call.c (build_over_call): If in a call to consteval ctor call is a TARGET_EXPR with cleanups and cxx_constant_value returns a different TARGET_EXPR, reuse the original TARGET_EXPR with the new TARGET_EXPR_INITIAL. * g++.dg/cpp2a/consteval27.C: New test. Jakub
Comments
On 1/18/22 05:10, Jakub Jelinek wrote: > Hi! > > The following testcase is miscompiled. We see the constructor is immediate, > in build_over_call we trigger: > if (obj_arg && is_dummy_object (obj_arg)) > { > call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain); > obj_arg = NULL_TREE; > } > which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP, > but then call cxx_constant_value on it. In cxx_eval_outermost_constant_expr > it triggers the: > else if (TREE_CODE (t) != CONSTRUCTOR) > { > r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); > TREE_CONSTANT (r) = true; > } > which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without > dtors (I think we need e.g. the TREE_CONSTANT for the callers), > and finally build_over_call uses that. Looks like you added the tf_no_cleanup in r10-3661 (constexpr new). Maybe that should only be added if TARGET_EXPR_CLEANUP (t) is null? > The following patch fixes that by reusing the earlier TARGET_EXPR and > overwriting TARGET_EXPR_INITIAL in it; I think the TARGET_EXPR_SLOT can't > appear in the TARGET_EXPR_INITIAL, because the CONSTRUCTOR has been built > already before get_target_expr_sfinae has been called and addresses of > an automatic var aren't constant expression. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > 2022-01-18 Jakub Jelinek <jakub@redhat.com> > > PR c++/104055 > * call.c (build_over_call): If in a call to consteval ctor call > is a TARGET_EXPR with cleanups and cxx_constant_value returns > a different TARGET_EXPR, reuse the original TARGET_EXPR with > the new TARGET_EXPR_INITIAL. > > * g++.dg/cpp2a/consteval27.C: New test. > > --- gcc/cp/call.c.jj 2022-01-11 23:11:22.049294946 +0100 > +++ gcc/cp/call.c 2022-01-17 11:48:50.272044412 +0100 > @@ -9935,7 +9935,14 @@ build_over_call (struct z_candidate *can > obj_arg = TREE_OPERAND (addr, 0); > } > } > - call = cxx_constant_value (call, obj_arg); > + tree cst_call = cxx_constant_value (call, obj_arg); > + if (call != cst_call > + && TREE_CODE (call) == TARGET_EXPR > + && TARGET_EXPR_CLEANUP (call) > + && TREE_CODE (cst_call) == TARGET_EXPR) > + TARGET_EXPR_INITIAL (call) = TARGET_EXPR_INITIAL (cst_call); > + else > + call = cst_call; > if (obj_arg && !error_operand_p (call)) > call = build2 (INIT_EXPR, void_type_node, obj_arg, call); > call = convert_from_reference (call); > --- gcc/testsuite/g++.dg/cpp2a/consteval27.C.jj 2022-01-17 11:54:09.656459199 +0100 > +++ gcc/testsuite/g++.dg/cpp2a/consteval27.C 2022-01-17 11:54:37.035068677 +0100 > @@ -0,0 +1,18 @@ > +// PR c++/104055 > +// { dg-do run { target c++20 } } > + > +int g; > + > +struct A { > + ~A () { if (a != 17 || b != 26) __builtin_abort (); g = 42; } > + consteval A () : a (17), b (26) {} > + int a, b; > +}; > + > +int > +main () > +{ > + A{}; > + if (g != 42) > + __builtin_abort (); > +} > > Jakub >
On Tue, Jan 18, 2022 at 09:08:03AM -0500, Jason Merrill wrote: > > which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP, > > but then call cxx_constant_value on it. In cxx_eval_outermost_constant_expr > > it triggers the: > > else if (TREE_CODE (t) != CONSTRUCTOR) > > { > > r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); > > TREE_CONSTANT (r) = true; > > } > > which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without > > dtors (I think we need e.g. the TREE_CONSTANT for the callers), > > and finally build_over_call uses that. > > Looks like you added the tf_no_cleanup in r10-3661 (constexpr new). Maybe > that should only be added if TARGET_EXPR_CLEANUP (t) is null? I thought about that, but I'm worried that if the cleanup action is non-empty, then the TREE_CONSTANT (r) = true; after it isn't appropriate, because the TARGET_EXPR has side-effects. And the callers like cxx_constant_value_sfinae just return error_mark_node if it isn't set: if (sfinae && !TREE_CONSTANT (r)) r = error_mark_node; Jakub
On 1/18/22 09:26, Jakub Jelinek wrote: > On Tue, Jan 18, 2022 at 09:08:03AM -0500, Jason Merrill wrote: >>> which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP, >>> but then call cxx_constant_value on it. In cxx_eval_outermost_constant_expr >>> it triggers the: >>> else if (TREE_CODE (t) != CONSTRUCTOR) >>> { >>> r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); >>> TREE_CONSTANT (r) = true; >>> } >>> which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without >>> dtors (I think we need e.g. the TREE_CONSTANT for the callers), >>> and finally build_over_call uses that. >> >> Looks like you added the tf_no_cleanup in r10-3661 (constexpr new). Maybe >> that should only be added if TARGET_EXPR_CLEANUP (t) is null? > > I thought about that, but I'm worried that if the cleanup action > is non-empty, then the TREE_CONSTANT (r) = true; after it isn't appropriate, > because the TARGET_EXPR has side-effects. Hmm. If we e.g. pull out a subobject value from the TARGET_EXPR temporary, we still need to run the cleanup, so we don't want to optimize the temporary away, so not setting TREE_CONSTANT sounds right. > And the callers like cxx_constant_value_sfinae just return error_mark_node > if it isn't set: > if (sfinae && !TREE_CONSTANT (r)) > r = error_mark_node; Yes. Is that wrong? I guess we need to be more careful about adjusting the arguments to cxx_constant_value_sfinae if we still want a constant value. Perhaps we also still want to set tf_no_cleanup if object is nonnull, since in that case we know the prvalue is going to be an initializer rather than a temporary. Jason
--- gcc/cp/call.c.jj 2022-01-11 23:11:22.049294946 +0100 +++ gcc/cp/call.c 2022-01-17 11:48:50.272044412 +0100 @@ -9935,7 +9935,14 @@ build_over_call (struct z_candidate *can obj_arg = TREE_OPERAND (addr, 0); } } - call = cxx_constant_value (call, obj_arg); + tree cst_call = cxx_constant_value (call, obj_arg); + if (call != cst_call + && TREE_CODE (call) == TARGET_EXPR + && TARGET_EXPR_CLEANUP (call) + && TREE_CODE (cst_call) == TARGET_EXPR) + TARGET_EXPR_INITIAL (call) = TARGET_EXPR_INITIAL (cst_call); + else + call = cst_call; if (obj_arg && !error_operand_p (call)) call = build2 (INIT_EXPR, void_type_node, obj_arg, call); call = convert_from_reference (call); --- gcc/testsuite/g++.dg/cpp2a/consteval27.C.jj 2022-01-17 11:54:09.656459199 +0100 +++ gcc/testsuite/g++.dg/cpp2a/consteval27.C 2022-01-17 11:54:37.035068677 +0100 @@ -0,0 +1,18 @@ +// PR c++/104055 +// { dg-do run { target c++20 } } + +int g; + +struct A { + ~A () { if (a != 17 || b != 26) __builtin_abort (); g = 42; } + consteval A () : a (17), b (26) {} + int a, b; +}; + +int +main () +{ + A{}; + if (g != 42) + __builtin_abort (); +}