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 AF825385454D for ; Fri, 18 Nov 2022 00:15:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AF825385454D 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=1668730518; 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: in-reply-to:in-reply-to:references:references; bh=LmHiA7sPW+RoL5cpBsdc1l0O2aUzCsGOprCM4SIokQI=; b=dFjPC5Rtuv2e4EJTH/qTsx0QQgoif7XinQPsskOJ3V0FUDNnB/L8O67DROyVnw8M65fv3q DLI9abPi4buSf5g6JLBidwV8QylWVu1L8Do7+8VcCqvx7BO1GOuHDFj9XeMj9Crzo8lZ/4 AT95auf5WTwAr4Yeop3OK8+8XzeU1Bk= 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_128_GCM_SHA256) id us-mta-343-QFQSPTdIONuo9RgHFjQQtA-1; Thu, 17 Nov 2022 19:15:08 -0500 X-MC-Unique: QFQSPTdIONuo9RgHFjQQtA-1 Received: by mail-qk1-f199.google.com with SMTP id bp10-20020a05620a458a00b006fa29f253dcso4282367qkb.11 for ; Thu, 17 Nov 2022 16:15:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LmHiA7sPW+RoL5cpBsdc1l0O2aUzCsGOprCM4SIokQI=; b=dNmM/92osWux4qi3oYytwxJQJiudNeTYwzZpfJD+oWKa4GkYd9ypAfF2pY+9Z0lF6S 7oTWkCvan2qAXZ2yVfHs7Gw35dgznvDuQRt1irY4xPVMeS8hw1KK0dk93haZv7L387Tf KcGhocloMtE0eucFsUFySeXHbW2IKPDABhnaEUwYbvHphJ6vCeVK3dp9LiKPDiepjL77 sbOYsCWRaP0pPf/ASwVyW2O4Ifs8RwpS2SYf5cPTMHS2d9ZBcY0hUmAlm4xEuj3GbX6G LKmRAyQVvK/XdzGn3EAiWK1YohagD3TCm2P5ynyL185iT+KKipoQRXEAoudtW+Hafjns riaw== X-Gm-Message-State: ANoB5pnHBsjsmH/PzC/bqTEwY4EnnJB1qvnDZG/ejuuRrfTfS8dUlpUk rXuRCFx9HkdPtYkZPcO5g4/Cz5Pn9xViD3DqU4xgHvblpHtxSEap9TBbqv7kr40UjpQUXqztQs/ t3q/w0ps5v/fdct/faA== X-Received: by 2002:ac8:4d06:0:b0:3a5:95cc:70e2 with SMTP id w6-20020ac84d06000000b003a595cc70e2mr4593715qtv.293.1668730507817; Thu, 17 Nov 2022 16:15:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf69tFfxiXxp3ji8fk6ZRNHIlvfxFNSSpNWCBMnnAk/QCPwGYOI/U3tgmQ1ThLfFz+WGDusm4Q== X-Received: by 2002:ac8:4d06:0:b0:3a5:95cc:70e2 with SMTP id w6-20020ac84d06000000b003a595cc70e2mr4593703qtv.293.1668730507574; Thu, 17 Nov 2022 16:15:07 -0800 (PST) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id bp34-20020a05620a45a200b006fb0e638f12sm1492866qkb.4.2022.11.17.16.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 16:15:06 -0800 (PST) Date: Thu, 17 Nov 2022 19:15:05 -0500 From: Marek Polacek To: Jakub Jelinek Cc: Jason Merrill , Jonathan Wakely , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++, v4: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions Message-ID: References: <016f168b-f143-baff-5f71-c48d4611ae11@redhat.com> <740b5e1e-7143-c291-5594-af937867fbc3@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.7 (2022-08-07) 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=-6.2 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, Nov 17, 2022 at 09:42:17PM +0100, Jakub Jelinek wrote: > On Thu, Nov 17, 2022 at 07:42:40PM +0100, Jakub Jelinek via Gcc-patches wrote: > > I thought for older C++ this is to catch > > void > > foo () > > { > > constexpr int a = ({ static constexpr int b = 2; b; }); > > } > > and for C++23 the only 3 spots that diagnose those. > > But perhaps for C++20 or older we can check if the var has a context > > of a constexpr function (then assume cp_finish_decl errored or pedwarned > > already) and only error or pedwarn otherwise. > > So, here is an updated patch, which in constexpr.cc will accept > DECL_EXPR of decl_*constant_var_p static/thread_local non-extern vars > for C++23 or if they are not declared in constexpr/consteval function. > So, the statement expression case will remain hard error for C++ <= 20 rather than > pedwarn, because due to the ctx->quiet vs. !ctx->quiet case I don't see > what else we could do, either something is a constant expression, or > it is not, but whether it is or is not shouldn't depend on > -Wpedantic/-Wno-pedantic/-Werror=pedantic. > > 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++23 and later or if they are declared inside of constexpr or > consteval function. > (potential_constant_expression_1): Similarly, except use > decl_maybe_constant_var_p instead of decl_constant_var_p if > processing_template_decl. > * decl.cc (diagnose_static_in_constexpr): New function. > (start_decl): Remove diagnostics of static or thread_local > vars in constexpr or consteval functions. > (cp_finish_decl): Call diagnose_static_in_constexpr. > gcc/testsuite/ > * g++.dg/cpp23/constexpr-nonlit17.C: New test. > * g++.dg/cpp23/constexpr-nonlit18.C: New test. > * g++.dg/cpp23/constexpr-nonlit19.C: New test. > * g++.dg/cpp23/constexpr-nonlit20.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. > > --- 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 20:53:15.432408015 +0100 > @@ -7100,17 +7100,35 @@ cxx_eval_constant_expression (const cons > /* Allow __FUNCTION__ etc. */ > && !DECL_ARTIFICIAL (r)) > { > - if (!ctx->quiet) > + bool ok = decl_constant_var_p (r); > + /* Since P2647R1 control can pass through definitions of static > + or thread_local vars usable in constant expressions. > + In C++20 or older, if such vars are declared inside of > + constexpr or consteval function, diagnose_static_in_constexpr > + should have already pedwarned on those. Otherwise they could > + be e.g. in a statement expression, reject those before > + C++23. */ > + if (ok && cxx_dialect < cxx23) > { > - if (CP_DECL_THREAD_LOCAL_P (r)) > - error_at (loc, "control passes through definition of %qD " > - "with thread storage duration", r); > - else > - error_at (loc, "control passes through definition of %qD " > - "with static storage duration", r); > + tree fnctx = decl_function_context (r); > + if (fnctx == NULL_TREE > + || !DECL_DECLARED_CONSTEXPR_P (fnctx)) > + ok = false; FWIW, I couldn't find a way to trigger this code. > + } > + if (!ok) > + { > + if (!ctx->quiet) > + { > + if (CP_DECL_THREAD_LOCAL_P (r)) > + error_at (loc, "control passes through definition of " > + "%qD with thread storage duration", r); > + else > + error_at (loc, "control passes through definition of " > + "%qD with static storage duration", r); > + } > + *non_constant_p = true; > + break; > } > - *non_constant_p = true; > - break; > } > > if (AGGREGATE_TYPE_P (TREE_TYPE (r)) > @@ -9588,21 +9606,41 @@ 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 (flags & tf_error) > - constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, > - "%qD defined % in " > - "% context", tmp); > - return false; > - } > - else if (TREE_STATIC (tmp)) > + if (TREE_STATIC (tmp) > + || (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))) > { > - if (flags & tf_error) > - constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, > - "%qD defined % in % " > - "context", tmp); > - return false; > + bool ok = (processing_template_decl > + ? decl_maybe_constant_var_p (tmp) > + : decl_constant_var_p (tmp)); > + /* Since P2647R1 control can pass through definitions of static > + or thread_local vars usable in constant expressions. > + In C++20 or older, if such vars are declared inside of > + constexpr or consteval function, diagnose_static_in_constexpr > + should have already pedwarned on those. Otherwise they could > + be e.g. in a statement expression, reject those before > + C++23. */ > + if (ok && cxx_dialect < cxx23) > + { > + tree fnctx = decl_function_context (tmp); > + if (fnctx == NULL_TREE > + || !DECL_DECLARED_CONSTEXPR_P (fnctx)) > + ok = false; > + } > + if (!ok) > + { > + if (flags & tf_error) > + { > + if (CP_DECL_THREAD_LOCAL_P (tmp)) > + constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, > + "%qD defined % in " > + "% context", tmp); > + else > + constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, > + "%qD defined % in " > + "% context", tmp); > + } > + return false; > + } > } > else if (!check_for_uninitialized_const_var > (tmp, /*constexpr_context_p=*/true, flags)) > --- gcc/cp/decl.cc.jj 2022-11-16 14:44:43.692339668 +0100 > +++ gcc/cp/decl.cc 2022-11-17 20:53:44.102011594 +0100 > @@ -5600,6 +5600,57 @@ 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 In C++17 we don't support consteval so I guess drop the "or consteval "? BTW, I notice that the patch breaks g++.dg/cpp1y/lambda-generic-func1.C g++.dg/cpp1z/constexpr-lambda16.C Maybe they just need dg- tweaks. Marek