From patchwork Wed May 25 15:08:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 54377 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C9839382F0BE for ; Wed, 25 May 2022 15:09:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9839382F0BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1653491367; bh=EkZ2Tc10vgYHjOBAtc8bItAEBglToJF9GFP6ovxy56M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=QUKcwigimbGUHmq6xWgov4CcyGqiqM/JGR/xtAVG7r/Il2PMdU5A2rGFjCMTM6l0Q Zu4aH6PXUcLK1A3cZm/1wJPZt/ChzpQ8knRjiV0+Qi+mcWWj2nQvbQcRJjWfPb1cmD xUIYnZ+998xhUw9jz+nHy8s8tnaYH2rpJbhZ1bm0= 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 A5B653860C08 for ; Wed, 25 May 2022 15:08:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A5B653860C08 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-481-nZ8ECiWENc6f9IN3nie9iA-1; Wed, 25 May 2022 11:08:57 -0400 X-MC-Unique: nZ8ECiWENc6f9IN3nie9iA-1 Received: by mail-qk1-f199.google.com with SMTP id x9-20020a05620a14a900b006a32ca95a72so13765926qkj.22 for ; Wed, 25 May 2022 08:08:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=EkZ2Tc10vgYHjOBAtc8bItAEBglToJF9GFP6ovxy56M=; b=DYmUEx9p896Rp49HxuPchBxytEj40mrNTEOgBLfvEoNPsS0WBoT4wVWmE38lAfIU+y 5RQfOb81YNlFwB4cbOJtU5+nfU2gTzhWwjiLzYCvmmHEKp1prWLhpDQGNbG3puetXjv3 PJD4QNLmLr75Jht5do6j3fIjMUJjJzmyOWET1bdyfehQQ0o52zxmPujn1Q2SYT/mWxh6 I3WmvBlDfKxsX8bwjGPUZQUH3tnUP5yAnddpamoh91fmqRHuiLvo57FkXm07Oecq1Qfc uFDIpbI4yQzN7jKiRczFm4+P4UQf9NREK63ILk4IfrkUo8bptxpCS2a9vcq8pJs+3NSo DNWA== X-Gm-Message-State: AOAM532YTNHhaKV2Qii7VL6LXd4h4/AvJy5+1u8dzpYsrG3Bb1cC7Mcn QbjN6TQHTXhsoQZE+z6NnaXqdyJITSaFTt0pv+EE4bIMjHMgmDOp+OlGsNv1mq9NazXfNc6ZXi/ u4fJp4EEPszbiDlQ9nHy9uEaUonF3L71gv/pX+fxs1dVCseF+M0TInS1Q2IqgUdqz3Q== X-Received: by 2002:a05:622a:15c6:b0:2f9:5a7:cbb8 with SMTP id d6-20020a05622a15c600b002f905a7cbb8mr25217149qty.369.1653491336286; Wed, 25 May 2022 08:08:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnNT8/In+gI/0E9/mGz/v+3ppjPV1CzGryxZ0maLIgGq1133mF95FNa1FdXNGej1QZo5iUXQ== X-Received: by 2002:a05:622a:15c6:b0:2f9:5a7:cbb8 with SMTP id d6-20020a05622a15c600b002f905a7cbb8mr25217093qty.369.1653491335698; Wed, 25 May 2022 08:08:55 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id j10-20020a37c24a000000b006a36059ed59sm1389902qkm.27.2022.05.25.08.08.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 May 2022 08:08:54 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: strict constexpr and local vars Date: Wed, 25 May 2022 11:08:52 -0400 Message-Id: <20220525150852.1115540-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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_LOW, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" A change I was working on made constexpr_searcher.cc start to fail, and when I looked at it I wondered why it had been accepted before. This turned out to be because we try to be more flexible about constant-evaluation of static initializers, as allowed, but we were wrongly doing the same for non-static initializers as well. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * constexpr.cc (maybe_constant_init_1): Only pass false for strict when initializing a variable of static duration. libstdc++-v3/ChangeLog: * testsuite/20_util/function_objects/constexpr_searcher.cc: Add constexpr. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-local4.C: New test. --- gcc/cp/constexpr.cc | 12 +++++++++--- gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C | 17 +++++++++++++++++ .../function_objects/constexpr_searcher.cc | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C base-commit: 1b661f3f5e712c951e774b3b91fffe4dac734cc7 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 388239ea8a8..1a70fda1dc5 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8301,9 +8301,15 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, else if (CONSTANT_CLASS_P (t) && allow_non_constant) /* No evaluation needed. */; else - t = cxx_eval_outermost_constant_expr (t, allow_non_constant, - /*strict*/false, - manifestly_const_eval, false, decl); + { + /* [basic.start.static] allows constant-initialization of variables with + static or thread storage duration even if it isn't required, but we + shouldn't bend the rules the same way for automatic variables. */ + bool is_static = (decl && DECL_P (decl) + && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); + t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, + manifestly_const_eval, false, decl); + } if (TREE_CODE (t) == TARGET_EXPR) { tree init = TARGET_EXPR_INITIAL (t); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C new file mode 100644 index 00000000000..bef62488579 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C @@ -0,0 +1,17 @@ +// { dg-do compile { target c++14 } } + +struct A +{ + int i; + constexpr A(int i): i(i) {}; +}; + +const A a = 42; + +constexpr int f() +{ + const int j = a.i; // { dg-message "'a'" } + return j; +} + +static_assert (f() == 42,""); // { dg-error "non-constant" } diff --git a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc index 33012200cb0..17069694c1b 100644 --- a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc +++ b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc @@ -28,13 +28,13 @@ #include -const std::string_view +constexpr std::string_view patt = "World"; constexpr std::string_view greet = "Hello, Humongous World of Wonder!!!"; -const std::wstring_view +constexpr std::wstring_view wpatt = L"World"; constexpr std::wstring_view