From patchwork Wed May 10 15:04: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: 92170 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3693754vqo; Wed, 10 May 2023 08:06:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ75aGdMEc3WxR40vcanq8Vlv+32BVaQgR3EzSP/vpKdVot+iPuIA0KA5CfNnO7unA2rAWNs X-Received: by 2002:a17:906:db07:b0:94f:6058:4983 with SMTP id xj7-20020a170906db0700b0094f60584983mr13454141ejb.76.1683731179540; Wed, 10 May 2023 08:06:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683731179; cv=none; d=google.com; s=arc-20160816; b=OI8jwd8f7vD8iUAXd2Rqi2ctUOWncK5+wSYxS1y4R0XltH5QEaXFidMC8OeO170f6M sR3YzpdolkW/s4gBIfieKaaNds0kB8mdhAUIQyO8df0AQ6Wafz3Smw2Zf6dWQWjQcQIN gFrFL85b9ikIkmkTv7PzxCjXJriV9FCcw2NPEJ/+amFODXGgU2xELA7YXsnCslkYsZRQ aPa+2QJ+8d5gvCUJqTMc+Hqov3Q6Zf8sYWP6XlDe048vibfqhW6BwtNq8yIWcVp0vdD8 5ik1Mz6o89x3D5EMBl09P9ssIcl5uj1HDOC8jPA3L5jY5eKDetT7nUoRTYR2aXHYKoak P9+A== 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-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=RRn0E7LiHwIy3+I4le8kYJT3sRl8/8uLz+W6IQejdzM=; b=0FoSzuxBbxTBnH4du/3hRZgVZ9E/sEUSuOLdNII+oRZgGun8Dac+phujIkx8tZjM6u HZy+3Ewbb4ci/rjbf1+Iyqj7HmC8Vo6HjYyqmEJTnH9/3Yfo0PNCjHQqYep3vE0bFMud tTca8eOHCei6j5Fcv9Xt+4L0WfCqEKShWev5uzKHUMLxsNm8SjGi+fxjQzhlac9Z02bL yiVHacOLnzZPdFOsrgMDwbgVRnn20QpjnY1fqYKI+MV9TW420paS0nGdZrt2TO8eYWS6 KO8BUFwGOuZcZITRkdn49cPF4dSHeFlxMrxN0a0Q/ySqQwjdZUY/etoBJSscIoQpYkBX 9QBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pSGGgbY8; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id nb11-20020a1709071c8b00b0094f9f324507si4924462ejc.192.2023.05.10.08.06.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 08:06:19 -0700 (PDT) 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=pSGGgbY8; 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 53E083853541 for ; Wed, 10 May 2023 15:06:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53E083853541 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683731174; bh=RRn0E7LiHwIy3+I4le8kYJT3sRl8/8uLz+W6IQejdzM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pSGGgbY8thGDELshnoyiODjkrHJSc2yz9sFkTRYS3kPPd6Ph8QRajgJK1zP74WSFM fRse1Ezsc9xpsrSAnKAdaUitU+Qju3kCD5NIva3q8RB3mTjfYrm4Cb0GzH922AFdNg 6aZUyTOL9+U6jUr2kHOXZWPgiNxT2IM+pexYKq6Y= 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 2BF79385702F for ; Wed, 10 May 2023 15:05:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BF79385702F Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-651-fQhvIX5qPES2RGQGtP2hyA-1; Wed, 10 May 2023 11:05:10 -0400 X-MC-Unique: fQhvIX5qPES2RGQGtP2hyA-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-61af548a8feso45135976d6.0 for ; Wed, 10 May 2023 08:05:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683731109; x=1686323109; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RRn0E7LiHwIy3+I4le8kYJT3sRl8/8uLz+W6IQejdzM=; b=I9MnjTJBBcBwLpSRyXknwU6ZLf/P0uUnAmzR/Kgc1XAC4tdR25ZFQzVGT4i9bj8HCR iZ3+2lKyyGM/WxCxZPE4zFq0Qn/hcrEbaGAuJl8G/rKG3uU2uB1GBrBxm6DxDhqRas2+ 1oLlNUC3AGVUd6Co41C+RnKJaRQ0d2j1//BKziyWXoo5iKYF2n5UGND7DbN+MmpKMRFq CGLyJqy5xx6TeaEiXxmjFe7udE+36mX8aRn+iOQ9C1Bqp0n7LdgVBr3CEVSxOsaFxx2Y VtSZpediy2ZCA4WCt9x+/AEci6bdPveejWsKYdzQXW1g1/+uk1JJMUiSKoreLIhBs/6Z RFqg== X-Gm-Message-State: AC+VfDwnAm+JETjq4iqPxGWO+KeaGnH1nJKyj/lyrkeUyhvs4f6hs2L8 9cugsuo1EhwvXmn79POOvIRuH+PSqLrasIldwh5WjuUY78fNK+b0UGH2XIhuDUx8J802Fkn1iyp VksEQottzbIG+vBqg8hofZmq5k3YZceVGImY6AYIJlDUeqKsN3qKSHhdrYWP5hhA6+8v8fOd7BA == X-Received: by 2002:a05:6214:20e2:b0:616:4da9:9aff with SMTP id 2-20020a05621420e200b006164da99affmr24149810qvk.16.1683731109131; Wed, 10 May 2023 08:05:09 -0700 (PDT) X-Received: by 2002:a05:6214:20e2:b0:616:4da9:9aff with SMTP id 2-20020a05621420e200b006164da99affmr24149733qvk.16.1683731108351; Wed, 10 May 2023 08:05:08 -0700 (PDT) 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 b10-20020a0cbf4a000000b0061b58b07130sm1583524qvj.137.2023.05.10.08.04.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 08:04:56 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed 1/2] c++: always check consteval address Date: Wed, 10 May 2023 11:04:40 -0400 Message-Id: <20230510150441.850396-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 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, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill 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?1765520105020682669?= X-GMAIL-MSGID: =?utf-8?q?1765520105020682669?= Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- The restriction on the "permitted result of a constant expression" to not refer to an immediate function applies regardless of context. The previous code tried to only check in cases where we wouldn't get the check in cp_fold_r, but with the next patch I would need to add another case and it shouldn't be a problem to always check. We also shouldn't talk about immediate evaluation when we aren't dealing with one. gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_outermost_constant_expr): Always check for address of immediate fn. (maybe_constant_init_1): Evaluate PTRMEM_CST. gcc/testsuite/ChangeLog: * g++.dg/DRs/dr2478.C: Handle -fimplicit-constexpr. * g++.dg/cpp23/consteval-if12.C: Adjust diagnostics. * g++.dg/cpp2a/consteval20.C: Likewise. * g++.dg/cpp2a/consteval24.C: Likewise. * g++.dg/cpp2a/srcloc20.C: Likewise. --- gcc/cp/constexpr.cc | 22 ++++++++++++++------- gcc/testsuite/g++.dg/DRs/dr2478.C | 17 +++++++++------- gcc/testsuite/g++.dg/cpp23/consteval-if12.C | 8 ++++---- gcc/testsuite/g++.dg/cpp2a/consteval20.C | 4 ++-- gcc/testsuite/g++.dg/cpp2a/consteval24.C | 8 ++++---- gcc/testsuite/g++.dg/cpp2a/srcloc20.C | 4 ++-- 6 files changed, 37 insertions(+), 26 deletions(-) base-commit: 204303c81e82ddd01e7dc5a5a63719d476f9043c diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 987a536d515..7b8090625e8 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8353,7 +8353,8 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, non_constant_p = true; } - if (!global_ctx.heap_vars.is_empty ()) + if (!non_constant_p && cxx_dialect >= cxx20 + && !global_ctx.heap_vars.is_empty ()) { tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, NULL); @@ -8384,15 +8385,22 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, /* Check that immediate invocation does not return an expression referencing any immediate function decls. */ - if (is_consteval || in_immediate_context ()) + if (!non_constant_p && cxx_dialect >= cxx20) if (tree immediate_fndecl = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, NULL)) { if (!allow_non_constant && !non_constant_p) - error_at (cp_expr_loc_or_input_loc (t), - "immediate evaluation returns address of immediate " - "function %qD", immediate_fndecl); + { + if (is_consteval) + error_at (cp_expr_loc_or_input_loc (t), + "immediate evaluation returns address of immediate " + "function %qD", immediate_fndecl); + else + error_at (cp_expr_loc_or_input_loc (t), + "constant evaluation returns address of immediate " + "function %qD", immediate_fndecl); + } r = t; non_constant_p = true; } @@ -8795,8 +8803,8 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, t = TARGET_EXPR_INITIAL (t); if (!is_nondependent_static_init_expression (t)) /* Don't try to evaluate it. */; - else if (CONSTANT_CLASS_P (t) && allow_non_constant) - /* No evaluation needed. */; + else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST) + /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */; else { /* [basic.start.static] allows constant-initialization of variables with diff --git a/gcc/testsuite/g++.dg/DRs/dr2478.C b/gcc/testsuite/g++.dg/DRs/dr2478.C index 7e939ac6850..7f581cabb7b 100644 --- a/gcc/testsuite/g++.dg/DRs/dr2478.C +++ b/gcc/testsuite/g++.dg/DRs/dr2478.C @@ -1,11 +1,14 @@ // DR 2478 - Properties of explicit specializations of implicitly-instantiated class templates // { dg-do compile { target c++20 } } +// Defeat -fimplicit-constexpr +int ii; + template struct S { - int foo () { return 0; } + int foo () { return ii; } constexpr int bar () { return 0; } - int baz () { return 0; } + int baz () { return ii; } consteval int qux () { return 0; } constexpr S () {} static constinit T x; @@ -14,7 +17,7 @@ struct S { template T S::x = S ().foo (); // { dg-error "'constinit' variable 'S::x' does not have a constant initializer" } - // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 } + // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 } template T S::y = S ().foo (); @@ -49,14 +52,14 @@ S::qux () template <> long S::x = S ().foo (); // { dg-bogus "'constinit' variable 'S::x' does not have a constant initializer" "" { xfail *-*-* } } - // { dg-bogus "call to non-'constexpr' function" "" { xfail *-*-* } .-1 } + // { dg-bogus "call to non-'constexpr' function|called in a constant expression" "" { xfail *-*-* } .-1 } template <> constinit long S::y = S ().foo (); // { dg-error "'constinit' variable 'S::y' does not have a constant initializer" } - // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 } + // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 } constinit auto a = S ().foo (); // { dg-error "'constinit' variable 'a' does not have a constant initializer" } - // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 } + // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 } constinit auto b = S ().bar (); constinit auto c = S ().foo (); constinit auto d = S ().bar (); // { dg-error "'constinit' variable 'd' does not have a constant initializer" } @@ -65,7 +68,7 @@ constinit auto e = S ().baz (); constinit auto f = S ().qux (); // { dg-error "'constinit' variable 'f' does not have a constant initializer" } // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 } constinit auto g = S ().baz (); // { dg-error "'constinit' variable 'g' does not have a constant initializer" } - // { dg-error "call to non-'constexpr' function" "" { target *-*-* } .-1 } + // { dg-error "call to non-'constexpr' function|called in a constant expression" "" { target *-*-* } .-1 } constinit auto h = S ().qux (); auto i = S::x; auto j = S::x; diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if12.C b/gcc/testsuite/g++.dg/cpp23/consteval-if12.C index 7a47680e5d8..03de87c3e09 100644 --- a/gcc/testsuite/g++.dg/cpp23/consteval-if12.C +++ b/gcc/testsuite/g++.dg/cpp23/consteval-if12.C @@ -19,10 +19,10 @@ bar () { S s; if consteval { // { dg-warning "'if consteval' only available with" "" { target c++20_only } } - constexpr auto fn1 = foo; // { dg-error "immediate evaluation returns address of immediate function" } - constexpr auto fn2 = &foo; // { dg-error "immediate evaluation returns address of immediate function" } - constexpr auto fn3 = &S::foo; // { dg-error "immediate evaluation returns address of immediate function" } - constexpr auto fn4 = &S::bar; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn1 = foo; // { dg-error "constant evaluation returns address of immediate function" } + constexpr auto fn2 = &foo; // { dg-error "constant evaluation returns address of immediate function" } + constexpr auto fn3 = &S::foo; // { dg-error "constant evaluation returns address of immediate function" } + constexpr auto fn4 = &S::bar; // { dg-error "constant evaluation returns address of immediate function" } constexpr auto fn5 = baz (); // { dg-error "immediate evaluation returns address of immediate function" } constexpr auto fn6 = qux (); // { dg-error "immediate evaluation returns address of immediate function" } constexpr auto fn7 = corge (); // { dg-error "immediate evaluation returns address of immediate function" } diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval20.C b/gcc/testsuite/g++.dg/cpp2a/consteval20.C index bd44712c535..ba3613318e6 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval20.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval20.C @@ -11,12 +11,12 @@ int bar () { auto c = &S::foo; // { dg-error "taking address of an immediate function" } - constexpr auto d = &S::foo; // { dg-error "taking address of an immediate function" } + constexpr auto d = &S::foo; // { dg-error "constant evaluation returns address of immediate function" } static auto e = &S::foo; // { dg-error "taking address of an immediate function" } return (s.*&S::foo) (); // { dg-error "taking address of an immediate function" } } -constexpr auto a = &S::foo; // { dg-error "taking address of an immediate function" } +constexpr auto a = &S::foo; // { dg-error "constant evaluation returns address of immediate function" } auto b = &S::foo; // { dg-error "taking address of an immediate function" } consteval int diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval24.C b/gcc/testsuite/g++.dg/cpp2a/consteval24.C index 6d7034c5515..22a9657e4a1 100644 --- a/gcc/testsuite/g++.dg/cpp2a/consteval24.C +++ b/gcc/testsuite/g++.dg/cpp2a/consteval24.C @@ -17,10 +17,10 @@ consteval int bar () { S s; - constexpr auto fn1 = foo; // { dg-error "immediate evaluation returns address of immediate function" } - constexpr auto fn2 = &foo; // { dg-error "immediate evaluation returns address of immediate function" } - constexpr auto fn3 = &S::foo; // { dg-error "immediate evaluation returns address of immediate function" } - constexpr auto fn4 = &S::bar; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn1 = foo; // { dg-error "constant evaluation returns address of immediate function" } + constexpr auto fn2 = &foo; // { dg-error "constant evaluation returns address of immediate function" } + constexpr auto fn3 = &S::foo; // { dg-error "constant evaluation returns address of immediate function" } + constexpr auto fn4 = &S::bar; // { dg-error "constant evaluation returns address of immediate function" } constexpr auto fn5 = baz (); // { dg-error "immediate evaluation returns address of immediate function" } constexpr auto fn6 = qux (); // { dg-error "immediate evaluation returns address of immediate function" } constexpr auto fn7 = corge (); // { dg-error "immediate evaluation returns address of immediate function" } diff --git a/gcc/testsuite/g++.dg/cpp2a/srcloc20.C b/gcc/testsuite/g++.dg/cpp2a/srcloc20.C index acdf5a6505f..e99f1dcf7c4 100644 --- a/gcc/testsuite/g++.dg/cpp2a/srcloc20.C +++ b/gcc/testsuite/g++.dg/cpp2a/srcloc20.C @@ -33,12 +33,12 @@ namespace std { using namespace std; auto a = source_location::current; // { dg-error "taking address of an immediate function" } -constexpr auto b = &source_location::current; // { dg-error "taking address of an immediate function" } +constexpr auto b = &source_location::current; // { dg-error "constant evaluation returns address of immediate function" } void foo () { auto c = &source_location::current; // { dg-error "taking address of an immediate function" } - constexpr auto d = source_location::current; // { dg-error "taking address of an immediate function" } + constexpr auto d = source_location::current; // { dg-error "constant evaluation returns address of immediate function" } static auto e = source_location::current; // { dg-error "taking address of an immediate function" } } From patchwork Wed May 10 15:04:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 92171 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3694279vqo; Wed, 10 May 2023 08:06:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ejE5ch20liQxDxLhg5kKPIozi6+1TjVfnDutKw2CYhvkSchwodpoL+hD590dvOeqqAeC4 X-Received: by 2002:aa7:dbca:0:b0:50b:d557:d446 with SMTP id v10-20020aa7dbca000000b0050bd557d446mr12974898edt.2.1683731211705; Wed, 10 May 2023 08:06:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683731211; cv=none; d=google.com; s=arc-20160816; b=zoO8AavSBbtCD2SXgnJybJ7a0iZDrWfsZzrDgck8f6SQVNME6pR2MlgumyxIkpWBFJ Hw+KhkRK9YVpHgkz7UP+Bj/FgHAS3W3MJwsygRMCTIyV3v5pWhShM1KWTcUV8TmQDT4B WiAGOfQVqXwB9CjrL2smlu3ZAR+UCdZ0ZMXMa5d4AF483awv4cTHbaQLzjIp3gp9p7y6 YZWdJ8/ApiWG4AcMhLgYxUmRlxI0jSTxe7QBaromkLaxf/tHcjmnbGNgE+s4CT/IapPb JzLq08pU3bP90XWj6yye9AE8+sSDbpxHsU9uJZx6IS1eb8zhU13+mDKDYh9UVSA64WGR BTlQ== 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-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=432cC4Bel4BAmA4X1JK2bHzEMFk+MT4ytl60dxHxyAA=; b=PRIJ96xLgYjIrZ7HcVee7huMU97x7mnYn7Rxcaa9cHWnO/1vxMv8cU5B5KMBAun0fK GkcW1xJgMmJOgDJ5bc05lLcGeBD5fyLAEDR4IbrgqFqe0gTbv2CumRC/RnRLbMBrPxP+ +dv/PsPZo66bCfSWMnXG6qV3prkVtkcJfLGw0FNgUWUNO3Z8L7lVSYGpaUvYJlPc5JTQ AJpxzHWeIVBIvcjqN7iEI3zf6zBjUKYkEyperX0pCrILumJyk+7Shk5f66qq46M9OxXx vJr1K7Zxktz91hzgyWviLoqhHglyaPKHGOB92DKroHR25sAitfgmtSFAd/4VWzczXl6f wgDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FwjygFwj; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b21-20020aa7c915000000b0050bfa164b9dsi3237853edt.275.2023.05.10.08.06.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 08:06:51 -0700 (PDT) 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=FwjygFwj; 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 EE33A3853810 for ; Wed, 10 May 2023 15:06:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE33A3853810 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683731206; bh=432cC4Bel4BAmA4X1JK2bHzEMFk+MT4ytl60dxHxyAA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=FwjygFwjPfqijoLn17ZhUG6H/knPgYNY+9OkCa5LgIZEHRIcJV/N197gCkt0b6ZIv 5yg38nruxyTAI0x8Pp3qWaqijNdiOg+XsTaurIy+nf3Yh1EQDw0ZYpxCdhwVa6edMK FvcvwCVtxc4yfDSfqTu6aKu8xtZVRfS6rjG2B4gg= 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 67A35385B534 for ; Wed, 10 May 2023 15:05:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67A35385B534 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-384-KFwazmR8MFe_V0xrTPcH5w-1; Wed, 10 May 2023 11:05:41 -0400 X-MC-Unique: KFwazmR8MFe_V0xrTPcH5w-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7578369dff3so1279889385a.0 for ; Wed, 10 May 2023 08:05:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683731141; x=1686323141; 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=432cC4Bel4BAmA4X1JK2bHzEMFk+MT4ytl60dxHxyAA=; b=d8NYyJYMDR6Qu/NaVAPOQUgFzXCuZkjDrAtLXdV9RVGmV06Mjh7NPVJRFRruLKD9e2 iqxu2C3jh5AhW4G6MbPAGbF1t+3VbiCRu15w22r/rX/7D88mAkRzVeybWDw9G1cL2jSa DsNctBOTzYBPmK66yLUJ5i05Z7FGbB49CcISDXrmHB7PBoIT5zVjNpdh0uB70Y0PLkNR X+QgvTNgTgABwdtggsQjzcPRtfwoM4osPL4foLXIZwOUSkbcLeZBcm6oKRZr75ECdCqj 1ztFKtGYr4fuZESRwIevCkedKeemcWont+l1zhBb3tTHRMShs8JOHTDIyRHdwBRMC5gU fYZw== X-Gm-Message-State: AC+VfDz0mJ9J9g4LNBrojIWawLsjYLDvBFGkb0Z45MzM/vHvQx923YDC wPbEXyXKmeVvbe1Xzb0ub9HQMbvxiqxhDSAooFkW8WNy71OeBEUoHNWmAgGK6zAg/dP59siMjKO wcYZGEcMNjUfdNmGYEzHAJn6XVMre0gSBVmcRc7YTvXSujqcNhinuV4q4y4A7tUx0FWQl5OL0pg == X-Received: by 2002:a05:6214:4018:b0:5ef:48c7:3f62 with SMTP id kd24-20020a056214401800b005ef48c73f62mr23287422qvb.15.1683731140922; Wed, 10 May 2023 08:05:40 -0700 (PDT) X-Received: by 2002:a05:6214:4018:b0:5ef:48c7:3f62 with SMTP id kd24-20020a056214401800b005ef48c73f62mr23287373qvb.15.1683731140487; Wed, 10 May 2023 08:05:40 -0700 (PDT) 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 b10-20020a0cbf4a000000b0061b58b07130sm1583524qvj.137.2023.05.10.08.05.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 08:05:21 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed 2/2] c++: be stricter about constinit [CWG2543] Date: Wed, 10 May 2023 11:04:41 -0400 Message-Id: <20230510150441.850396-2-jason@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230510150441.850396-1-jason@redhat.com> References: <20230510150441.850396-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 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_H2, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill 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?1765520138842301716?= X-GMAIL-MSGID: =?utf-8?q?1765520138842301716?= Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- DR 2543 clarifies that constinit variables should follow the language, and diagnose non-constant initializers (according to [expr.const]) even if they can actually initialize the variables statically. DR 2543 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_outermost_constant_expr): Preserve TARGET_EXPR flags. (potential_constant_expression_1): Check TARGET_EXPR_ELIDING_P. * typeck2.cc (store_init_value): Diagnose constinit sooner. gcc/testsuite/ChangeLog: * g++.dg/DRs/dr2543.C: New test. --- gcc/cp/constexpr.cc | 12 +++++++ gcc/cp/typeck2.cc | 55 +++++++++++++++++-------------- gcc/testsuite/g++.dg/DRs/dr2543.C | 5 +++ 3 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2543.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 7b8090625e8..8f7f0b7d325 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8448,6 +8448,17 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, } } + if (TREE_CODE (t) == TARGET_EXPR + && TREE_CODE (r) == TARGET_EXPR) + { + /* Preserve this flag for potential_constant_expression, and the others + for good measure. */ + TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t); + TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t); + TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t); + TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t); + } + /* Remember the original location if that wouldn't need a wrapper. */ if (location_t loc = EXPR_LOCATION (t)) protected_set_expr_location (r, loc); @@ -9774,6 +9785,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, case TARGET_EXPR: if (!TARGET_EXPR_DIRECT_INIT_P (t) + && !TARGET_EXPR_ELIDING_P (t) && !literal_type_p (TREE_TYPE (t))) { if (flags & tf_error) diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index bf03967a71f..f5cc7c8371c 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -843,23 +843,45 @@ store_init_value (tree decl, tree init, vec** cleanups, int flags) bool const_init; tree oldval = value; if (DECL_DECLARED_CONSTEXPR_P (decl) + || DECL_DECLARED_CONSTINIT_P (decl) || (DECL_IN_AGGR_P (decl) && DECL_INITIALIZED_IN_CLASS_P (decl))) { value = fold_non_dependent_expr (value, tf_warning_or_error, /*manifestly_const_eval=*/true, decl); + if (value == error_mark_node) + ; /* Diagnose a non-constant initializer for constexpr variable or non-inline in-class-initialized static data member. */ - if (!require_constant_expression (value)) - value = error_mark_node; - else if (processing_template_decl) - /* In a template we might not have done the necessary - transformations to make value actually constant, - e.g. extend_ref_init_temps. */ - value = maybe_constant_init (value, decl, true); + else if (!is_constant_expression (value)) + { + /* Maybe we want to give this message for constexpr variables as + well, but that will mean a lot of testsuite adjustment. */ + if (DECL_DECLARED_CONSTINIT_P (decl)) + error_at (location_of (decl), + "% variable %qD does not have a " + "constant initializer", decl); + require_constant_expression (value); + value = error_mark_node; + } else - value = cxx_constant_init (value, decl); + { + value = maybe_constant_init (value, decl, true); + + /* In a template we might not have done the necessary + transformations to make value actually constant, + e.g. extend_ref_init_temps. */ + if (!processing_template_decl + && !TREE_CONSTANT (value)) + { + if (DECL_DECLARED_CONSTINIT_P (decl)) + error_at (location_of (decl), + "% variable %qD does not have a " + "constant initializer", decl); + value = cxx_constant_init (value, decl); + } + } } else value = fold_non_dependent_init (value, tf_warning_or_error, @@ -875,22 +897,7 @@ store_init_value (tree decl, tree init, vec** cleanups, int flags) if (!TYPE_REF_P (type)) TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); if (!const_init) - { - /* [dcl.constinit]/2 "If a variable declared with the constinit - specifier has dynamic initialization, the program is - ill-formed." */ - if (DECL_DECLARED_CONSTINIT_P (decl)) - { - error_at (location_of (decl), - "% variable %qD does not have a constant " - "initializer", decl); - if (require_constant_expression (value)) - cxx_constant_init (value, decl); - value = error_mark_node; - } - else - value = oldval; - } + value = oldval; } /* Don't fold initializers of automatic variables in constexpr functions, that might fold away something that needs to be diagnosed at constexpr diff --git a/gcc/testsuite/g++.dg/DRs/dr2543.C b/gcc/testsuite/g++.dg/DRs/dr2543.C new file mode 100644 index 00000000000..87512d30455 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2543.C @@ -0,0 +1,5 @@ +// CWG 2543 +// { dg-do compile { target c++20 } } + +float f; +constinit int * pi = (int*) &f; // { dg-error "constant" } reinterpret_cast, not constant-initialized