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.129.124]) by sourceware.org (Postfix) with ESMTPS id CE4AE3858039 for ; Fri, 11 Nov 2022 17:07:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CE4AE3858039 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=1668186439; 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; bh=KmSIBPmAuLDWDRQ5kLptKlEthItKJCuNwrGcKUBuMZ0=; b=bsFlJD8PPBAZo0zeUSlZwuP9E1pploj6ZrRKTXySD2QTFWiftpGdizog8r2muUHckXopFH 9IOgdUi2CQefgQmcdOcN7MV7OnhFj5hKiZ4CBRgzhmC8bVwtNm1acJn9+GTF7Qq7yUbVFh mmoosa48bU/NyPouTsy4Xidf1u0j9+8= 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-329-4IC6iWYcPFCvucSzbyz2Hw-1; Fri, 11 Nov 2022 12:07:07 -0500 X-MC-Unique: 4IC6iWYcPFCvucSzbyz2Hw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DC2803810D3A for ; Fri, 11 Nov 2022 17:07:06 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9627B492B10; Fri, 11 Nov 2022 17:07:06 +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 2ABH73Xw3264910 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 11 Nov 2022 18:07:04 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2ABH73An3264909; Fri, 11 Nov 2022 18:07:03 +0100 Date: Fri, 11 Nov 2022 18:07:02 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, Marek Polacek Subject: [PATCH] c++: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 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.9 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: Hi! The following patch on top of Marek's P2448 PR106649 patch (mainly because that patch implements the previous __cpp_constexpr feature test macro bump so this can't go in earlier; OT, P2280R4 doesn't have any feature test macro?) implements this simple paper. Ok for trunk if it passes bootstrap/regtest and is voted into C++23? 2022-11-11 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_maybe_constant_var_p static or thread_local vars for C++23. (potential_constant_expression_1): Likewise. gcc/testsuite/ * g++.dg/cpp23/constexpr-nonlit17.C: New test. * g++.dg/cpp23/feat-cxx2b.C: Adjust expected __cpp_constexpr value. --- gcc/c-family/c-cppbuiltin.cc.jj 2022-11-11 17:14:52.021613271 +0100 +++ gcc/c-family/c-cppbuiltin.cc 2022-11-11 17:17:45.065265246 +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=202110L"); cpp_define (pfile, "__cpp_named_character_escapes=202207L"); cpp_define (pfile, "__cpp_static_call_operator=202207L"); --- gcc/cp/constexpr.cc.jj 2022-11-11 17:14:52.024613231 +0100 +++ gcc/cp/constexpr.cc 2022-11-11 17:16:54.384952917 +0100 @@ -7085,7 +7085,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 < cxx23 || !decl_maybe_constant_var_p (r))) { if (!ctx->quiet) { @@ -9577,7 +9578,10 @@ 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 < cxx23 + || !decl_maybe_constant_var_p (tmp))) { if (flags & tf_error) constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, @@ -9585,7 +9589,9 @@ potential_constant_expression_1 (tree t, "% context", tmp); return false; } - else if (TREE_STATIC (tmp)) + else if (TREE_STATIC (tmp) + && (cxx_dialect < cxx23 + || !decl_maybe_constant_var_p (tmp))) { if (flags & tf_error) constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, --- gcc/testsuite/g++.dg/cpp23/constexpr-nonlit17.C.jj 2022-11-11 17:59:59.972852793 +0100 +++ gcc/testsuite/g++.dg/cpp23/constexpr-nonlit17.C 2022-11-11 17:59:38.725141231 +0100 @@ -0,0 +1,12 @@ +// P2647R1 - Permitting static constexpr variables in constexpr functions +// { dg-do compile { target c++23 } } + +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/feat-cxx2b.C.jj 2022-11-11 17:14:52.194610922 +0100 +++ gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C 2022-11-11 17:48:56.038865825 +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 Jakub