From patchwork Wed Dec 13 16:47:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1875797 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=C26pkQ0l; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Sr1d00ywTz20LT for ; Thu, 14 Dec 2023 03:48:02 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D8D80385C412 for ; Wed, 13 Dec 2023 16:48:00 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 1610A3858C78 for ; Wed, 13 Dec 2023 16:47:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1610A3858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1610A3858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486070; cv=none; b=aOtqIwPEtO1Dtf4Qnp2CavY9CYkwCAeiUKvtSsL4DiqV3jxswWL1f+LE4u3VsqCLLEjdJDximsTP49oyJgIW4QbGJeMaF8g8KL+ck1j0WzdMGZVBIOOsTlkhbGCx+HTp5xKgcns9VFh122JGF4BAijSUIzqvw5xQMXvKMW/Cv1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486070; c=relaxed/simple; bh=dS0TCR1zFP0M+KGsMwivovgADltQEygJgqKTN6f+6VA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=U2WAgwLLzcejctPedbuShcLKHrgRFg4bwZYfjo33nd5dhzL55PR4aPyhoX8ybHptnr3TQNN/g4d/NpoXXJALCQh2LMkZ4nO0P2mM1YDfKbUQDOfzPLM5msPHrLKhXI3eUUwuk9g3WqlKli8VuRceVDXEgrnxL2MkLFOhy+2+OQ0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702486067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3+D57GauHLjJc6H9ZRRGNP5ni/6p5Lo4jPEWCLUe7oI=; b=C26pkQ0lk+dwnrXMV9oEFP6RNqKltYAz54lZVZl+vhj4TvDq3CZUiVuh8Kptt8uJiN7m2m 4ffOMAMnYOJYBhOcNIq7V/6R1C+Pa2Dnz6plImORf2HMP1xkkZ/wJj2ELUDO+qEryeLeMd JqezuQTygCEVnOMKifFAWSaiVP/5qIE= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-549-hTVDjdAbMyup9wnBqGpNWA-1; Wed, 13 Dec 2023 11:47:44 -0500 X-MC-Unique: hTVDjdAbMyup9wnBqGpNWA-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-67ee1ca3b05so34629576d6.2 for ; Wed, 13 Dec 2023 08:47:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702486064; x=1703090864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3+D57GauHLjJc6H9ZRRGNP5ni/6p5Lo4jPEWCLUe7oI=; b=Y8+L9/xiwaozRAWpPS5sLMhRUgyhBi2J0AWYfD4xLh7TjM8WEpN8qHbuy0Gq2//9qt xhPru28xkQllJw6vgEftaYUAf3E4tQF1jTCJpsun7UU5r+lFuWFxrkip6lE4EefCbcCe Hvx5n424BZ7ByCwh9OMd/ACwpzg5IHOJpb8Fw2o4WEE3Cdvq2zxRgGkUgzHf3fiBDRZp JL1VKbK1peGLzkx9/uqUrFusKureoiE+8zbivh1qiWWf5174QM6pVWnC0+HwaJQm+FaV hHqXEziRNE0wNxmQSuSDx0tM0i/z9jDa2Upl81uBrDa630Orfcgyn2wWXGxFWSPxsqbJ tcPA== X-Gm-Message-State: AOJu0Yx4oc4gEmV4ADbvMX020qOfMqas9IiA4IRVM4hqW1LgdLwskkso 7RadGkjxRMdD5GVu1ZDsC1DW2CMFENbLubDHRTaSoUn9twut8xO4A1yc3M3wjZjGS1bf87s3gvW 2tG0ARRGZFBehJMLTbnpNPd1+uh8OjDEC4J0VAwxbTDyfwu9vXzhXCjq7SPKXdh/22RMG7lkCZg == X-Received: by 2002:a05:6214:518c:b0:67f:4e8:c80 with SMTP id kl12-20020a056214518c00b0067f04e80c80mr151813qvb.119.1702486063814; Wed, 13 Dec 2023 08:47:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGoryS5in1mOPduddMDEglquIr0enV6CTaiCZ9ZFOefR6K6be4RLOmmkA6Aych1MjUjjyZ2Dg== X-Received: by 2002:a05:6214:518c:b0:67f:4e8:c80 with SMTP id kl12-20020a056214518c00b0067f04e80c80mr151799qvb.119.1702486063420; Wed, 13 Dec 2023 08:47:43 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id j25-20020a05620a0a5900b0077f37896f24sm4613249qka.97.2023.12.13.08.47.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:47:42 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed 1/4] c++: copy location to AGGR_INIT_EXPR Date: Wed, 13 Dec 2023 11:47:37 -0500 Message-Id: <20231213164740.1591535-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, 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-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- When building an AGGR_INIT_EXPR from a CALL_EXPR, we shouldn't lose location information. gcc/cp/ChangeLog: * tree.cc (build_aggr_init_expr): Copy EXPR_LOCATION. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-nsdmi7b.C: Adjust line. * g++.dg/template/copy1.C: Likewise. --- gcc/cp/tree.cc | 1 + gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi7b.C | 4 ++-- gcc/testsuite/g++.dg/template/copy1.C | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) base-commit: d2b269ce30d77dbfc6c28c75887c330d4698b132 diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index da4d5c51f07..c4e41fd7b5c 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -689,6 +689,7 @@ build_aggr_init_expr (tree type, tree init) CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init); CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init); CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init); + SET_EXPR_LOCATION (rval, EXPR_LOCATION (init)); } else rval = init; diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi7b.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi7b.C index a410e482664..586ee54124c 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi7b.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi7b.C @@ -20,8 +20,8 @@ bar() { A a = foo(); a.p->n = 5; - return a; -} // { dg-error "non-.constexpr." "" { target c++20_down } } + return a; // { dg-error "non-.constexpr." "" { target c++20_down } } +} constexpr int baz() diff --git a/gcc/testsuite/g++.dg/template/copy1.C b/gcc/testsuite/g++.dg/template/copy1.C index eacd9e2c025..7e0a3805a77 100644 --- a/gcc/testsuite/g++.dg/template/copy1.C +++ b/gcc/testsuite/g++.dg/template/copy1.C @@ -6,10 +6,10 @@ struct A { - // { dg-error "reference" "" { target c++14_down } .+1 } A(A&); // { dg-message "A::A" "" { target c++14_down } } template A(T); // { dg-message "A::A" "" { target c++14_down } } }; +// { dg-error "reference" "" { target c++14_down } .+1 } A a = 0; // { dg-error "no match" "" { target c++14_down } } From patchwork Wed Dec 13 16:47:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1875800 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dSd9BBEK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Sr1dz0xTtz20LT for ; Thu, 14 Dec 2023 03:48:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 19934385E020 for ; Wed, 13 Dec 2023 16:48:53 +0000 (GMT) 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 CEFFB385B83B for ; Wed, 13 Dec 2023 16:47:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CEFFB385B83B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CEFFB385B83B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486087; cv=none; b=hZuMp3nTldUKTaG0ouhlWSLrWAihU5hUWPHU/90gfLZTRDuPC/9jG4kSPsKum/VT6IqmnxOZLHQicRWfsIEElNRKiINjj7DgwZFHtzRhO9wNEFSE3bjrNm4iWtHOOHNIpflVWqqHvDdQToCkGFnhKiGS6DE7IVfJkfrpm5AORQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486087; c=relaxed/simple; bh=0E9LFn/SglZuGb13z3xxG0TTOEOuLJO4VKb4O14WoOc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=VtaNhvnEs7fmcRGisJym9QtNyZRZbecBLxk7RvYyN7plktU5YZ6Zgj9Ck4Q3a83kxo+uK36B0PQxfIkiyXrcmZ1yofWz6EB3Ys9wSP21W/QW2Hm+gJqg1ms3dzpzqKIfQVqp1kjuKqsDF5pGWADFSH5yxWLqLl+hqggU7dBTLEs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702486077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZvHhUL+OxAp3TzVHkxcdE/nMPmQWK3POYOAnRiEhJtA=; b=dSd9BBEKpOGsE3q7DXHeF3tDcj1daaH8IUU3tKub8QY1dPSHLWQ1ce2L8go2CuiUtjGuoQ C5gLamfbTDGEBjxbs64zDVRZun3x9e8kM+PXBPwHTO8iarN3n6iCpGujf2eyk/se5R38pp QlIM3eAQEq9onk+LQP4HQ3qDHZnmPIY= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-32-8Q5BSHwMMFi2PxYu3f9Cew-1; Wed, 13 Dec 2023 11:47:45 -0500 X-MC-Unique: 8Q5BSHwMMFi2PxYu3f9Cew-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-77f7ba27036so480726885a.1 for ; Wed, 13 Dec 2023 08:47:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702486064; x=1703090864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZvHhUL+OxAp3TzVHkxcdE/nMPmQWK3POYOAnRiEhJtA=; b=ve7hTE0TGwQXrTBBCBj2JQUG80Ub3afVuj51PwJeCgk1GhOzIUTv9xib8UUH+AHv6x djrtmeCTbaU7AnhNaaM+gDrlhv8nYNHs21cQ89o0qHf3EvLSHMImIFuAmoZAHOtzdEJW 7p6+qQ05pPS6lwb4HxvzSj+tEfpcjpdqqVeNamybcBKsN68Ro2fvLIU1izBojINn19Mf //axeaumapFR8x0OkiUw+1J81g9dKYSs/S6ZdHbPc3EVIiC91pN1Mm6dH9/I7Jp9dXwS gf6qClM6KdyNiMtYuSc0r7NDQZqMTu+VWOfcav1trgLz1RwKYugxzk4I9s2S1P1trlDG nbvw== X-Gm-Message-State: AOJu0YyiMiyioID9GQG98Mx0wuMsSQfe+RRjbc5138brrNHV0UZb1ZIn hjnQbcpYda47+XqWXH4ZAcBKXCZUXgWG9eJLZv45TWlVvd8TmpbOCPwBBL3CyAeoUudxbgcvsga Kto85GUXoMDbc/bwY4nugyxYXbsmCBqyyxBRX6uZoMw5uYTA47yg3xPAxBhVNRqqWuSUr7IL5Tw == X-Received: by 2002:a05:620a:31aa:b0:77d:84d1:a594 with SMTP id bi42-20020a05620a31aa00b0077d84d1a594mr13402339qkb.10.1702486064683; Wed, 13 Dec 2023 08:47:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzuWHojoOjmn8OagboRACOKw2TkI6LZn1hSaYbzWoB69rpy4U8y1Krb9g2MJ+9wCGD6h2qsw== X-Received: by 2002:a05:620a:31aa:b0:77d:84d1:a594 with SMTP id bi42-20020a05620a31aa00b0077d84d1a594mr13402320qkb.10.1702486064241; Wed, 13 Dec 2023 08:47:44 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id j25-20020a05620a0a5900b0077f37896f24sm4613249qka.97.2023.12.13.08.47.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:47:43 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed 2/4] c++: constant direct-initialization [PR108243] Date: Wed, 13 Dec 2023 11:47:38 -0500 Message-Id: <20231213164740.1591535-2-jason@redhat.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213164740.1591535-1-jason@redhat.com> References: <20231213164740.1591535-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- When testing the proposed patch for PR71093 I noticed that it changed the diagnostic for consteval-prop6.C. I then noticed that the diagnostic wasn't very helpful either way; it was complaining about modification of the 'x' variable, but it's not a problem to initialize a local variable with a consteval constructor as long as the value is actually constant, we want to know why the value isn't constant. And then it turned out that this also fixed a missed-optimization bug in the testsuite. PR c++/108243 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_outermost_constant_expr): Turn a constructor CALL_EXPR into a TARGET_EXPR. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/consteval-prop6.C: Adjust diagnostic. * g++.dg/opt/is_constant_evaluated3.C: Remove xfails. --- gcc/cp/constexpr.cc | 16 +++++++++++++++- gcc/testsuite/g++.dg/cpp2a/consteval-prop6.C | 2 +- .../g++.dg/opt/is_constant_evaluated3.C | 8 ++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 58187a4fd12..4cf9dd71b05 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8651,7 +8651,21 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, } if (!object) { - if (TREE_CODE (t) == TARGET_EXPR) + if (TREE_CODE (t) == CALL_EXPR) + { + /* If T is calling a constructor to initialize an object, reframe + it as an AGGR_INIT_EXPR to avoid trying to modify an object + from outside the constant evaluation, which will fail even if + the value is actually constant (is_constant_evaluated3.C). */ + tree fn = cp_get_callee_fndecl_nofold (t); + if (fn && DECL_CONSTRUCTOR_P (fn)) + { + object = CALL_EXPR_ARG (t, 0); + object = build_fold_indirect_ref (object); + r = build_aggr_init_expr (type, r); + } + } + else if (TREE_CODE (t) == TARGET_EXPR) object = TARGET_EXPR_SLOT (t); else if (TREE_CODE (t) == AGGR_INIT_EXPR) object = AGGR_INIT_EXPR_SLOT (t); diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-prop6.C b/gcc/testsuite/g++.dg/cpp2a/consteval-prop6.C index 93ed398d9bf..ca7db7c63d3 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval-prop6.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval-prop6.C @@ -48,7 +48,7 @@ struct X { int a = sizeof(undef(0)); int x = undef(0); - X() = default; // { dg-error "modification of .x. is not a constant expression" } + X() = default; // { dg-error {'consteval int undef\(int\)' used before its definition} } }; void diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C index 0a1e46e5638..783127cf909 100644 --- a/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C +++ b/gcc/testsuite/g++.dg/opt/is_constant_evaluated3.C @@ -17,7 +17,7 @@ int main() { } // { dg-final { scan-tree-dump "a1 = {\\.n=42, \\.m=0}" "original" } } -// { dg-final { scan-tree-dump "a2 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } -// { dg-final { scan-tree-dump "a3 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } -// { dg-final { scan-tree-dump "a4 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } -// { dg-final { scan-tree-dump "a5 = {\\.n=42, \\.m=0}" "original" { xfail *-*-* } } } +// { dg-final { scan-tree-dump "a2 = {\\.n=42, \\.m=0}" "original" } } +// { dg-final { scan-tree-dump "a3 = {\\.n=42, \\.m=0}" "original" } } +// { dg-final { scan-tree-dump "a4 = {\\.n=42, \\.m=0}" "original" } } +// { dg-final { scan-tree-dump "a5 = {\\.n=42, \\.m=0}" "original" } } From patchwork Wed Dec 13 16:47:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1875798 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Q3Ok5PCN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Sr1dK1N87z20LT for ; Thu, 14 Dec 2023 03:48:21 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B35583860765 for ; Wed, 13 Dec 2023 16:48:18 +0000 (GMT) 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 0413D3857001 for ; Wed, 13 Dec 2023 16:47:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0413D3857001 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0413D3857001 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486087; cv=none; b=A5joBkJpWx5edDbXZYbxoTAq8vQsoyqh5jOkyDDQ94pOc6+eEDtBd5g4Bq9yEiozelr2npuPciyy+QOUrIjwiheD6otEhnTm7OnupCvFhN1cSPuDA3lQiP4CHw80ipKPYTrQcMGX/vVp/PAO0qsZQKhJKgnyuye7gAAx9dxTqao= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486087; c=relaxed/simple; bh=BKDayOjoRw97aeVTsrixomJU1K7QtGmubltXiCN5IG8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Y6bC1a0qFo2pHhF/6v4jVV6gca3gSF+dLkCb66iwrpMN30617jDHd5gkzTOYNWQO/ixrZyOs4fTcFvzxO+o0aVMTUc2YmKMDJFQEI4J7nyEPSwomghXFQDgMewIsgUJoU6YxTSI/ZGCnwk0xdEocyVvAxm4x/NXw/Fb4+H8hor4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702486076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uOsEPe1ZWRgKAULfx7WismIE7BcIIpo1SkPFjoXy9rc=; b=Q3Ok5PCNuNELwGAp6BkmdwpaKlwbOJeuh4tjhbnug1ijJ00EpVsMX2Vhz2vmiyqSptH3gG HKvyWVsjomzL+KD60W6BSVoyjcFFPoQd1mgWECooCp+FRw7A0B6MMbTqyD2FkXJstKRWO+ 3N/QE0eMHhRzpoFh0RU+9byl+Ro+FtM= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-316-a76eH9O6MCyj1V2Xt6zBrQ-1; Wed, 13 Dec 2023 11:47:46 -0500 X-MC-Unique: a76eH9O6MCyj1V2Xt6zBrQ-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-77f96e14ee2so86184185a.1 for ; Wed, 13 Dec 2023 08:47:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702486065; x=1703090865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uOsEPe1ZWRgKAULfx7WismIE7BcIIpo1SkPFjoXy9rc=; b=QQkPexd8doEcZ4XpJgKLWUzXtUG1c27DhxNZConmcaSTKzquBEZFh363DoVMYqdx/H rtSYi4FtjMbzBpNVAOeskr6b5+AHKVmRN9+32qk+ukQwLwP8x31HUjyFardf/TNe4eLy Ga8NNmVDDHQauDz0DiMc11MWKfSav0OOOLbdjeuXwf7ljJ+4KuJG/DYPnU1L9654AvHA z7wliNvQ0fLA0/BTbnHV554SlorBj/9uVZPhSiEMpL9rzSDdFGyffh4TT2ISdij1NtDH EZ+cWYi4agVUbtbaF+lfQkAc4GKne3GeUT1pzclQAJ3PQ656Uj501/0NDa2f0wpf0po+ 0+Ew== X-Gm-Message-State: AOJu0YynMkhVnux7+aPcsHs01ExfBWw5xjI0Ggz7vMaSwN1JbFtz0E7X 76XYnoBbFqU/3mkBdNA0/XHOTngP6JDBQjJWIycRA6UnQDSVsSDeey7KNMzdeYeuKhp9pC66lCM gKBSy7BifpxSgYVzP1wxT/I+DYe9jfJxhaKF33jc4+EowJy10Ioenq49LsybBELZNYoLLKvXg9g == X-Received: by 2002:a05:620a:5603:b0:77f:272f:1e5a with SMTP id vu3-20020a05620a560300b0077f272f1e5amr9317050qkn.60.1702486065416; Wed, 13 Dec 2023 08:47:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IE/lRq1dJCdA+47vgdniTMMK1kihfJAReahOD+IT0HhsTF1NY2UoI5OgeqmIUWg/6eDn1YLJQ== X-Received: by 2002:a05:620a:5603:b0:77f:272f:1e5a with SMTP id vu3-20020a05620a560300b0077f272f1e5amr9317042qkn.60.1702486065048; Wed, 13 Dec 2023 08:47:45 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id j25-20020a05620a0a5900b0077f37896f24sm4613249qka.97.2023.12.13.08.47.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:47:44 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed 3/4] c++: fix in-charge parm in constexpr Date: Wed, 13 Dec 2023 11:47:39 -0500 Message-Id: <20231213164740.1591535-3-jason@redhat.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213164740.1591535-1-jason@redhat.com> References: <20231213164740.1591535-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, 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-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- I was puzzled by the proposed patch for PR71093 specifically ignoring the in-charge parameter; the problem turned out to be that when cxx_eval_call_expression jumps from the clone to the cloned function, it assumes that the latter has the same parameters, and so the in-charge parm doesn't get an argument. Since a class with vbases can't have constexpr 'tors there isn't actually a need for an in-charge parameter in a destructor, but we used to use it for deleting destructors and never removed it. I have a patch to do that for GCC 15, but for now let's work around it. gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_call_expression): Handle missing in-charge argument. --- gcc/cp/constexpr.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 4cf9dd71b05..9d9e96c2afd 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3169,6 +3169,19 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, ctx->global->put_value (remapped, arg); remapped = DECL_CHAIN (remapped); } + for (; remapped; remapped = TREE_CHAIN (remapped)) + if (DECL_NAME (remapped) == in_charge_identifier) + { + /* FIXME destructors unnecessarily have in-charge parameters + even in classes without vbases, map it to 0 for now. */ + gcc_assert (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))); + ctx->global->put_value (remapped, integer_zero_node); + } + else + { + gcc_assert (seen_error ()); + *non_constant_p = true; + } /* Add the RESULT_DECL to the values map, too. */ gcc_assert (!DECL_BY_REFERENCE (res)); ctx->global->put_value (res, NULL_TREE); From patchwork Wed Dec 13 16:47:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1875799 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=M7CDsGv1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Sr1dM6g2Tz20LT for ; Thu, 14 Dec 2023 03:48:23 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B3614386184F for ; Wed, 13 Dec 2023 16:48:20 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 721C5385C411 for ; Wed, 13 Dec 2023 16:48:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 721C5385C411 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 721C5385C411 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486087; cv=none; b=Oi/3v3nOM+tjsyDPr0/cp68maxagQyWZlCcmvvo0uNGcje24xIYvHn0vjp62rCRIXprHRMbjKbesMkiJ6L+iHSkWdHxmyD1lAPkAkQk64RMuxsiEBxSk1+oteueDeuUC+mEhmGyzA21F24JSrnnb72C2b22Bx7jk0rm7QViwR1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702486087; c=relaxed/simple; bh=WCsKgQ01HAQdMCuRDz2mZv/d4z6+8sezQ1pFzsE9QLk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=k0VVPq5vlM6lFRTTvWpjfUt14IplbB7ffIxhYmd5WJVW+blhImPcpnJbxxYEB1Lox42ysStHX8qa/Z7hS0EpkTbzxRYWz89+OiolknxfCjwc7VSJ7V7zfBc/evz6saNclmNIzxMXFHQ+Yq7u78Db/yVKVL5fMO9Lqv6sHg2KfJ8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702486080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S1OtABJKlvAdlO73fQ/FPvdGjgYsVreLeZdlnFt6PjE=; b=M7CDsGv1X437cZjxT5xCFFLiU/NKI8IdmJ7BIZNkczPGXmIE7iaej04Js64KGQvn/+9WpF jEVV49G2Ou/9oof9Ef0+SQ7sBmbKaXnxHKndohEo8evgb/EZsO7fCqGgNmyNN8MUj5FdNe tJTiPeLgMjv0J4ehZSc8wifeYAxFG74= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-281--3r5CFA7Pvmo0sLJUbZYRQ-1; Wed, 13 Dec 2023 11:47:48 -0500 X-MC-Unique: -3r5CFA7Pvmo0sLJUbZYRQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-67ccd612093so95421536d6.1 for ; Wed, 13 Dec 2023 08:47:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702486067; x=1703090867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S1OtABJKlvAdlO73fQ/FPvdGjgYsVreLeZdlnFt6PjE=; b=iPCsdV5+CtIwBwUa1eFSawFAUXYJ6pfFMI0xLJ7P2jDxpALr4b5oA3eYN78HM6EcpI q2HWr1YGm3wENpGVLQgw/mceVSLZ1D7loMyQ318vkRVU4mONmiAlh5wu+pnwUq8NyKct BQBK5Bgq4gsfqjV4oBHO/wWvCfe+0AazESjCKy+2JVaNsul3/KCP3mC2nx5j/ZB85DHv TQcqZgoEew45sFxgs+FV/K8PZ81rvLbBfGN0yLqAHSjYlbzAYeczwmi+DSrMWYFo4GwX jhC2vi1wdOGtwYXSbjytWdtXTSspg2kIN+tBa2cyVn64ohxfxEuyZeTU/zAjCb+QgDzT G3Bg== X-Gm-Message-State: AOJu0YzIL0mf+pmqh/yYfoeKbNpa9n3CnR5Awt/VBVxPGwMWypGaDJz0 7UUbsgY+iOMjURrOSiFOWYhahCAED8yayBIGk59xFBnBhTMPETbxGq5hgiSJOLuiOdboSSXzVW1 jeZztUybjjlyVqcTOCKlOVAKOE5k3FjW0W0DZCsT9QcX7XwiXNkUWDnA5H+pIkJ7aDSIlrVKSwQ == X-Received: by 2002:a05:6214:5292:b0:67e:ee0f:b6aa with SMTP id kj18-20020a056214529200b0067eee0fb6aamr2872454qvb.6.1702486067218; Wed, 13 Dec 2023 08:47:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPOyBf85XyU9ppsScfmRdyA4ru0bZsZpkVnY8BJIGj5b2jUY9jWFL9Fvlfrk2boB0jxiMRYw== X-Received: by 2002:a05:6214:5292:b0:67e:ee0f:b6aa with SMTP id kj18-20020a056214529200b0067eee0fb6aamr2872440qvb.6.1702486066453; Wed, 13 Dec 2023 08:47:46 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id j25-20020a05620a0a5900b0077f37896f24sm4613249qka.97.2023.12.13.08.47.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:47:45 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Cc: Nathaniel Shead Subject: [pushed 4/4] c++: End lifetime of objects in constexpr after destructor call [PR71093] Date: Wed, 13 Dec 2023 11:47:40 -0500 Message-Id: <20231213164740.1591535-4-jason@redhat.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213164740.1591535-1-jason@redhat.com> References: <20231213164740.1591535-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, 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-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. This is modified from Nathaniel's last version by adjusting for my recent CLOBBER changes and removing the special handling of __in_chrg which is no longer needed since my previous commit. -- 8< -- This patch adds checks for using objects after they've been manually destroyed via explicit destructor call. Currently this is only implemented for 'top-level' objects; FIELD_DECLs and individual elements of arrays will need a lot more work to track correctly and are left for a future patch. The other limitation is that destruction of parameter objects is checked too 'early', happening at the end of the function call rather than the end of the owning full-expression as they should be for consistency; see cpp2a/constexpr-lifetime2.C. This is because I wasn't able to find a good way to link the constructed parameter declarations with the variable declarations that are actually destroyed later on to propagate their lifetime status, so I'm leaving this for a later patch. PR c++/71093 gcc/cp/ChangeLog: * constexpr.cc (constexpr_global_ctx::get_value_ptr): Don't return NULL_TREE for objects we're initializing. (constexpr_global_ctx::destroy_value): Rename from remove_value. Only mark real variables as outside lifetime. (constexpr_global_ctx::clear_value): New function. (destroy_value_checked): New function. (cxx_eval_call_expression): Defer complaining about non-constant arg0 for operator delete. Use remove_value_safe. (cxx_fold_indirect_ref_1): Handle conversion to 'as base' type. (outside_lifetime_error): Include name of object we're accessing. (cxx_eval_store_expression): Handle clobbers. Improve error messages. (cxx_eval_constant_expression): Use remove_value_safe. Clear bind variables before entering body. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-lifetime1.C: Improve error message. * g++.dg/cpp1y/constexpr-lifetime2.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime3.C: Likewise. * g++.dg/cpp1y/constexpr-lifetime4.C: Likewise. * g++.dg/cpp2a/bitfield2.C: Likewise. * g++.dg/cpp2a/constexpr-new3.C: Likewise. New check. * g++.dg/cpp1y/constexpr-lifetime7.C: New test. * g++.dg/cpp2a/constexpr-lifetime1.C: New test. * g++.dg/cpp2a/constexpr-lifetime2.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/constexpr.cc | 148 +++++++++++++++--- .../g++.dg/cpp1y/constexpr-lifetime1.C | 2 +- .../g++.dg/cpp1y/constexpr-lifetime2.C | 2 +- .../g++.dg/cpp1y/constexpr-lifetime3.C | 2 +- .../g++.dg/cpp1y/constexpr-lifetime4.C | 2 +- .../g++.dg/cpp1y/constexpr-lifetime7.C | 93 +++++++++++ gcc/testsuite/g++.dg/cpp2a/bitfield2.C | 2 +- .../g++.dg/cpp2a/constexpr-lifetime1.C | 21 +++ .../g++.dg/cpp2a/constexpr-lifetime2.C | 23 +++ gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C | 17 +- 10 files changed, 284 insertions(+), 28 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime2.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 9d9e96c2afd..e1b2d27fc36 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1193,13 +1193,20 @@ public: return *p; return NULL_TREE; } - tree *get_value_ptr (tree t) + tree *get_value_ptr (tree t, bool initializing) { if (modifiable && !modifiable->contains (t)) return nullptr; if (tree *p = values.get (t)) - if (*p != void_node) - return p; + { + if (*p != void_node) + return p; + else if (initializing) + { + *p = NULL_TREE; + return p; + } + } return nullptr; } void put_value (tree t, tree v) @@ -1208,13 +1215,19 @@ public: if (!already_in_map && modifiable) modifiable->add (t); } - void remove_value (tree t) + void destroy_value (tree t) { - if (DECL_P (t)) + if (TREE_CODE (t) == VAR_DECL + || TREE_CODE (t) == PARM_DECL + || TREE_CODE (t) == RESULT_DECL) values.put (t, void_node); else values.remove (t); } + void clear_value (tree t) + { + values.remove (t); + } }; /* Helper class for constexpr_global_ctx. In some cases we want to avoid @@ -1238,7 +1251,7 @@ public: ~modifiable_tracker () { for (tree t: set) - global->remove_value (t); + global->clear_value (t); global->modifiable = nullptr; } }; @@ -1278,6 +1291,40 @@ struct constexpr_ctx { mce_value manifestly_const_eval; }; +/* Remove T from the global values map, checking for attempts to destroy + a value that has already finished its lifetime. */ + +static void +destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p) +{ + if (t == error_mark_node || TREE_TYPE (t) == error_mark_node) + return; + + /* Don't error again here if we've already reported a problem. */ + if (!*non_constant_p + && DECL_P (t) + /* Non-trivial destructors have their lifetimes ended explicitly + with a clobber, so don't worry about it here. */ + && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)) + /* ...except parameters are remapped in cxx_eval_call_expression, + and the destructor call during cleanup won't be able to tell that + this value has already been destroyed, so complain now. This is + not quite unobservable, but is extremely unlikely to crop up in + practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */ + || TREE_CODE (t) == PARM_DECL) + && ctx->global->is_outside_lifetime (t)) + { + if (!ctx->quiet) + { + auto_diagnostic_group d; + error ("destroying %qE outside its lifetime", t); + inform (DECL_SOURCE_LOCATION (t), "declared here"); + } + *non_constant_p = true; + } + ctx->global->destroy_value (t); +} + /* This internal flag controls whether we should avoid doing anything during constexpr evaluation that would cause extra DECL_UID generation, such as template instantiation and function body copying. */ @@ -2806,6 +2853,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, && (CALL_FROM_NEW_OR_DELETE_P (t) || is_std_allocator_allocate (ctx->call))) { + const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun)); const int nargs = call_expr_nargs (t); tree arg0 = NULL_TREE; for (int i = 0; i < nargs; ++i) @@ -2813,12 +2861,15 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, tree arg = CALL_EXPR_ARG (t, i); arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, non_constant_p, overflow_p); - VERIFY_CONSTANT (arg); + /* Deleting a non-constant pointer has a better error message + below. */ + if (new_op_p || i != 0) + VERIFY_CONSTANT (arg); if (i == 0) arg0 = arg; } gcc_assert (arg0); - if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) + if (new_op_p) { tree type = build_array_type_nelts (char_type_node, tree_to_uhwi (arg0)); @@ -2867,7 +2918,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, return t; } DECL_NAME (var) = heap_deleted_identifier; - ctx->global->remove_value (var); + ctx->global->destroy_value (var); ctx->global->heap_dealloc_count++; return void_node; } @@ -2890,7 +2941,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, return t; } DECL_NAME (var) = heap_deleted_identifier; - ctx->global->remove_value (var); + ctx->global->destroy_value (var); ctx->global->heap_dealloc_count++; return void_node; } @@ -3255,9 +3306,9 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, non_constant_p, overflow_p); /* Remove the parms/result from the values map. */ - ctx->global->remove_value (res); + destroy_value_checked (ctx, res, non_constant_p); for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) - ctx->global->remove_value (parm); + destroy_value_checked (ctx, parm, non_constant_p); /* Free any parameter CONSTRUCTORs we aren't returning directly. */ while (!ctors->is_empty ()) @@ -5657,6 +5708,10 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, } } + /* Handle conversion to "as base" type. */ + if (CLASSTYPE_AS_BASE (optype) == type) + return op; + /* Handle conversion to an empty base class, which is represented with a NOP_EXPR. Do this before spelunking into the non-empty subobjects, which is likely to be a waste of time (109678). */ @@ -5908,7 +5963,7 @@ outside_lifetime_error (location_t loc, tree r) } else { - error_at (loc, "accessing object outside its lifetime"); + error_at (loc, "accessing %qE outside its lifetime", r); inform (DECL_SOURCE_LOCATION (r), "declared here"); } } @@ -6125,8 +6180,10 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, constexpr_ctx new_ctx = *ctx; tree init = TREE_OPERAND (t, 1); - if (TREE_CLOBBER_P (init)) - /* Just ignore clobbers. */ + + if (TREE_CLOBBER_P (init) + && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) + /* Only handle clobbers ending the lifetime of objects. */ return void_node; /* First we figure out where we're storing to. */ @@ -6136,7 +6193,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, tree type = TREE_TYPE (target); bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; - if (preeval) + if (preeval && !TREE_CLOBBER_P (init)) { /* Evaluate the value to be stored without knowing what object it will be stored in, so that any side-effects happen first. */ @@ -6244,11 +6301,18 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, && const_object_being_modified == NULL_TREE) const_object_being_modified = object; + if (DECL_P (object) + && TREE_CLOBBER_P (init) + && DECL_NAME (object) == heap_deleted_identifier) + /* Ignore clobbers of deleted allocations for now; we'll get a better error + message later when operator delete is called. */ + return void_node; + /* And then find/build up our initializer for the path to the subobject we're initializing. */ tree *valp; if (DECL_P (object)) - valp = ctx->global->get_value_ptr (object); + valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); else valp = NULL; if (!valp) @@ -6256,10 +6320,45 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, /* A constant-expression cannot modify objects from outside the constant-expression. */ if (!ctx->quiet) - error ("modification of %qE is not a constant expression", object); + { + auto_diagnostic_group d; + if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier) + { + error ("modification of allocated storage after deallocation " + "is not a constant expression"); + inform (DECL_SOURCE_LOCATION (object), "allocated here"); + } + else if (DECL_P (object) && ctx->global->is_outside_lifetime (object)) + { + if (TREE_CLOBBER_P (init)) + error ("destroying %qE outside its lifetime", object); + else + error ("modification of %qE outside its lifetime " + "is not a constant expression", object); + inform (DECL_SOURCE_LOCATION (object), "declared here"); + } + else + { + if (TREE_CLOBBER_P (init)) + error ("destroying %qE from outside current evaluation " + "is not a constant expression", object); + else + error ("modification of %qE from outside current evaluation " + "is not a constant expression", object); + } + } *non_constant_p = true; return t; } + + /* Handle explicit end-of-lifetime. */ + if (TREE_CLOBBER_P (init)) + { + if (refs->is_empty ()) + ctx->global->destroy_value (object); + return void_node; + } + type = TREE_TYPE (object); bool no_zero_init = true; @@ -6533,7 +6632,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, /* The hash table might have moved since the get earlier, and the initializer might have mutated the underlying CONSTRUCTORs, so we must recompute VALP. */ - valp = ctx->global->get_value_ptr (object); + valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); for (unsigned i = 0; i < vec_safe_length (indexes); i++) { ctors[i] = valp; @@ -7650,7 +7749,7 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, /* Forget SAVE_EXPRs and TARGET_EXPRs created by this full-expression. */ for (tree save_expr : save_exprs) - ctx->global->remove_value (save_expr); + destroy_value_checked (ctx, save_expr, non_constant_p); } break; @@ -8203,13 +8302,18 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, non_constant_p, overflow_p, jump_target); case BIND_EXPR: + /* Pre-emptively clear the vars declared by this BIND_EXPR from the value + map, so that when checking whether they're already destroyed later we + don't get confused by remnants of previous calls. */ + for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) + ctx->global->clear_value (decl); r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), lval, non_constant_p, overflow_p, jump_target); for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) - ctx->global->remove_value (decl); - return r; + destroy_value_checked (ctx, decl, non_constant_p); + break; case PREINCREMENT_EXPR: case POSTINCREMENT_EXPR: diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C index 43aa7c974c1..3fda29e0cc2 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C @@ -10,4 +10,4 @@ constexpr const int& test() { auto local = S{}; // { dg-message "note: declared here" } return local.get(); } -constexpr int x = test(); // { dg-error "accessing object outside its lifetime" } +constexpr int x = test(); // { dg-error "accessing .local. outside its lifetime" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C index 2f5ae8db6d5..d82ba5c8b73 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C @@ -8,7 +8,7 @@ struct S { constexpr int error() { const auto& local = S{}.get(); // { dg-message "note: declared here" } - return local; // { dg-error "accessing object outside its lifetime" } + return local; // { dg-error "accessing '\[^'\]+' outside its lifetime" } } constexpr int x = error(); // { dg-message "in .constexpr. expansion" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C index 53785521d05..67e9b91c723 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C @@ -7,7 +7,7 @@ constexpr int f(int i) { int j = 123; // { dg-message "note: declared here" } p = &j; } - return *p; // { dg-error "accessing object outside its lifetime" } + return *p; // { dg-error "accessing 'j' outside its lifetime" } } constexpr int i = f(0); // { dg-message "in .constexpr. expansion" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C index 181a1201663..6f0d749dcf2 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C @@ -5,7 +5,7 @@ constexpr const double& test() { return local; } -static_assert(test() == 3.0, ""); // { dg-error "constant|accessing object outside its lifetime" } +static_assert(test() == 3.0, ""); // { dg-error "constant|accessing '\[^'\]+' outside its lifetime" } // no deference, shouldn't error static_assert((test(), true), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C new file mode 100644 index 00000000000..4148f42f7be --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C @@ -0,0 +1,93 @@ +// PR c++/71093 +// { dg-do compile { target c++14 } } + +constexpr int f (const int *p) +{ + typedef int T; + p->~T (); // { dg-error "destroying" } + return *p; +} + +constexpr int i = 0; +constexpr int j = f (&i); + + +template +constexpr bool test_access() { + T x {}; + x.~T(); + T y = x; // { dg-error "lifetime" } + return true; +} + +template +constexpr bool test_modification() { + T x {}; + x.~T(); + x = T(); // { dg-error "lifetime" } + return true; +} + +template +constexpr bool test_scope() { + { + T x {}; + x.~T(); + } // { dg-error "destroying" } + return true; +} + +template +constexpr bool test_destroy_temp() { + T{}.~T(); // { dg-error "destroying" } + return true; +} + +template +constexpr bool test_parameter(T t) { + // note: error message occurs at point of call + t.~T(); + return true; +} + +template +constexpr void test_bindings_impl(int n) { + if (n == 0) return; + T a {}; + if (n == 1) return; + T b {}; +} + +template +constexpr bool test_bindings() { + test_bindings_impl(1); + test_bindings_impl(0); + test_bindings_impl(2); + return true; +} + +constexpr bool i1 = test_access(); // { dg-message "in .constexpr." } +constexpr bool i2 = test_modification(); // { dg-message "in .constexpr." } +constexpr bool i3 = test_scope(); // { dg-message "in .constexpr." } +constexpr bool i4 = test_destroy_temp(); // { dg-message "in .constexpr." "" { xfail *-*-* } } +constexpr bool i5 = test_parameter(int{}); // { dg-error "destroying" } +constexpr bool i6 = test_bindings(); + +struct Trivial { int x; }; +constexpr bool t1 = test_access(); // { dg-message "in .constexpr." } +constexpr bool t2 = test_modification(); // { dg-message "in .constexpr." } +constexpr bool t3 = test_scope(); // { dg-message "in .constexpr." } +constexpr bool t4 = test_destroy_temp(); // { dg-message "in .constexpr." } +constexpr bool t5 = test_parameter(Trivial{}); // { dg-error "destroying" } +constexpr bool t6 = test_bindings(); + +#if __cplusplus >= 202002L +struct NonTrivial { int x; constexpr ~NonTrivial() {} }; // { dg-error "destroying" "" { target c++20 } } +constexpr bool n1 = test_access(); // { dg-message "in .constexpr." "" { target c++20 } } +constexpr bool n2 = test_modification(); // { dg-message "in .constexpr." "" { target c++20 } } +constexpr bool n3 = test_scope(); // { dg-message "in .constexpr." "" { target c++20 } } +constexpr bool n4 = test_destroy_temp(); // { dg-message "in .constexpr." "" { target c++20 } } +constexpr bool n5 = test_parameter(NonTrivial{}); // { dg-error "destroying" "" { target c++20 } } +constexpr bool n6 = test_bindings(); +#endif + diff --git a/gcc/testsuite/g++.dg/cpp2a/bitfield2.C b/gcc/testsuite/g++.dg/cpp2a/bitfield2.C index dcb424fc8f6..885d4f0e26d 100644 --- a/gcc/testsuite/g++.dg/cpp2a/bitfield2.C +++ b/gcc/testsuite/g++.dg/cpp2a/bitfield2.C @@ -13,7 +13,7 @@ template struct U { int j : W = 7; // { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } } int k : W { 8 }; // { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } } - int l : V ? 7 : a = 3; // { dg-error "modification of .a. is not a constant expression" } + int l : V ? 7 : a = 3; // { dg-error "modification of .a. from outside current evaluation is not a constant expression" } // { dg-error "width not an integer constant" "" { target *-*-* } .-1 } int m : (V ? W : b) = 9; // { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } } // { dg-error "zero width for bit-field" "" { target *-*-* } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime1.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime1.C new file mode 100644 index 00000000000..36163844eca --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime1.C @@ -0,0 +1,21 @@ +// { dg-do compile { target c++20 } } + +#include "construct_at.h" + +struct S { int x; }; +constexpr int f() { + S s; + s.~S(); + std::construct_at(&s, 5); + return s.x; +} +static_assert(f() == 5); + +struct T { int x; constexpr ~T() {} }; +constexpr int g() { + T t; + t.~T(); + std::construct_at(&t, 12); + return t.x; +} +static_assert(g() == 12); diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime2.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime2.C new file mode 100644 index 00000000000..56cc9e3c1c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-lifetime2.C @@ -0,0 +1,23 @@ +// { dg-do compile { target c++20 } } + +#include "construct_at.h" + +struct S { int x; }; + +constexpr bool foo(S s, S*& p) { + p = &s; + s.~S(); + return true; +} + +constexpr bool bar() { + // This is, strictly speaking, implementation-defined behaviour; + // see [expr.call] p6. However, in all other cases we destroy + // at the end of the full-expression, so the below should be fixed. + S* p; + foo(S{}, p), std::construct_at(p); // { dg-bogus "destroying" "" { xfail *-*-* } } + + return true; +} + +constexpr bool x = bar(); diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C index 3ba440fec53..5d9f192507b 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new3.C @@ -34,7 +34,7 @@ constexpr auto v3 = f3 (); // { dg-message "in 'constexpr' expansion of" } constexpr bool f4 (int *p) { - delete p; // { dg-error "deallocation of storage that was not previously allocated" } + delete p; // { dg-error "destroying 'q' from outside current evaluation" } return false; } @@ -70,3 +70,18 @@ f7 () } constexpr auto v7 = f7 (); + +constexpr bool +f8_impl (int *p) +{ + delete p; // { dg-error "deallocation of storage that was not previously allocated" } + return false; +} + +constexpr bool +f8 () +{ + int q = 0; + return f8_impl (&q); +} +constexpr auto v8 = f8 (); // { dg-message "in 'constexpr' expansion of" }