From patchwork Thu Nov 17 09:13:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 21516 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp295296wrr; Thu, 17 Nov 2022 01:15:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf5mXGo7B3VpMpZ9lkDD9f6D5NVa/+/IIeXS7VKV2Zvhwufh0O49ySF2t0L3FpKKBJnzzQb5 X-Received: by 2002:a05:6402:48e:b0:463:ab06:f338 with SMTP id k14-20020a056402048e00b00463ab06f338mr1337462edv.371.1668676514253; Thu, 17 Nov 2022 01:15:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668676514; cv=none; d=google.com; s=arc-20160816; b=q1tiTIBursitYWDzP6cQBoiM87+XP5npiDphCd0hSPv3J9YxoOd15Q4ex1t8RViOCX +DmDt0P/G0LwQYX/SvcjBMq6Xg/6M4eqJbJq+COREe2jUNMuF+YJWcbUmvmPpvkXlD8f b/SaNeN/Jk3iv/gyp0A45fdyn94hqQysGkAHIovw67oOhDPi7FTNTYIeRJOwaO9X746B tLOyShLIIg3AfuJSnjjsQj/jPLYQ+NCJbyom3AQKh+sywjnooaDh6JJbvY9uMMCp+vXe VnOpNVkpaO2yqQHJ66VQMENQrDlILFqrDcNduxbSwmXYl9IXB7uAgO6DHTvujDb4CPqn 4EXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=eng1Ma76UHe78YNZ9EWrZd7HsoiYekNC1ZBPztihjAw=; b=je6xLM7c9gUPf3sF/AxBH6bNjML3eXBTF+6Ea6Cwxkhrx9LeyTU8k7tPH9XUZT7fHX 6HSfVm7c5iFRh2iMMO2S7XCPBdRfFl+nVmNtbI0Zx5Xh/S99h4T1FNJMNFc95qsIEE6W GsXf/u6IZ4zRuRU4Nk7oh/gtMD+iYXvxPuflJQbNOFKh7U7rgwkEZUmYUkNl+L75C8PM gfqtZUwKAX/lice1ZukAUKM0VTGxkA/2wzXrmLrR1tHYXKAMyV6L4y86hUEevA0WCQxc DjeWF+LPt7wtCChEL3JP+tt+2AWHSR0GCKzjsglwExxJBZcSghbw1U3rxO/JjPMQsY2Z Km/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=K42Y0nZC; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w4-20020a05640234c400b0045cd68a2bc5si544701edc.17.2022.11.17.01.15.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 01:15:14 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=K42Y0nZC; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 281BF394D83B for ; Thu, 17 Nov 2022 09:15:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 281BF394D83B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668676513; bh=eng1Ma76UHe78YNZ9EWrZd7HsoiYekNC1ZBPztihjAw=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=K42Y0nZCK6xmw7rfhkLEVEz8DF8ievDiWwjm3L2RLkj2/8wdy+8hSBN8dDV6X8DI9 63JKUKA2mItTsKsIfYLMRGlTIdZ0x6U/FIYAfELHLxj6LPwvv94RZWpxBRdfV+6YUC 0lKAzjcUyaNerfMTej1RS3NGYNpMDCL/O1XjEes8= 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 80474394D83B for ; Thu, 17 Nov 2022 09:14:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 80474394D83B Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-433-GZqKEix-OlKEk7ZjYSIk2Q-1; Thu, 17 Nov 2022 04:14:04 -0500 X-MC-Unique: GZqKEix-OlKEk7ZjYSIk2Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7A6F888B7A8 for ; Thu, 17 Nov 2022 09:14:03 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 157A240C6EC3; Thu, 17 Nov 2022 09:14:02 +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 2AH9DrtN012517 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 17 Nov 2022 10:13:53 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AH9Drlh012516; Thu, 17 Nov 2022 10:13:53 +0100 Date: Thu, 17 Nov 2022 10:13:52 +0100 To: Jason Merrill Cc: Jonathan Wakely , gcc-patches@gcc.gnu.org, Marek Polacek Subject: [PATCH] c++, v3: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions Message-ID: References: <016f168b-f143-baff-5f71-c48d4611ae11@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 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 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749734144157828409?= X-GMAIL-MSGID: =?utf-8?q?1749734144157828409?= On Wed, Nov 16, 2022 at 03:26:32PM -0500, Jason Merrill wrote: > On 11/16/22 09:46, Jakub Jelinek wrote: > > On Wed, Nov 16, 2022 at 09:33:27AM -0500, Jason Merrill wrote: > > > > and at that point I fear decl_maybe_constant_var_p will not work > > > > properly. Shall this hunk be moved somewhere else (cp_finish_decl?) > > > > where we can already call it, or do the above in start_decl for > > > > cxx_dialect < cxx20 and add a cxx_dialect == cxx20 hunk in cp_finish_decl? > > > > > > Hmm, I'd expect decl_maybe_constant_var_p to work fine at this point. > > > > For static constexpr vars sure, but what about > > static const where start_decl doesn't know the initializer? > > Sure, decl_maybe_constant_var_p will not crash in that case, but > > it will return true even if the static const var doesn't have > > a constant initializer. Sure, we'd catch that later on when actually > > trying to constexpr evaluate the function and hitting there the > > spots added for C++23 in potential_constant_expression*/cxx_eval_*, > > but it would mean that we don't reject it when nothing calls the functions. > > > > I meant something like: > > constexpr int bar (int x) { if (x) throw 1; return 0; } > > constexpr int foo () { static const int a = bar (1); return 0; } > > with -std=c++20 IMHO shouldn't be accepted, while in C++23 it should. > > I'd expect us to reject that in C++20 in potential_constant_expression, but > it's a fair point; it is awkward that P2242 wasn't also accepted as a DR. > > Moving the check from start_decl to cp_finish_decl makes sense to me. So like this? I had to outline the check from start_decl to a function because it is needed in cp_finish_decl in two different places (the processing_template_decl path and at the end and it can't be done before the processing_template_decl spot, because the initializer isn't finalized at that point for !procesing_template_decl). Also, decl_maybe_constant_var_p doesn't do what is needed when !processing_template_decl, but I think we want decl_maybe_constant_var_p in templates so that we don't instantiate anything. Lightly tested so far, ok for trunk if it passes full bootstrap/regtest? 2022-11-17 Jakub Jelinek gcc/c-family/ * c-cppbuiltin.cc (c_cpp_builtins): Bump __cpp_constexpr value from 202207L to 202211L. gcc/cp/ * constexpr.cc (cxx_eval_constant_expression): Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions. Allow decl_constant_var_p static or thread_local vars for C++20 and later. (potential_constant_expression_1): For C++20 or later, allow static or thread_local decl_maybe_constant_var_p vars, for !processing_template_decl only decl_constant_var_p vars. * decl.cc (diagnose_static_in_constexpr): New function. (start_decl): Use it for C++17 or earlier. (cp_finish_decl): Call it for C++20. gcc/testsuite/ * g++.dg/cpp23/constexpr-nonlit17.C: New test. * g++.dg/cpp23/constexpr-nonlit18.C: New test. * g++.dg/cpp23/feat-cxx2b.C: Adjust expected __cpp_constexpr value. * g++.dg/ext/stmtexpr19.C: Don't expect an error for C++20 or later. Jakub --- gcc/c-family/c-cppbuiltin.cc.jj 2022-11-17 09:00:42.106249011 +0100 +++ gcc/c-family/c-cppbuiltin.cc 2022-11-17 09:01:49.286320527 +0100 @@ -1074,7 +1074,7 @@ c_cpp_builtins (cpp_reader *pfile) /* Set feature test macros for C++23. */ cpp_define (pfile, "__cpp_size_t_suffix=202011L"); cpp_define (pfile, "__cpp_if_consteval=202106L"); - cpp_define (pfile, "__cpp_constexpr=202207L"); + cpp_define (pfile, "__cpp_constexpr=202211L"); cpp_define (pfile, "__cpp_multidimensional_subscript=202211L"); cpp_define (pfile, "__cpp_named_character_escapes=202207L"); cpp_define (pfile, "__cpp_static_call_operator=202207L"); --- gcc/cp/constexpr.cc.jj 2022-11-17 08:48:30.530357181 +0100 +++ gcc/cp/constexpr.cc 2022-11-17 09:56:50.479522863 +0100 @@ -7098,7 +7098,8 @@ cxx_eval_constant_expression (const cons && (TREE_STATIC (r) || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) /* Allow __FUNCTION__ etc. */ - && !DECL_ARTIFICIAL (r)) + && !DECL_ARTIFICIAL (r) + && (cxx_dialect < cxx20 || !decl_constant_var_p (r))) { if (!ctx->quiet) { @@ -9588,7 +9589,12 @@ potential_constant_expression_1 (tree t, tmp = DECL_EXPR_DECL (t); if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) { - if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) + if (CP_DECL_THREAD_LOCAL_P (tmp) + && !DECL_REALLY_EXTERN (tmp) + && (cxx_dialect < cxx20 + || (processing_template_decl + ? !decl_maybe_constant_var_p (tmp) + : !decl_constant_var_p (tmp)))) { if (flags & tf_error) constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, @@ -9596,7 +9602,11 @@ potential_constant_expression_1 (tree t, "% context", tmp); return false; } - else if (TREE_STATIC (tmp)) + else if (TREE_STATIC (tmp) + && (cxx_dialect < cxx20 + || (processing_template_decl + ? !decl_maybe_constant_var_p (tmp) + : !decl_constant_var_p (tmp)))) { if (flags & tf_error) constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, --- gcc/cp/decl.cc.jj 2022-11-16 14:44:43.692339668 +0100 +++ gcc/cp/decl.cc 2022-11-17 09:54:30.556482833 +0100 @@ -5600,6 +5600,41 @@ groktypename (cp_decl_specifier_seq *typ return type; } +/* For C++17 and older diagnose static or thread_local decls in constexpr + or consteval functions. For C++20 similarly, except if they are + usable in constant expressions. */ + +static void +diagnose_static_in_constexpr (tree decl) +{ + if (current_function_decl && VAR_P (decl) + && DECL_DECLARED_CONSTEXPR_P (current_function_decl) + && cxx_dialect < cxx23 + && (cxx_dialect < cxx20 + || (processing_template_decl + ? !decl_maybe_constant_var_p (decl) + : !decl_constant_var_p (decl)))) + { + bool ok = false; + if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl)) + error_at (DECL_SOURCE_LOCATION (decl), + "%qD defined % in %qs function only " + "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, + DECL_IMMEDIATE_FUNCTION_P (current_function_decl) + ? "consteval" : "constexpr"); + else if (TREE_STATIC (decl)) + error_at (DECL_SOURCE_LOCATION (decl), + "%qD defined % in %qs function only available " + "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, + DECL_IMMEDIATE_FUNCTION_P (current_function_decl) + ? "consteval" : "constexpr"); + else + ok = true; + if (!ok) + cp_function_chain->invalid_constexpr = true; + } +} + /* Process a DECLARATOR for a function-scope or namespace-scope variable or function declaration. (Function definitions go through start_function; class member @@ -5860,28 +5895,8 @@ start_decl (const cp_declarator *declara DECL_THIS_STATIC (decl) = 1; } - if (current_function_decl && VAR_P (decl) - && DECL_DECLARED_CONSTEXPR_P (current_function_decl) - && cxx_dialect < cxx23) - { - bool ok = false; - if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl)) - error_at (DECL_SOURCE_LOCATION (decl), - "%qD defined % in %qs function only " - "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, - DECL_IMMEDIATE_FUNCTION_P (current_function_decl) - ? "consteval" : "constexpr"); - else if (TREE_STATIC (decl)) - error_at (DECL_SOURCE_LOCATION (decl), - "%qD defined % in %qs function only available " - "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, - DECL_IMMEDIATE_FUNCTION_P (current_function_decl) - ? "consteval" : "constexpr"); - else - ok = true; - if (!ok) - cp_function_chain->invalid_constexpr = true; - } + if (cxx_dialect < cxx20) + diagnose_static_in_constexpr (decl); if (!processing_template_decl && VAR_P (decl)) start_decl_1 (decl, initialized); @@ -8424,6 +8439,10 @@ cp_finish_decl (tree decl, tree init, bo set_user_assembler_name (decl, asmspec); DECL_HARD_REGISTER (decl) = 1; } + + if (cxx_dialect == cxx20) + diagnose_static_in_constexpr (decl); + return; } @@ -8749,6 +8768,9 @@ cp_finish_decl (tree decl, tree init, bo && !DECL_HARD_REGISTER (decl)) targetm.lower_local_decl_alignment (decl); + if (cxx_dialect == cxx20) + diagnose_static_in_constexpr (decl); + invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl); } --- gcc/testsuite/g++.dg/cpp23/constexpr-nonlit17.C.jj 2022-11-17 09:00:42.108248984 +0100 +++ gcc/testsuite/g++.dg/cpp23/constexpr-nonlit17.C 2022-11-17 09:00:42.108248984 +0100 @@ -0,0 +1,12 @@ +// P2647R1 - Permitting static constexpr variables in constexpr functions +// { dg-do compile { target c++20 } } + +constexpr char +test () +{ + static const int x = 5; + static constexpr char c[] = "Hello World"; + return *(c + x); +} + +static_assert (test () == ' '); --- gcc/testsuite/g++.dg/cpp23/constexpr-nonlit18.C.jj 2022-11-17 09:29:45.776136195 +0100 +++ gcc/testsuite/g++.dg/cpp23/constexpr-nonlit18.C 2022-11-17 10:04:32.894045579 +0100 @@ -0,0 +1,49 @@ +// P2647R1 - Permitting static constexpr variables in constexpr functions +// { dg-do compile { target c++14 } } + +constexpr int +f1 (int x) +{ + if (x) + throw 1; + return 0; +} + +constexpr int +f2 () +{ + static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } } + return 0; +} + +constexpr int +f3 () +{ + static const int a = 5; // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++17_down } } + return 0; +} + +constexpr int +f4 () // { dg-message "declared here" "" { target c++20_down } } +{ // { dg-message "is not usable as a 'constexpr' function because:" "" { target c++23 } .-1 } + static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } } + return 0; // { dg-error "'a' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 } +} + +constexpr int a4 = f4 (); // { dg-error "called in a constant expression" } + +constexpr int +f5 () +{ + static const int a = f1 (0); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++17_down } } + return 0; +} + +constexpr int +f6 () // { dg-message "declared here" "" { target c++17_down } } +{ + static const int a = f1 (0); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++17_down } } + return 0; +} + +constexpr int a6 = f6 (); // { dg-error "called in a constant expression" "" { target c++17_down } } --- gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C.jj 2022-11-17 08:48:30.561356753 +0100 +++ gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C 2022-11-17 09:00:42.108248984 +0100 @@ -134,8 +134,8 @@ #ifndef __cpp_constexpr # error "__cpp_constexpr" -#elif __cpp_constexpr != 202207 -# error "__cpp_constexpr != 202207" +#elif __cpp_constexpr != 202211 +# error "__cpp_constexpr != 202211" #endif #ifndef __cpp_decltype_auto --- gcc/testsuite/g++.dg/ext/stmtexpr19.C.jj 2022-11-17 08:48:02.730741221 +0100 +++ gcc/testsuite/g++.dg/ext/stmtexpr19.C 2022-11-17 09:00:42.109248970 +0100 @@ -8,7 +8,7 @@ const test* setup() { static constexpr test atest = { - ({ static const int inner = 123; &inner; }) // { dg-error "static" } + ({ static const int inner = 123; &inner; }) // { dg-error "static" "" { target c++17_down } } }; return &atest;