From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 E44643858C78 for ; Fri, 3 Mar 2023 15:18:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E44643858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677856706; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=MD/BYZvMK92k1WvLPvVbBqLHHCdGPDNhlVxl1xPzpTg=; b=ajxu/u5LopGQZ1IRnf/XZPNlQAnoYYj8Y0ogNOpEQ2eU100aHIEG+9aHbC6lkOgoaicpwF kdUgz5wzPIX4ROiaixx0K6CORdmEUHorGmSGPFGYZxJ0qo+pRlBfZuUYwWFp8mhfZ9hJY4 TUrWb01Zll5tuebim8SRcuJz63Gny/o= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-638-W-MQ7n_oN4yRzu_ZfCYbuQ-1; Fri, 03 Mar 2023 10:18:25 -0500 X-MC-Unique: W-MQ7n_oN4yRzu_ZfCYbuQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C8B181C0754B for ; Fri, 3 Mar 2023 15:18:24 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 87D9B440D8; Fri, 3 Mar 2023 15:18:24 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 323FIMe52485283 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 3 Mar 2023 16:18:22 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 323FIMDe2485282; Fri, 3 Mar 2023 16:18:22 +0100 Date: Fri, 3 Mar 2023 16:18:21 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Don't defer local statics initialized with constant expressions [PR108702] Message-ID: Reply-To: Jakub Jelinek References: <5aff4f89-5f00-c71a-5681-3d481a354c9b@redhat.com> MIME-Version: 1.0 In-Reply-To: <5aff4f89-5f00-c71a-5681-3d481a354c9b@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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=-3.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, Mar 02, 2023 at 11:48:04AM -0500, Jason Merrill wrote: > > The stmtexpr19.C testcase used to be rejected as it has a static > > variable in statement expression in constexpr context, but as that > > static variable is initialized by constant expression, when P2647R1 > > was implemented we agreed to make it valid. > > > > Now, as reported, the testcase compiles fine, but doesn't actually link > > because the static variable isn't defined anywhere, and with -flto ICEs > > because of this problem. This is because we never > > varpool_node::finalize_decl those vars, the constant expression in which > > the DECL_EXPR is present for the static VAR_DECL is folded (constant > > evaluated) into just the address of the VAR_DECL. > > Would it make sense to define it when we see the DECL_EXPR in constant > evaluation? So like this? Passes GXX_TESTSUITE_STDS=98,11,14,17,20,2b make check-g++ so far. 2023-03-03 Jakub Jelinek PR c++/108702 * constexpr.cc: Include toplev.h. (cxx_eval_constant_expression) : When seeing a local static initialized by constant expression outside of a constexpr function which has been deferred by make_rtl_for_nonlocal_decl, call rest_of_decl_compilation on it. * g++.dg/ext/stmtexpr19.C: Use dg-do link rather than dg-do compile. --- gcc/cp/constexpr.cc.jj 2023-03-03 00:34:44.113679918 +0100 +++ gcc/cp/constexpr.cc 2023-03-03 13:26:57.602871900 +0100 @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3. #include "attribs.h" #include "fold-const.h" #include "intl.h" +#include "toplev.h" static bool verify_constant (tree, bool, bool *, bool *); #define VERIFY_CONSTANT(X) \ @@ -7127,6 +7128,24 @@ cxx_eval_constant_expression (const cons break; } + /* make_rtl_for_nonlocal_decl could have deferred emission of + a local static var, but if it appears in a statement expression + which is constant expression evaluated to e.g. just the address + of the variable, its DECL_EXPR will never be seen during + gimple lowering's record_vars_into as the statement expression + will not be in the IL at all. */ + if (VAR_P (r) + && TREE_STATIC (r) + && !DECL_REALLY_EXTERN (r) + && DECL_FUNCTION_SCOPE_P (r) + && !var_in_maybe_constexpr_fn (r) + && decl_constant_var_p (r)) + { + varpool_node *node = varpool_node::get (r); + if (node == NULL || !node->definition) + rest_of_decl_compilation (r, 0, at_eof); + } + if (AGGREGATE_TYPE_P (TREE_TYPE (r)) || VECTOR_TYPE_P (TREE_TYPE (r))) { --- gcc/testsuite/g++.dg/ext/stmtexpr19.C.jj 2023-02-09 15:52:29.623359240 +0100 +++ gcc/testsuite/g++.dg/ext/stmtexpr19.C 2023-03-03 12:24:20.217186735 +0100 @@ -1,6 +1,6 @@ // PR c++/81073 // { dg-options "" } -// { dg-do compile { target c++11 } } +// { dg-do link { target c++11 } } struct test { const int *addr; }; Jakub