From patchwork Sun Apr 26 22:48:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1277258 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=iny4C3VB; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 499NNx1SJCz9sSK for ; Mon, 27 Apr 2020 08:48:30 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 85199384B0C0; Sun, 26 Apr 2020 22:48:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85199384B0C0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1587941306; bh=ZPAJYk8DoK5bbIh2sd5EMOffaBkXl5lYCpS5CYRRI8s=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=iny4C3VBxMXzY+eOIWzoIIstz3brS0yaq8jDG8NKuExZ2LxYJ8UmL/08v8/0kox6l JnkvgitwiFXJ8VfCmplnwRTwhhpM2djJyXvoLJEz44ChFBrIX0qWOwvX8kYmowJiJh FXytjzHEUa1kDMqrGAbXCf/EMhdHtFm1Z6DYxSK4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id 081FF385DC1B for ; Sun, 26 Apr 2020 22:48:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 081FF385DC1B Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-505-f3K9YhHpPRaJ7RgAw3e0eA-1; Sun, 26 Apr 2020 18:48:21 -0400 X-MC-Unique: f3K9YhHpPRaJ7RgAw3e0eA-1 Received: by mail-qk1-f199.google.com with SMTP id x7so17505494qkb.17 for ; Sun, 26 Apr 2020 15:48:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=IHs4tnaqBYZIiiLo+h1EWKXCyVbUoUrEe/9pOsG8e54=; b=J2KqSwZmDxboZAPQcDso2qpeNg4fp1HGhVlDGDnIuznOzCC703cozPCLWzwi6x33xz mDD2qlNlhhZjKnIL3rECnqXyI8/yakE2T6jbvMrcipZlDZDt2UBUCqQYgvzoBIP32sB1 zFkHA7+WzI/kcc+71nKH0avuzGGFdoaazLTDU0rNl015KYjOGs35+lAXbzPK+ksmsp9d GzqYUFLAlaLkgKHMClqM09AinKleStbInfQuuF7QMJp70tFNVgQObMWm+yWJ7UK3ZFX/ D1v3jqyNA9ohvnHsSpckp38kPKMufuIHHO7DFpYpxf/e0yepLWvY9mFgQOOW4nsezExa mT/w== X-Gm-Message-State: AGi0Pubh3Dj8wp1dIqEzN4B5Z2kX8wy+VES9rWwtLucPMmF2mPK5PKFv 5hemXbp6gy66sVkDeS6Xhh+5jCCA2wIZ+gyFURzjWIhcAy6thdHJzIYpFoCWcZ/Nl9RUzu3uO66 +DqaVmO63PzUJWh6dXQ== X-Received: by 2002:ac8:32a4:: with SMTP id z33mr20782912qta.363.1587941301020; Sun, 26 Apr 2020 15:48:21 -0700 (PDT) X-Google-Smtp-Source: APiQypLv7Mvd/Ds1e/XuUkoRx5F9zWBDonv4D7es2ELXFfwlXtuzBeved6ik8Ix5h7QwD/2KaeqkzA== X-Received: by 2002:ac8:32a4:: with SMTP id z33mr20782900qta.363.1587941300690; Sun, 26 Apr 2020 15:48:20 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id x16sm8523384qkh.14.2020.04.26.15.48.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2020 15:48:20 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Delegating constructor in constexpr init [PR94772] Date: Sun, 26 Apr 2020 18:48:17 -0400 Message-Id: <20200426224817.2656335-1-ppalka@redhat.com> X-Mailer: git-send-email 2.26.2.266.ge870325ee8 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-28.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" In the testcase below, the call to the target constructor foo{} from foo's delegating constructor is encoded as the INIT_EXPR *(struct foo *) this = AGGR_INIT_EXPR <4, __ct_comp, D.2140, ...>; During initialization of the variable 'bar', we prematurely set TREE_READONLY on bar's CONSTRUCTOR in two places before the outer delegating constructor has returned: first, at the end of cxx_eval_call_expression after evaluating the RHS of the above INIT_EXPR, and second, at the end of cxx_eval_store_expression after having finished evaluating the above INIT_EXPR. This then prevents the rest of the outer delegating constructor from mutating 'bar'. This (hopefully minimally risky) patch makes cxx_eval_call_expression refrain from setting TREE_READONLY when evaluating the target constructor of a delegating constructor. It also makes cxx_eval_store_expression refrain from setting TREE_READONLY when the object being initialized is "*this', on the basis that it should be the responsibility of the routine that set 'this' in the first place to set the object's TREE_READONLY appropriately. Passes 'make check-c++', does this look OK to commit after full bootstrap/regtest? gcc/cp/ChangeLog: PR c++/94772 * constexpr.c (cxx_eval_call_expression): Don't set new_obj if we're evaluating the target constructor of a delegating constructor. (cxx_eval_store_expression): Don't set TREE_READONLY if the LHS of the INIT_EXPR is '*this'. gcc/testsuite/ChangeLog: PR c++/94772 * g++.dg/cpp1y/constexpr-tracking-const23.C: New test. --- gcc/cp/constexpr.c | 29 +++++++++++++++---- .../g++.dg/cpp1y/constexpr-tracking-const23.C | 21 ++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const23.C diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 6b3e514398b..a9ddd861195 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -2367,10 +2367,20 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, /* In a constructor, it should be the first `this' argument. At this point it has already been evaluated in the call to cxx_bind_parameters_in_call. */ - new_obj = TREE_VEC_ELT (new_call.bindings, 0); - STRIP_NOPS (new_obj); - if (TREE_CODE (new_obj) == ADDR_EXPR) - new_obj = TREE_OPERAND (new_obj, 0); + + if (ctx->call && ctx->call->fundef + && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl) + && (TREE_VEC_ELT (ctx->call->bindings, 0) + == TREE_VEC_ELT (new_call.bindings, 0))) + /* We're calling the target constructor of a delegating constructor, so + there is no new object. */; + else + { + new_obj = TREE_VEC_ELT (new_call.bindings, 0); + STRIP_NOPS (new_obj); + if (TREE_CODE (new_obj) == ADDR_EXPR) + new_obj = TREE_OPERAND (new_obj, 0); + } } tree result = NULL_TREE; @@ -4950,7 +4960,16 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, if (TREE_CODE (t) == INIT_EXPR && TREE_CODE (*valp) == CONSTRUCTOR && TYPE_READONLY (type)) - TREE_READONLY (*valp) = true; + { + if (INDIRECT_REF_P (target) + && (is_this_parameter + (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) + /* We've just initialized '*this' (perhaps via the target constructor of + a delegating constructor). Leave it up to the caller that set 'this' + to set TREE_READONLY appropriately. */; + else + TREE_READONLY (*valp) = true; + } /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing CONSTRUCTORs, if any. */ diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const23.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const23.C new file mode 100644 index 00000000000..266b62b852f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const23.C @@ -0,0 +1,21 @@ +// PR c++/94772 +// { dg-do compile { target c++14 } } + +struct foo +{ + int x{}; + + constexpr foo() noexcept = default; + + constexpr foo(int a) : foo{} + { x = -a; } + + constexpr foo(int a, int b) : foo{a} + { x += a + b; } +}; + +int main() +{ + constexpr foo bar{1, 2}; + static_assert(bar.x == 2, ""); +}